第二十六章 ansible主要模組介紹
阿新 • • 發佈:2020-09-23
一、Ansible模組回顧
1.command模組
[root@m01 ~]# ansible web01 -m command -a 'free -m'
2.shell模組
#支援管道符這種特殊符號
[root@m01 ~]# ansible web01 -m shell -a 'ps -ef | grep httpd'
[root@m01 ~]# ansible web01 -m shell -a 'yum localinstall -y /package/*.rpm'
3.script模組
[root@m01 ~]# ansible web01 -m script -a '/root/mkdir.sh'
4.yum模組
#1.使用服務名字安裝
[root@m01 ~]# ansible web01 -m yum -a 'name=httpd state=present'
#2.使用網上安裝包的地址安裝
[root@m01 ~]# ansible web01 -m yum -a 'name=http://.../.../.../httpd.rpm state=present'
#3.使用本地rpm包(包一定在受控端)
[root@m01 ~]# ansible web01 -m yum -a 'name=/package/http.rpm state=present'
yum
name:
httpd #包的名字
http://.../.../... #包在網上的地址
/package/http.rpm #包在受控端的地址
state
latest #安裝最新的包
present #安裝
absent #解除安裝
5.yum_repository模組
6.copy模組
#1.推送控制端檔案到受控端
[root@m01 ~]# ansible web01 -m copy -a 'src=/root/mkdir.sh dest=/tmp/'
#2.推送檔案並授權
[root@m01 ~]# ansible web01 -m copy -a 'src=/root/mkdir.sh dest=/tmp/ owner=nginx group=nginx mode=0644'
#3.推送檔案並將原檔案備份
[root@m01 ~]# ansible web01 -m copy -a 'src=/root/mkdir.sh dest=/tmp/ backup=yes'
#4.直接將內容寫到受控端新檔案
[root@m01 ~]# ansible web01 -m copy -a 'content="rsync_backup:123456" dest=/tmp/rsync_password owner=rsync group=rsync mode=600'
#注意:
1.授權時,使用者必須是存在的系統使用者
2.檔案可以修改名字
3.推送檔案至目錄,目標地址最好在最後加 /
copy
src: #控制端的檔案或目錄
dest: #受控端的目錄
owner: #檔案推送後的屬主
group: #檔案推送後的屬組
backup: #檔案推送後,是否備份原檔案
follow: #識別軟連結
mode: #檔案推送後的許可權
content: #直接將內容寫入檔案
7.file模組
#1.建立目錄
[root@m01 ~]# ansible web01 -m file -a 'path=/code/worpdress state=directory'
#2.建立檔案
[root@m01 ~]# ansible web01 -m file -a 'path=/code/worpdress/index.html state=touch'
#3.建立檔案或目錄時授權
[root@m01 ~]# ansible web01 -m file -a 'path=/code/worpdress/index.php state=touch owner=nginx group=nginx mode=777'
#4.遞迴授權
[root@m01 ~]# ansible web01 -m file -a 'path=/code/ owner=nginx group=nginx mode=777 recurse=yes'
#注意:
1.建立檔案時上層目錄必須存在
2.授權時,直接授權上層目錄加遞迴引數即可
file
src: #做軟連線時的原目錄
dest: #做軟連線時的目標檔案
path: #要在受控端建立的檔案或目錄
owner: #屬主
group: #屬組
mode: #許可權
recurse: #遞迴授權
state:
directory #建立目錄
touch #建立檔案
link #軟連結
absent #刪除
8.get_url
#1.下載檔案
[root@m01 ~]# ansible web01 -m get_url -a 'url=http://10.0.0.7/index.html dest=/tmp/'
#2.下載檔案並授權
[root@m01 ~]# ansible web01 -m get_url -a 'url=http://10.0.0.7/index.html dest=/tmp/ owner=nginx group=nginx mode=777'
get_url:
url: #要下載的內容
dest: #下載到哪裡,可以改名
mode: #下載包後的許可權
checksum: #下載是進行驗證
sha256:
md5:
owner: #下載包後的屬主
group: #下載包後的屬組
二、Ansible 模組 新
1.service模組
1)幫助語法
[root@m01 ~]# ansible-doc service
EXAMPLES:
- name: Start service httpd, if not started
service:
name: httpd
state: started
enabled: yes
name: httpd #服務的名字
state:
started #啟動服務
stopped #停止服務
restarted #重啟服務
reloaded #過載服務
enabled: #開機自啟
yes
no
2)例項
#1.停止nginx服務
[root@m01 ~]# ansible web03 -m service -a 'name=nginx state=stopped'
#2.啟動httpd服務,並加入開機自啟
[root@m01 ~]# ansible web03 -m service -a 'name=httpd state=started enabled=yes'
2.systemd模組
1)幫助語法
[root@m01 ~]# ansible-doc systemd
EXAMPLES:
- name: Start service httpd, if not started
systemd:
name: httpd
state: started
enabled: yes
daemon_reload: yes
name: httpd #服務的名字
state:
started #啟動服務
stopped #停止服務
restarted #重啟服務
reloaded #過載服務
enabled: #開機自啟
yes
no
daemon_reload: #後臺啟動
2)例項
#1.停止nginx服務
[root@m01 ~]# ansible web03 -m systemd -a 'name=nginx state=stopped'
#2.啟動httpd服務,並加入開機自啟
[root@m01 ~]# ansible web03 -m systemd -a 'name=httpd state=started enabled=yes'
3.group模組
1)幫助語法
EXAMPLES:
- name: Ensure group "somegroup" exists
group:
name: somegroup #組名字
state:
present #建立使用者組
absent #刪除使用者組
gid: 666 #使用者組ID
2)例項
#建立使用者組
[root@m01 ~]# ansible web_group -m group -a 'name=www state=present gid=666'
#刪除使用者組
[root@m01 ~]# ansible web_group -m group -a 'name=www state=absent'
4.user模組
1)幫助語法
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
user:
name: johnd #使用者名稱
comment: John Doe #使用者的註釋
uid: 1040 #使用者id
group: admin #使用者的組
groups: admins,developers #指定附加組
shell: /bin/bash #指定登入指令碼
append: yes #新增附加組時使用
remove: yes #移除家目錄
generate_ssh_key: yes #是否生成金鑰對
ssh_key_bits: 2048 #祕鑰加密的位數
ssh_key_file: .ssh/id_rsa #祕鑰檔案
expires: 1422403387 #使用者的有效時間
state:
present #新增使用者
absent #刪除使用者
create_home:yes/no #是否建立家目錄
password #給使用者新增密碼(單引號)
2)實踐
#1.建立使用者
[root@m01 ~]# ansible web_group -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present'
#2.僅刪除使用者
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent'
#3.刪除使用者及使用者組
[root@m01 ~]# ansible web_group -m user -a 'name=www state=absent remove=yes'
#注意:
1.如果使用者名稱字跟組名字相同,刪除使用者是會將組也刪除
2.當組下面有多個使用者,刪除的與組同名的使用者也不會刪除組
5.cron模組
1)幫助語法
EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /d
cron:
name: "check dirs" #定時任務的註釋
minute: "0" #分鐘
hour: "5,2" #小時
day: "2" #日
month: "2" #月
weekday: "2" #周
job: "ls -alh > /dev/null" #定時任務的內容
state:
absent #刪除定時任務
present #新增定時任務
2)實踐
#1.新增定時任務
[root@m01 ~]# ansible web01 -m cron -a 'name="時間同步" minute=*/10 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#2.修改定時任務(不修改名字,只修改內容)
[root@m01 ~]# ansible web01 -m cron -a 'name="時間同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#3.刪除定時任務(一定要用name引數)
[root@m01 ~]# ansible web01 -m cron -a 'name="時間同步" state=absent'
#4.註釋定時任務
[root@m01 ~]# ansible web01 -m cron -a 'name="時間同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null" disabled=yes'
6.磁碟掛載mount模組
1)幫助語法
EXAMPLES:
# Before 2.3, option 'name' was used instead of 'path'
- name: Mount DVD read-only
mount:
path: /mnt/dvd #掛載的目錄(nfs客戶端)
src: /dev/sr0 #遠端被掛載的目錄 (nfs服務端)
fstype: nfs #掛在型別
opts: ro,noauto #自動掛載的引數
state:
present #寫入自動掛載,但實際沒有掛咋,需要重啟伺服器
unmounted #取消臨時掛載,但是沒有清理自動掛載
mounted #寫入自動掛載,並且直接掛載了(常用)
absent #取消臨時掛載,並且清理自動掛載(常用)
2)準備掛載的服務端
1.安裝nfs
[root@m01 ~]# ansible nfs -m yum -a 'name=nfs-utils state=present'
2.配置nfs
[root@m01 ~]# ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'
3.建立使用者
[root@m01 ~]# ansible nfs -m group -a 'name=www gid=666'
[root@m01 ~]# ansible nfs -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no'
4.建立目錄並授權
[root@m01 ~]# ansible nfs -m file -a 'path=/data state=directory owner=www group=www'
5.啟動
[root@m01 ~]# ansible nfs -m systemd -a 'name=nfs state=started'
3)客戶端使用模組掛載
#掛載目錄,並加入開機自動掛載
[root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=mounted'
#取消掛載,並取消開機自動掛載
[root@m01 ~]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=absent'
7.selinux模組
1)幫助語法
EXAMPLES:
- name: Enable SELinux
selinux:
policy: targeted
state:
enforcing #開啟
disabled #關閉
2)關閉selinux
[root@m01 ~]# ansible web01 -m selinux -a 'state=disabled'
8.firewalld模組
1)幫助語法
EXAMPLES:
- firewalld:
service: https #防火牆開啟的服務
permanent:
yes #永久生效
no #臨時生效
state:
enabled #開啟
disable #關閉
port: 8081/tcp 161-162/udp #防火牆配置的埠
zone: dmz #指定配置空間
rich_rule: #富規則
source: 192.0.2.0/24 #防火牆配置的源ip
masquerade:
yes #開啟ip偽裝
no #關閉ip偽裝
interface: eth2 #繫結網絡卡
2)防火牆配置實踐
#1.允許訪問http,永久生效
[root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
#2.允許80埠被訪問,臨時生效
[root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'
#3.允許10.0.0.0/24網段訪問22埠
[root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 service name=ssh accept" state=enabled'
#4.允許10.0.0.0/24網段訪問所有服務
[root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'
9.unarchive 解壓模組
1)幫助語法
- name: Unarchive a file that is already on the remote machine
unarchive:
src: /tmp/foo.zip #要解壓的包
dest: /usr/local/bin #解壓到目標位置
remote_src:
yes #要解壓的包在受控端
no #要解壓的包在控制端
2)例項
#1.解壓控制端的包到受控端
[root@m01 /package]# ansible web01 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/'
#2.解壓受控端的包到受控端
[root@m01 /package]# ansible web03 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/ remote_src=yes'
10.archive 壓縮模組
1)幫助語法
EXAMPLES:
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
archive:
path: /path/to/foo #要壓縮的檔案或目錄
dest: /path/to/foo.tgz #壓縮後的檔案
format:bz2, gz, tar, xz, zip #指定打包的型別
2)例項
#1.打包站點目錄
[root@m01 /package]# ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz'
11.Ansible主機資訊模組 setup
為什麼要講這個模組?
這個模組非常實用,在公司中總會有一些需求
比如:
1.根據不同主機不同IP建立對應IP的目錄
2.根據不同主機不同主機名建立對應主機名的目錄
3.自動化運維平臺需要自動獲取到主機的IP地址,記憶體資訊,磁碟資訊,主機名...等
4.如果安裝資料庫,分配記憶體為實體記憶體的80%,此時有3臺不同實體記憶體的機器2G、4G、16G
寫一個playbook的情況下,我需要獲取到對應主機的記憶體並作出計算,寫判斷。
1)使用
1.獲取所有主機資訊
[root@m01 ~]# ansible web01 -m setup
2.獲取主機名(使用setup獲取的資訊,指定對應的小標題獲取指定的資訊)
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
2)常用的引數
ansible_all_ipv4_addresses:僅顯示ipv4的資訊。
ansible_devices:僅顯示磁碟裝置資訊。
ansible_distribution:顯示是什麼系統,例:centos,suse等。
ansible_distribution_major_version:顯示是系統主版本。
ansible_distribution_version:僅顯示系統版本。
ansible_machine:顯示系統型別,例:32位,還是64位。
ansible_eth0:僅顯示eth0的資訊。
ansible_hostname:僅顯示主機名。
ansible_kernel:僅顯示核心版本。
ansible_lvm:顯示lvm相關資訊。
ansible_memtotal_mb:顯示系統總記憶體。
ansible_memfree_mb:顯示可用系統記憶體。
ansible_memory_mb:詳細顯示記憶體情況。
ansible_swaptotal_mb:顯示總的swap記憶體。
ansible_swapfree_mb:顯示swap記憶體的可用記憶體。
ansible_mounts:顯示系統磁碟掛載情況。
ansible_processor:顯示cpu個數(具體顯示每個cpu的型號)。
ansible_processor_vcpus:顯示cpu個數(只顯示總的個數)。
三、ansible搭建作業平臺
1.環境準備
主機名 | IP | 角色 |
---|---|---|
m01 | 10.0.0.61 | 跳板機 |
web01 | 172.16.1.7 | web伺服器 |
web02 | 172.16.1.8 | web伺服器 |
2.m01配置
1.上傳相關原始碼包
[root@m01 ~]# rz
[root@m01 ~]# ll
-rw-r--r-- 1 root root 26995 Aug 13 16:42 kaoshi.zip
-rw-r--r-- 1 root root 19889622 Aug 26 09:04 php.tar.gz
2.編寫指令碼
[root@m01 ~]# vim zuoye.sh