1. 程式人生 > 其它 >ansible 常用模組詳解

ansible 常用模組詳解

1 主機連通性測試

  我們使用ansible web -m ping命令來進行主機連通性測試,效果如下:
[root@jenkins ~]# ansible test -m ping
192.168.64.129 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@jenkins ~]#
這樣就說明我們的主機是連通狀態的。接下來的操作才可以正常進行。

2 command 模組

  這個模組可以直接在遠端主機上執行命令,並將結果返回本主機。
  舉例如下:
[root@jenkins ~]# ansible test -m command -a 'ss -ntl'
192.168.64.129 | CHANGED | rc=0 >>
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         1024             127.0.0.1:9093             0.0.0.0:*       
LISTEN    0         1024             127.0.0.1:9100             0.0.0.0:*       
LISTEN    0         1024             127.0.0.1:9229             0.0.0.0:*       
LISTEN    0         128                0.0.0.0:111              0.0.0.0:*       
LISTEN    0         511                0.0.0.0:80               0.0.0.0:*       
LISTEN    0         1024             127.0.0.1:8080             0.0.0.0:*       
LISTEN    0         128              127.0.0.1:9168             0.0.0.0:*       
LISTEN    0         128              127.0.0.1:8082             0.0.0.0:*       

命令模組接受命令名稱,後面是空格分隔的列表引數。指定的命令將在所有選定的節點上執行。它不會通過shell進行處理,比如$HOME和操作如"<",">","|",";","&" 工作(需要使用(shell)模組實現這些功能)。
注意,該命令不支援| 管道命令。
下面來看一看該模組下常用的幾個命令:
chdir    # 在執行命令之前,先切換到該目錄
executable  # 切換shell來執行命令,需要使用命令的絕對路徑
free_form  # 要執行的Linux指令,一般使用Ansible的-a引數代替。
creates    # 一個檔名,當這個檔案存在,則該命令不執行,可以用來做判斷
removes     # 一個檔名,這個檔案不存在,則該命令不執行
下面我們來看看這些命令的執行效果:
#先切換到/mnt/ 目錄,再執行“ls”命令
[root@jenkins ~]# ansible test -m command -a 'chdir=/mnt/ ls'
192.168.64.129 | CHANGED | rc=0 >>
hgfs
#如果/mnt/hgts存在,則不執行“ls” 命令
[root@jenkins ~]# ansible test -m command -a 'creates=/mnt/hgfs ls'
192.168.64.129 | SUCCESS | rc=0 >>
skipped, since /mnt/hgfs exists
#如果/mnt/hgfs存在,則執行“cat ls /mnt”命令
[root@jenkins ~]# ansible test -m command -a 'removes=/mnt/hgfs ls /mnt/'
192.168.64.129 | CHANGED | rc=0 >>
Hgfs

3 shell 模組

shell模組可以在遠端主機上呼叫shell直譯器執行命令,支援shell的各種功能,例如管道等。
[root@jenkins ~]# ansible test -m shell -a 'cat /etc/passwd |grep "sunpengfei"'
192.168.64.129 | CHANGED | rc=0 >>
sunpengfei:x:1000:1000:sunpengfei:/home/sunpengfei:/bin/bash
只要是我們的shell命令,都可以通過這個模組在遠端主機上執行,這裡就不一一舉例了。

4 copy 模組

這個模組用於將檔案複製到遠端主機,同時支援指定內容生成檔案和修改許可權等。
其相關選項如下:
src   #被複制到遠端主機的本地檔案。可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,則會遞迴複製,用法類似於"rsync"
content  #用於替換"src",可以直接指定檔案的值
dest     #必選項,將原始檔複製到的遠端主機的絕對路徑
backup      #當檔案內容發生改變後,在覆蓋之前把原始檔備份,備份檔案包含時間資訊
directory_mode #遞迴設定目錄的許可權,預設為系統預設許可權
force       #當目標主機包含該檔案,但內容不同時,設為"yes",表示強制覆蓋;設為"no",表示目標主機的目標位置不存在該檔案才複製。預設為"yes"
others     #所有的 file 模組中的選項可以在這裡使用

4.1用法舉例如下:

4.1.1複製檔案:

[root@jenkins ~]# ansible test -m copy -a 'src=~/abc.txt dest=/data/abc.txt'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "03cfd743661f07975fa2f1220c5194cbaff48451",
    "dest": "/data/abc.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "0bee89b07a248e27c83fc3d5951213c1",
    "mode": "0644",
    "owner": "root",
    "size": 4,
    "src": "/root/.ansible/tmp/ansible-tmp-1620374566.5316975-34707-14765376079630/source",
    "state": "file",
"uid": 0
}

4.1.2指定內容生成檔案,並制定許可權

[root@jenkins ~]# ansible test -m copy -a 'content="I am keer\n" dest=/data/name mode=666'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "0421570938940ea784f9d8598dab87f07685b968",
    "dest": "/data/name",
    "gid": 0,
    "group": "root",
    "md5sum": "497fa8386590a5fc89090725b07f175c",
    "mode": "0666",
    "owner": "root",
    "size": 10,
    "src": "/root/.ansible/tmp/ansible-tmp-1620374768.1690423-34768-206471036865339/source",
    "state": "file",
    "uid": 0
}
我們現在可以去檢視一下我們生成的檔案及其許可權:
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data/'
192.168.64.129 | CHANGED | rc=0 >>
總用量 8
-rw-r--r-- 1 root root  4 57 16:02 abc.txt
-rw-rw-rw- 1 root root 10 57 16:06 name
可以看出我們的name檔案已經生成,並且許可權為666。

4.1.3關於覆蓋

我們把檔案的內容修改一下,然後選擇覆蓋備份:
[root@jenkins ~]# ansible test -m copy -a 'content="I am keerya\n" backup=yes dest=/data/name mode=666'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/data/name.61216.2021-05-07@16:13:25~",
    "changed": true,
    "checksum": "064a68908ab9971ee85dbc08ea038387598e3778",
    "dest": "/data/name",
    "gid": 0,
    "group": "root",
    "md5sum": "8ca7c11385856155af52e560f608891c",
    "mode": "0666",
    "owner": "root",
    "size": 12,
    "src": "/root/.ansible/tmp/ansible-tmp-1620375204.8166783-34875-156887504370617/source",
    "state": "file",
    "uid": 0
}
現在我們可以去檢視一下:
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data/'
192.168.64.129 | CHANGED | rc=0 >>
總用量 12
-rw-r--r-- 1 root root  4 57 16:02 abc.txt
-rw-rw-rw- 1 root root 12 57 16:13 name
-rw-rw-rw- 1 root root 10 57 16:06 name.61216.2021-05-07@16:13:25~

5 file 模組

該模組主要用於設定檔案的屬性,比如建立檔案、建立連結檔案、刪除檔案等。
下面是一些常見的命令:
force  #需要在兩種情況下強制建立軟連結,一種是原始檔不存在,但之後會建立的情況下;另一種是目標軟連結已存在,需要先取消之前的軟鏈,然後建立新的軟鏈,有兩個選項:yes|no
group  #定義檔案/目錄的屬組。後面可以加上mode:定義檔案/目錄的許可權
owner  #定義檔案/目錄的屬主。後面必須跟上path:定義檔案/目錄的路徑
recurse  #遞迴設定檔案的屬性,只對目錄有效,後面跟上src:被連結的原始檔路徑,只應用於state=link的情況
dest  #被連結到的路徑,只應用於state=link的情況
state  #狀態,有以下選項:
directory:如果目錄不存在,就建立目錄
file:即使檔案不存在,也不會被建立
link:建立軟連結
hard:建立硬連結
touch:如果檔案不存在,則會建立一個新的檔案,如果檔案或目錄已存在,則更新其最後修改時間
absent:刪除目錄、檔案或者取消連結檔案

5.2用法舉例如下:

5.2.1建立目錄:

[root@jenkins ~]# ansible test -m file -a 'path=/data/app state=directory'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/data/app",
    "size": 6,
    "state": "directory",
    "uid": 0
}
檢視一下建立的檔案
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data'
192.168.64.129 | CHANGED | rc=0 >>
總用量 12
-rw-r--r-- 1 root root  4 57 16:02 abc.txt
drwxr-xr-x 2 root root  6 57 16:17 app
-rw-rw-rw- 1 root root 12 57 16:13 name
-rw-rw-rw- 1 root root 10 57 16:06 name.61216.2021-05-07@16:13:25~

5.2.2建立連結檔案

[root@jenkins ~]#  ansible test -m file -a 'path=/data/aaa.txt src=abc.txt state=link'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/data/aaa.txt",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 7,
    "src": "abc.txt",
    "state": "link",
    "uid": 0
}
驗證一下
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data'
192.168.64.129 | CHANGED | rc=0 >>
總用量 12
lrwxrwxrwx 1 root root  7 57 16:21 aaa.txt -> abc.txt
-rw-r--r-- 1 root root  4 57 16:02 abc.txt
drwxr-xr-x 2 root root  6 57 16:17 app
-rw-rw-rw- 1 root root 12 57 16:13 name
-rw-rw-rw- 1 root root 10 57 16:06 name.61216.2021-05-07@16:13:25~

5.2.3刪除檔案

[root@jenkins ~]# ansible test -m file -a 'path=/data/aaa.txt state=absent'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/data/aaa.txt",
    "state": "absent"
}
檢視一下發現aaa.txt連結檔案已被刪除
[root@jenkins ~]# ansible test -m shell -a 'ls -l /data'
192.168.64.129 | CHANGED | rc=0 >>
總用量 12
-rw-r--r-- 1 root root  4 57 16:02 abc.txt
drwxr-xr-x 2 root root  6 57 16:17 app
-rw-rw-rw- 1 root root 12 57 16:13 name
-rw-rw-rw- 1 root root 10 57 16:06 name.61216.2021-05-07@16:13:25~

6 fetch 模組

  該模組用於從遠端某主機獲取(複製)檔案到本地。
  有兩個選項:
dest:用來存放檔案的目錄
src:在遠端拉取的檔案,並且必須是一個file,不能是目錄
  具體舉例如下:
[root@jenkins ~]#  ansible test -m fetch -a 'src=/data/name.61216.2021-05-07@16:13:25~ dest=~/' 
192.168.64.129 | CHANGED => {
    "changed": true,
    "checksum": "0421570938940ea784f9d8598dab87f07685b968",
    "dest": "/root/192.168.64.129/data/name.61216.2021-05-07@16:13:25~",
    "md5sum": "497fa8386590a5fc89090725b07f175c",
    "remote_checksum": "0421570938940ea784f9d8598dab87f07685b968",
    "remote_md5sum": null
}
我們可以在本機上檢視一下檔案是否複製成功。要注意,檔案儲存的路徑是我們設定的接收目錄下的被管制主機ip目錄下:
[root@jenkins ~]# ll ~/192.168.64.129/data/
總用量 4
-rw-r--r-- 1 root root 10 57 16:31 name.61216.2021-05-07@16:13:25~

7 cron 模組

  該模組適用於管理cron計劃任務的。
  其使用的語法跟我們的crontab檔案中的語法一致,同時,可以指定以下選項:
day= #日應該執行的工作( 1-31, *, */2, )
hour= # 小時 ( 0-23, *, */2, )
minute= #分鐘( 0-59, *, */2, )
month= # 月( 1-12, *, /2, )
weekday= # 周 ( 0-6 for Sunday-Saturday,, )
job= #指明執行的命令是什麼
name= #定時任務描述
reboot # 任務在重啟時執行,不建議使用,建議使用special_time
special_time #特殊的時間範圍,引數:reboot(重啟時),annually(每年),monthly(每月),weekly(每週),daily(每天),hourly(每小時)
state #指定狀態,present表示新增定時任務,也是預設設定,absent表示刪除定時任務
user # 以哪個使用者的身份執行

7.1舉例如下:

7.1.1新增計劃任務

[root@jenkins ~]# ansible test -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "ntp update every 5 min"
    ]
}
我們可以去檢視一下:
[root@jenkins ~]# ansible test -m shell -a 'crontab -l'
192.168.64.129 | CHANGED | rc=0 >>
#Ansible: ntp update every 5 min
*/5 * * * * /sbin/ntpdate 172.17.0.1 &> /dev/null

7.1.2 刪除計劃任務

[root@jenkins ~]# ansible test -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null >> /tmp/disk_total &> /dev/null" state=absent'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}
刪除完成後,我們再檢視一下現有的計劃任務確認一下:
[root@jenkins ~]#  ansible test -m shell -a 'crontab -l'
192.168.64.129 | CHANGED | rc=0 >>
我們的刪除操作已經成功。

8 yum 模組

  顧名思義,該模組主要用於軟體的安裝。
  其選項如下:
name=          #所安裝的包的名稱
state=         #present--->安裝, latest--->安裝最新的, absent---> 解除安裝軟體。
update_cache   #強制更新yum的快取
conf_file     #指定遠端yum安裝時所依賴的配置檔案(安裝本地已有的包)。
disable_pgp_check  #是否禁止GPG checking,只用於presentor latest。
disablerepo   #臨時禁止使用yum庫。 只用於安裝或更新時。
enablerepo    #臨時使用的yum庫。只用於安裝或更新時。
下面我們就來安裝一個包試試看:
[root@jenkins ~]# ansible app -m yum -a 'name=httpd state=present'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64",
        "Installed: httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fdbbcc.noarch",
        "Installed: centos-logos-httpd-80.5-2.el8.noarch",
        "Installed: httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64",
        "Installed: apr-1.6.3-11.el8.x86_64",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
    ]
}
解除安裝軟體:
[root@jenkins ~]# ansible app -m yum -a 'name=httpd state=absent'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
        "Removed: httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64"
    ]
}

9 service 模組

  該模組用於服務程式的管理。
  其主要選項如下:
arguments #命令列提供額外的引數
enabled #設定開機啟動。
name= #服務名稱
runlevel #開機啟動的級別,一般不用指定。
sleep #在重啟服務的過程中,是否等待。如在服務關閉以後等待2秒再啟動。(定義在劇本中。)
state #有四種狀態,分別為:started--->啟動服務, stopped--->停止服務, restarted--->重啟服務, reloaded--->過載配置

例子:

9.1開啟服務並設定自啟動

[root@jenkins ~]# ansible app -m service -a 'name=nginx state=started enabled=true'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "nginx",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",
        "After": "network.target basic.target nss-lookup.target systemd-journald.socket sysinit.target tmp.mount remote-fs.target -.mount systemd-tmpfiles-setup.service system.slice",
………
我們可以去檢視一下80埠是否開啟:
[root@jenkins ~]# ansible app -m shell -a 'ss -ntl'
192.168.64.128 | CHANGED | rc=0 >>
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128                0.0.0.0:111              0.0.0.0:*       
LISTEN    0         128                0.0.0.0:80               0.0.0.0:*       
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*       
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*       
LISTEN    0         5                127.0.0.1:631              0.0.0.0:*       
LISTEN    0         128                   [::]:111                 [::]:*       
LISTEN    0         128                   [::]:80                  [::]:*       
LISTEN    0         128                   [::]:22                  [::]:*       
LISTEN    0         5                    [::1]:631                 [::]:*     

9.2關閉服務

[root@jenkins ~]# ansible app -m service -a 'name=nginx state=stopped'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "nginx",
"state": "stopped",
……
我們在檢視一下80埠:
[root@jenkins ~]# ansible app -m shell -a 'ss -ntl'
192.168.64.128 | CHANGED | rc=0 >>
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128                0.0.0.0:111              0.0.0.0:*       
LISTEN    0         32           192.168.122.1:53               0.0.0.0:*       
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*       
LISTEN    0         5                127.0.0.1:631              0.0.0.0:*       
LISTEN    0         128                   [::]:111                 [::]:*       
LISTEN    0         128                   [::]:22                  [::]:*       
LISTEN    0         5                    [::1]:631                 [::]:*     

10 user 模組

  該模組主要是用來管理使用者賬號。
  其主要選項如下:
comment     # 使用者的描述資訊
createhome  # 是否建立家目錄
force  		# 在使用state=absent時, 行為與userdel –force一致.
group  		# 指定基本組
groups 		# 指定附加組,如果指定為(groups=)表示刪除所有組
home  		# 指定使用者家目錄
move_home 	# 如果設定為home=時, 試圖將使用者主目錄移動到指定的目錄
name 			# 指定使用者名稱
non_unique  # 該選項允許改變非唯一的使用者ID值
password  	# 指定使用者密碼
remove  		# 在使用state=absent時, 行為是與userdel –remove一致
shell  		# 指定預設shell
state  		# 設定帳號狀態,不指定為建立,指定值為absent表示刪除
system  		# 當建立一個使用者,設定這個使用者是系統使用者。這個設定不能更改現有使用者
uid  			# 指定使用者的uid
舉例如下:

10.1新增一個使用者並指定其 uid

[root@jenkins ~]# ansible all -m user -a 'name=spf uid=11111'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 11111,
    "home": "/home/spf",
    "name": "spf",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 11111
}
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 11111,
    "home": "/home/spf",
    "name": "spf",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 11111
}
驗證使用者是否存在:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/passwd |grep spf'
192.168.64.128 | CHANGED | rc=0 >>
spf:x:11111:11111::/home/spf:/bin/bash
192.168.64.129 | CHANGED | rc=0 >>
spf:x:11111:11111::/home/spf:/bin/bash

10.2刪除使用者

[root@jenkins ~]# ansible all -m user -a 'name=spf state=absent'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "spf",
    "remove": false,
    "state": "absent"
}
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "spf",
    "remove": false,
    "state": "absent"
}
再次驗證:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/passwd |grep spf'
192.168.64.128 | FAILED | rc=1 >>
non-zero return code
192.168.64.129 | FAILED | rc=1 >>
non-zero return code
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/passwd |grep root'
192.168.64.128 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
192.168.64.129 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

11 group 模組

  該模組主要用於新增或刪除組。
  常用的選項如下:
gid=  	#設定組的GID號
name=  	#指定組的名稱
state= 	#指定組的狀態,預設為建立,設定值為absent為刪除
system= 	#設定值為yes,表示建立為系統組
舉例如下:

11.1建立組

[root@jenkins ~]# ansible all -m group -a 'name=spfgroup gid=12222'
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 12222,
    "name": "spfgroup",
    "state": "present",
    "system": false
}
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 12222,
    "name": "spfgroup",
    "state": "present",
    "system": false
}
建立過後,我們來檢視一下:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/group | grep 12222'
192.168.64.128 | CHANGED | rc=0 >>
spfgroup:x:12222:
192.168.64.129 | CHANGED | rc=0 >>
spfgroup:x:12222:

11.2刪除組

[root@jenkins ~]#  ansible all -m group -a 'name=spfgroup state=absent'
192.168.64.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "spfgroup",
    "state": "absent"
}
192.168.64.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "spfgroup",
    "state": "absent"
}
再次驗證:
[root@jenkins ~]# ansible all -m shell -a 'cat /etc/group | grep 12222' 
192.168.64.129 | FAILED | rc=1 >>
non-zero return code
192.168.64.128 | FAILED | rc=1 >>
non-zero return code

12 script 模組

該模組用於將本機的指令碼在被管理端的機器上執行。
  該模組直接指定指令碼的路徑即可,我們通過例子來看一看到底如何使用的:
  首先,我們寫一個指令碼,並給其加上執行許可權:
[root@jenkins ~]# vim /tmp/df.sh
[root@jenkins ~]# cat /tmp/df.sh 
#!/bin/bash

date >> /tmp/disk_total.log
df -lh >> /tmp/disk_total.log

[root@jenkins ~]# chmod +x /tmp/df.sh
然後,我們直接執行命令來實現在被管理端執行該指令碼:
[root@jenkins ~]# ansible all -m script -a '/tmp/df.sh'
192.168.64.129 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.64.129 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.64.129 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
192.168.64.128 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.64.128 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.64.128 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
照例檢視一下檔案內容:
[root@jenkins ~]# ansible all -m shell -a 'cat /tmp/disk_total.log'
192.168.64.128 | CHANGED | rc=0 >>
20210507星期五 17:35:43 CST
檔案系統                     容量  已用  可用 已用% 掛載點
devtmpfs                     866M     0  866M    0% /dev
tmpfs                        896M     0  896M    0% /dev/shm
tmpfs                        896M  9.7M  886M    2% /run
tmpfs                        896M     0  896M    0% /sys/fs/cgroup
/dev/mapper/cl_leanote-root   64G  5.0G   60G    8% /
/dev/mapper/cl_leanote-home   32G  269M   31G    1% /home
/dev/sda1                   1014M  240M  775M   24% /boot
tmpfs                        180M  1.2M  178M    1% /run/user/42
tmpfs                        180M   28K  179M    1% /run/user/1000
tmpfs                        180M     0  180M    0% /run/user/0
192.168.64.129 | CHANGED | rc=0 >>
20210507星期五 17:35:43 CST
檔案系統                     容量  已用  可用 已用% 掛載點
devtmpfs                     1.8G     0  1.8G    0% /dev
tmpfs                        1.9G   12K  1.9G    1% /dev/shm
tmpfs                        1.9G   13M  1.8G    1% /run
tmpfs                        1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/mapper/cl_leanote-root   64G  8.5G   56G   14% /
/dev/mapper/cl_leanote-home   32G  269M   31G    1% /home
/dev/sda1                   1014M  240M  775M   24% /boot
tmpfs                        371M  1.2M  370M    1% /run/user/42
tmpfs                        371M     0  371M    0% /run/user/0

13 setup 模組

  該模組主要用於收集資訊,是通過呼叫facts元件來實現的。
  facts元件是Ansible用於採集被管機器裝置資訊的一個功能,我們可以使用setup模組查機器的所有facts資訊,可以使用filter來檢視指定資訊。整個facts資訊被包裝在一個JSON格式的資料結構中,ansible_facts是最上層的值。
  facts就是變數,內建變數 。每個主機的各種資訊,cpu顆數、記憶體大小等。會存在facts中的某個變數中。呼叫後返回很多對應主機的資訊,在後面的操作中可以根據不同的資訊來做不同的操作。如redhat系列用yum安裝,而debian系列用apt來安裝軟體。

13.1檢視資訊

我們可以直接用命令獲取到變數的值,具體我們來看看例子:
[root@jenkins ~]# ansible all -m setup -a 'filter="*mem*"'  #檢視記憶體資訊
192.168.64.128 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 128,
        "ansible_memory_mb": {
            "nocache": {
                "free": 514,
                "used": 1276
            },
            "real": {
                "free": 128,
                "total": 1790,
                "used": 1662
            },
            "swap": {
                "cached": 6,
                "free": 3836,
                "total": 3887,
                "used": 51
            }
        },
        "ansible_memtotal_mb": 1790,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.64.129 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 117,
        "ansible_memory_mb": {
            "nocache": {
                "free": 536,
                "used": 3173
            },
            "real": {
                "free": 117,
                "total": 3709,
                "used": 3592
            },
            "swap": {
                "cached": 77,
                "free": 3375,
                "total": 3887,
                "used": 512
            }
        },
        "ansible_memtotal_mb": 3709,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

13.2儲存資訊

  我們的setup模組還有一個很好用的功能就是可以儲存我們所篩選的資訊至我們的主機上,同時,檔名為我們被管制的主機的IP,這樣方便我們知道是哪臺機器出的問題。
我們可以看一看例子:
[root@jenkins ~]# ansible all -m setup -a 'filter="*mem*"' --tree /tmp/facts
192.168.64.129 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 111,
        "ansible_memory_mb": {
            "nocache": {
                "free": 537,
                "used": 3172
            },
            "real": {
                "free": 111,
                "total": 3709,
                "used": 3598
            },
            "swap": {
                "cached": 77,
                "free": 3375,
                "total": 3887,
                "used": 512
            }
        },
        "ansible_memtotal_mb": 3709,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.64.128 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 125,
        "ansible_memory_mb": {
            "nocache": {
                "free": 515,
                "used": 1275
            },
            "real": {
                "free": 125,
                "total": 1790,
                "used": 1665
            },
            "swap": {
                "cached": 6,
                "free": 3836,
                "total": 3887,
                "used": 51
            }
        },
        "ansible_memtotal_mb": 1790,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
然後我們去檢視一下:
[root@jenkins ~]# ls /tmp/facts/
192.168.64.128  192.168.64.129
[root@jenkins ~]# cat /tmp/facts/192.168.64.129
{"ansible_facts": {"ansible_memfree_mb": 111, "ansible_memory_mb": {"nocache": {"free": 537, "used": 3172}, "real": {"free": 111, "tot