ansible常用模塊介紹
1. command modules
選項:
-
chdir 在運行命令之前,先切換到指定目錄:
[root@web1 ~]# ansible web -m command -a "ls -l chdir=/tmp" 192.168.1.21 | SUCCESS | rc=0 >> total 168 drwx------ 2 root root 4096 Sep 20 09:33 ansible_UaZm9Y -rw-------. 1 root root 95 Jul 26 04:15 crontab.O7izOx drwxr-xr-x 3 root root 4096 Aug 12 21:09 pear -rw-------. 1 root root 0 Jul 26 03:49 yum.log -rw------- 1 root root 67800 Jul 27 21:35 yum_save_tx-2017-07-27-21-35n2gP71.yumtx -rw-rw-r-- 1 zabbix zabbix 81445 Aug 12 14:42 zabbix_agentd.log -rw-rw-r-- 1 zabbix zabbix 5 Aug 12 08:37 zabbix_agentd.pid 192.168.1.22 | SUCCESS | rc=0 >> total 64 drwx------ 2 root root 4096 Sep 20 09:33 ansible__iQSmn -rw-------. 1 root root 95 Jul 26 04:15 crontab.O7izOx srwxrwxrwx 1 mysql mysql 0 Sep 20 08:38 mysql.sock drwxr-xr-x 3 root root 4096 Aug 13 10:42 pear -rw-r--r-- 1 root root 0 Sep 20 09:30 test -rw-------. 1 root root 0 Jul 26 03:49 yum.log -rw-rw-r-- 1 zabbix zabbix 48406 Aug 12 14:41 zabbix_agentd.log
-
creates 指定文件(目錄)名,如果文件存在,就不執行命令。
[root@web1 ~]# ansible web -m command -a "ls -l /tmp creates=/tmp/test" 192.168.1.21 | SUCCESS | rc=0 >> total 168 drwx------ 2 root root 4096 Sep 20 09:37 ansible_PO0TDu -rw-------. 1 root root 95 Jul 26 04:15 crontab.O7izOx drwxr-xr-x 3 root root 4096 Aug 12 21:09 pear -rw-------. 1 root root 0 Jul 26 03:49 yum.log -rw------- 1 root root 67800 Jul 27 21:35 yum_save_tx-2017-07-27-21-35n2gP71.yumtx -rw-rw-r-- 1 zabbix zabbix 81445 Aug 12 14:42 zabbix_agentd.log -rw-rw-r-- 1 zabbix zabbix 5 Aug 12 08:37 zabbix_agentd.pid 192.168.1.22 | SUCCESS | rc=0 >> skipped, since /tmp/test exists
-
removes 後面指定一個文件(目錄)名,如果指定的文件(目錄)不存在,則不運行命令。
[root@web1 ~]# ansible web -m command -a "ls -l /tmp removes=/tmp/test" 192.168.1.21 | SUCCESS | rc=0 >> skipped, since /tmp/test does not exist 192.168.1.22 | SUCCESS | rc=0 >> total 64 drwx------ 2 root root 4096 Sep 20 09:38 ansible_PC7a8Y -rw-------. 1 root root 95 Jul 26 04:15 crontab.O7izOx srwxrwxrwx 1 mysql mysql 0 Sep 20 08:38 mysql.sock drwxr-xr-x 3 root root 4096 Aug 13 10:42 pear -rw-r--r-- 1 root root 0 Sep 20 09:30 test -rw-------. 1 root root 0 Jul 26 03:49 yum.log -rw-rw-r-- 1 zabbix zabbix 48406 Aug 12 14:41 zabbix_agentd.log
2. script modules
在遠程主機上運行本地腳本
[root@web1 shell.sh]# vim test.sh #!/bin/bash echo "hello world!" ~ [root@web1 shell.sh]# ansible web -m script -a "test.sh" 192.168.1.21 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.21 closed.\r\n", "stdout": "hello world!\r\n", "stdout_lines": [ "hello world!" ] } 192.168.1.22 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.22 closed.\r\n", "stdout": "hello world!\r\n", "stdout_lines": [ "hello world!" ] }
creates和removes參數和command模塊的這兩個參數類似
[root@web1 shell.sh]# ansible web -m script -a "/root/shell.sh/test.sh creates=/tmp/test"
192.168.1.22 | SKIPPED
192.168.1.21 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.1.21 closed.\r\n",
"stdout": "hello world!\r\n",
"stdout_lines": [
"hello world!"
]
}
[root@web1 shell.sh]# ansible web -m script -a "/root/shell.sh/test.sh removes=/tmp/test"
192.168.1.21 | SKIPPED
192.168.1.22 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.1.22 closed.\r\n",
"stdout": "hello world!\r\n",
"stdout_lines": [
"hello world!"
]
}
3. shell modules
在遠程節點上執行命令,也可以執行一個shell腳本,但是該腳本必須在遠程節點上存在。
chdir 、creates、removes、command模塊的參數一樣。
[root@web1 shell.sh]# ansible web -m shell -a "echo $HOME"
192.168.1.21 | SUCCESS | rc=0 >>
/root
192.168.1.22 | SUCCESS | rc=0 >>
/root
[root@web1 shell.sh]# ansible web -m shell -a "echo $HOME removes=/tmp/test"
192.168.1.21 | SUCCESS | rc=0 >>
skipped, since /tmp/test does not exist
192.168.1.22 | SUCCESS | rc=0 >>
/root
[root@web1 shell.sh]# ansible web -m shell -a "/root/shell.sh/test.sh removes=/tmp/test"
192.168.1.21 | SUCCESS | rc=0 >>
skipped, since /tmp/test does not exist
#執行失敗是因爲遠程主機上,並沒有這個腳本。
192.168.1.22 | FAILED | rc=127 >>
/bin/sh: /root/shell.sh/test.sh: No such file or directory
4.copy modules
複製本地文件到遠程路徑下。
- src 本地文件的絕對路徑,或者相對路徑。如果是個路徑會遞歸複製,路徑如果是/結尾,只複製目錄裏面的內容,如果不是以/結尾,會複製目錄本身和裏面的內容。
- dest 必選參數,爲目標文件指定遠程節點上的一個絕對路徑,如果src是一個目錄,那麼該參數也必須指定一個目錄。
-
backup 可選參數,如果源文件改變。就爲目標文件創建一個備份文件,給備份文件添加一個時間戳信息,值爲yes或者no,默認爲no。
[root@web1 shell.sh]# ansible web -m copy -a "src=test.sh dest=/root/" 192.168.1.21 | SUCCESS => { "changed": true, "checksum": "f3f7435d0a20eb859ff4b97bfb67c594fa71cf8c", "dest": "/root/test.sh", "gid": 0, "group": "root", "md5sum": "e9f6c05023d61dba208370895b7ebf87", "mode": "0644", "owner": "root", "size": 32, "src": "/root/.ansible/tmp/ansible-tmp-1505874564.52-157402683765598/source", "state": "file", "uid": 0 } 192.168.1.22 | SUCCESS => { "changed": true, "checksum": "f3f7435d0a20eb859ff4b97bfb67c594fa71cf8c", "dest": "/root/test.sh", "gid": 0, "group": "root", "md5sum": "e9f6c05023d61dba208370895b7ebf87", "mode": "0644", "owner": "root", "size": 32, "src": "/root/.ansible/tmp/ansible-tmp-1505874564.65-69782187734750/source", "state": "file", "uid": 0 } #在爲目標文件創建一個備份文件。 [root@web1 shell.sh]# echo "hello" >>test.sh [root@web1 shell.sh]# ansible web -m copy -a "src=test.sh dest=/root/shell/ backup=yes" 192.168.1.22 | SUCCESS => { "backup_file": "/root/shell/test.sh.12903.2017-09-20@10:46:50~", "changed": true, "checksum": "cb613f058ae5f2a3e326da2a3343cbfbdd14e62d", "dest": "/root/shell/test.sh", "gid": 0, "group": "root", "md5sum": "e68983f2b04c89ead4afb061ad1313b0", "mode": "0644", "owner": "root", "size": 38, "src": "/root/.ansible/tmp/ansible-tmp-1505875610.03-205491280550072/source", "state": "file", "uid": 0 } 192.168.1.21 | SUCCESS => { "backup_file": "/root/shell/test.sh.2627.2017-09-20@10:46:50~", "changed": true, "checksum": "cb613f058ae5f2a3e326da2a3343cbfbdd14e62d", "dest": "/root/shell/test.sh", "gid": 0, "group": "root", "md5sum": "e68983f2b04c89ead4afb061ad1313b0", "mode": "0644", "owner": "root", "size": 38, "src": "/root/.ansible/tmp/ansible-tmp-1505875610.11-210337191627097/source", "state": "file", "uid": 0 }
-
directory_mode 當遞歸複製的時候,爲所創建的目錄設置權限。如果沒有指定則使用系統默認的權限。該參數隻影響新創建的目錄,不會影響已經存在的目錄。
[root@web1 ~]# ansible web -m copy -a "src=/root/shell.sh/ dest=/root/shell.sh/ directory_mode=0777" 192.168.1.21 | SUCCESS => { "changed": true, "dest": "/root/shell.sh/", "src": "/root/shell.sh" } [root@localhost ~]# ll -d shell.sh drwxrwxrwx 2 root root 4096 Sep 20 14:33 shell.sh
- force 該參數默認值爲yes,當遠程文件與本地文件內容不一致時,會替換遠程文件。遠程文件不存在時,會傳輸文件。
- group 設置遠程文件或者目錄的所屬組
- mode 設置文件或者目錄的權限
-
owner 設置文件或者目錄的所有者
[root@web1 ~]# ansible web -m copy -a "src=/root/shell.sh/ dest=/root/shell.sh/ group=jiajie" 192.168.1.21 | SUCCESS => { "changed": true, "dest": "/root/shell.sh/", "src": "/root/shell.sh" } [root@localhost shell.sh]# ll total 16 -rw-r--r-- 1 root jiajie 3897 Sep 20 14:33 config_install_lampV2.sh -rw-r--r-- 1 root jiajie 139 Sep 20 14:33 ipvsadm.save -rw-r--r-- 1 root jiajie 577 Sep 20 14:33 ssh_scp.sh -rw-r--r-- 1 root jiajie 38 Sep 20 14:33 test.sh # ansible web -m copy -a "src=/root/shell.sh/ dest=/root/shell.sh/ group=jiajie mode=0755" 192.168.1.21 | SUCCESS => { "changed": true, "dest": "/root/shell.sh/", "src": "/root/shell.sh" } [root@localhost shell.sh]# ll total 16 -rwxr-xr-x 1 root jiajie 3897 Sep 20 14:33 config_install_lampV2.sh -rwxr-xr-x 1 root jiajie 139 Sep 20 14:33 ipvsadm.save -rwxr-xr-x 1 root jiajie 577 Sep 20 14:33 ssh_scp.sh -rwxr-xr-x 1 root jiajie 38 Sep 20 14:33 test.sh [root@web1 ~]# ansible web -m copy -a "src=/root/shell.sh/ dest=/root/shell.sh/ owner=jiajie group=jiajie mode=0755" [root@localhost shell.sh]# ll total 16 -rwxr-xr-x 1 jiajie jiajie 3897 Sep 20 14:33 config_install_lampV2.sh -rwxr-xr-x 1 jiajie jiajie 139 Sep 20 14:33 ipvsadm.save -rwxr-xr-x 1 jiajie jiajie 577 Sep 20 14:33 ssh_scp.sh -rwxr-xr-x 1 jiajie jiajie 38 Sep 20 14:33 test.sh
5.fetch modules
將遠程主機中的文件拷貝到本機中,和copy模塊恰好相反。並且在保存的時候使用在主機名下的形式來進行保存。
- dest 用來存放文件的目錄。
- src 指定拉取遠程文件的名字,只能爲文件,不能是目錄。
- fail_on_missing 如果設置爲yes,當源文件不存在的時候,將會失敗。
- flat 允許覆蓋默認行爲從hostname/path到/file的,如果dest以/結尾,它將使用源文件的基礎名稱
-
validate_checksum 當文件fetch之後進行md5檢查
[root@web1 ~]# ansible web -m command -a 'ls -l /tmp' 192.168.1.21 | SUCCESS | rc=0 >> total 168 drwx------ 2 root root 4096 Sep 20 15:13 ansible_jDhFlL -rw-------. 1 root root 95 Jul 26 04:15 crontab.O7izOx drwxr-xr-x 3 root root 4096 Aug 12 21:09 pear -rw-------. 1 root root 0 Jul 26 03:49 yum.log -rw------- 1 root root 67800 Jul 27 21:35 yum_save_tx-2017-07-27-21-35n2gP71.yumtx -rw-rw-r-- 1 zabbix zabbix 81445 Aug 12 14:42 zabbix_agentd.log -rw-rw-r-- 1 zabbix zabbix 5 Aug 12 08:37 zabbix_agentd.pid [root@web1 ~]# ansible web -m command -a 'ls -l /tmp' 192.168.1.21 | SUCCESS | rc=0 >> total 168 drwx------ 2 root root 4096 Sep 20 15:13 ansible_jDhFlL -rw-------. 1 root root 95 Jul 26 04:15 crontab.O7izOx drwxr-xr-x 3 root root 4096 Aug 12 21:09 pear -rw-------. 1 root root 0 Jul 26 03:49 yum.log -rw------- 1 root root 67800 Jul 27 21:35 yum_save_tx-2017-07-27-21-35n2gP71.yumtx -rw-rw-r-- 1 zabbix zabbix 81445 Aug 12 14:42 zabbix_agentd.log -rw-rw-r-- 1 zabbix zabbix 5 Aug 12 08:37 zabbix_agentd.pid [root@web1 ~]# ansible web -m fetch -a "dest=/root/test src=/tmp/yum.log" 192.168.1.21 | SUCCESS => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/root/test/192.168.1.21/tmp/yum.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null } [root@web1 ~]# tree test test └── 192.168.1.21 └── tmp └── yum.log 2 directories, 1 file
6.stat modules
檢索文件或者文件系統的狀態。
- path 指明文件的路徑
-
follow 無默認值。是否獲取鏈接所指向源文件的信息。默認情況下,當path是個符號鏈接的時候,只獲取符號鏈接本身的信。
[root@web1 tmp]# ansible web -m stat -a "path=/tmp/yum.log" 192.168.1.21 | SUCCESS => { "changed": false, "stat": { "atime": 1505891687.684082, "attr_flags": "e", "attributes": [ "extents" ], "block_size": 4096, "blocks": 0, "charset": "binary", "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "ctime": 1501012146.7690001, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 391682, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "md5": "d41d8cd98f00b204e9800998ecf8427e", "mimetype": "application/x-empty", "mode": "0600", "mtime": 1501012146.7690001, "nlink": 1, "path": "/tmp/yum.log", "pw_name": "root", "readable": true, "rgrp": false, "roth": false, "rusr": true, "size": 0, "uid": 0, "version": "18446744073615032431", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false } }
7. lineinfile modules
指定文件的行,使用正則的後向引用替換某一行內容
- backup 默認爲no,是否在修改之前爲目標文件做一個備份
- state 默認爲present,指定的行是否存在
- create 默認爲no,當state=present時,指定該參數爲yes表示當文件不存在時自動創建。默認情況下當文件不存在時返回錯
- dest 要操作的目標文件路徑。
- line 需要指定state=present,將新的一行插入文件或替換其中的某行內容。如果指定了backrefs=yes,則可以使用正則表達式的後向引用功能。
- regexp 正則表達式, 若state=present,則爲替換匹配到的行,只替換最後一次匹配到的行.若state=absent,會刪除匹配到的行
- backrefs 默認爲no,是否使用正則表達式的後向引用。
- insertafter 默認爲EOF, state=present,將會在最後一次匹配到內容的後邊插入新的指定內容。EOF表示在文件默認插入一個新行。 如果正則表達式沒有匹配到任何內容則效果和使用EOF一樣, 表示在文件默認加入新行。
-
insertbefore 默認是不啓用該選項,需要state=present,在最後一次匹配到的行之前添加新行。BOF表示在文件開頭新加一行, 如果正則表達式沒有匹配到任何內容則在文件開頭新加一行
#追加行HOST=127.0.0.1 [root@web1 tmp]# ansible web -m lineinfile -a "dest=/root/test.txt line=HOST=127.0.0.1" 192.168.1.21 | SUCCESS => { "backup": "", "changed": true, "msg": "line added" } #將HOST=127.0.0.1替換成HOST=192.168.1.1 [root@web1 tmp]# ansible web -m lineinfile -a "dest=/root/test.txt regexp=^HOST line=HOST=192.168.1.1" 192.168.1.21 | SUCCESS => { "backup": "", "changed": true, "msg": "line replaced" } #刪除HOST=192.168.1.1 [root@web1 tmp]# ansible web -m lineinfile -a "dest=/root/test.txt line=HOST=192.168.1.1 state=absent" 192.168.1.21 | SUCCESS => { "backup": "", "changed": true, "found": 1, "msg": "1 line(s) removed" } #在匹配到的HOST行後面加一行。如果沒有匹配到就自動追加一行在最後 [root@web1 tmp]# ansible web -m lineinfile -a "dest=/root/test.txt line=web_nginx insertafter=^HOST" 192.168.1.21 | SUCCESS => { "backup": "", "changed": true, "msg": "line added" }
8.template modules
將控制節點的模板文件做變量替換後,傳到遠程節點。
- backup 默認爲no,是否備份目標文件
- dest 要操作的文件在遠程節點上的路徑
- force 默認爲yes,當目標文件與源文件內容不一樣的時候,目標文件會被替換。若改爲no,該文件只能不存在的時候被傳輸一次
- src 模板文件的路徑, 可以是絕對路徑也可以是相對路徑。
9.cron modules
管理cron.d和crontab計劃任務
- backup 默認不啓用, 在修改之前是否將原來的計劃任務備份。
- cron_file 默認不啓用, 如果指定, 將使用指定的文件代替用戶的crontab.該文件應該都在cron.d目錄下。
- day 默認爲*,日(1-31,,/2)
- hour 默認爲*,時 (0-23,,/2)
- minute 默認爲*,分(0-59,,/2)
- month 默認爲*,月(1-12,,/2)
- weekday 默認爲,周(0-6,)
- job 計劃任務要執行的命令
- user 要管理哪個用戶的計劃任務
- state 默認爲present,當爲absent時, 可以按照計劃任務name刪除指定條目
-
special_time 默認不啓用。 可以指定某個特殊時間運行的任務。reboot重啓的時候執行,yearly每年執行一次,annually每月執行一次,monthly每個月執行一次,weekly每週執行一次,daily每年執行一次,hourly每小時執行一次
[root@web1 tmp]# ansible web -m cron -a "name='test crom' hour=3,5 job='ls -l >>/dev/null' user=jiajie" 192.168.1.21 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "test crom" ] } [root@localhost ~]# cat /var/spool/cron/jiajie #Ansible: test crom * 3,5 * * * ls -l >>/dev/null #刪除計劃任務 [root@web1 tmp]# ansible web -m cron -a "name='test crom' user=jiajie state=absent" 192.168.1.21 | SUCCESS => { "changed": true, "envs": [], "jobs": [] } #重啓執行計劃任務 [root@web1 tmp]# ansible web -m cron -a "name='test crom' user=jiajie special_time=reboot job='ls -l'" 192.168.1.21 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "test crom" ] } [root@localhost tmp]# cat /var/spool/cron/jiajie #Ansible: test crom @reboot ls -l
10.service modules
遠程管理服務模塊,支持這幾種服務模式: BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart
- enabled 服務是否開機啓動
- must_exist 默認爲True,避免當某個服務不存在的時候, 模塊執行終止。
- name 服務名稱。
- runlevel 默認值是服務本身的默認設置。 該服務的運行級別。
- state 對服務要執行的操作, started,stopped,restarted,reloaded
-
args 需要給命令提供的附加參數
#開啓防火牆 [root@web1 tmp]# ansible web -m service -a "name=iptables state=started" 192.168.1.21 | SUCCESS => { "changed": true, "name": "iptables", "state": "started" } #設置開機自啓動 [root@web1 tmp]# ansible web -m service -a "name=iptables enabled=yes" 192.168.1.21 | SUCCESS => { "changed": true, "enabled": true, "name": "iptables" }
11.setup modules
獲取遠程主機的facts信息。該模塊會自動調用playbooks獲取遠程節點的信息
- fact_path fact文件路徑, 默認在/etc/ansible/facts.d, 利用該文件可以自定義本節點的facts信息, 默認是不存在的。
-
filter 過濾返回的facts信息
[root@web1 tmp]# ansible web -m setup 192.168.1.21 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.1.21" ], "ansible_all_ipv6_addresses": [ "fe80::20c:29ff:fe39:730e" ], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "07/02/2015", "ansible_bios_version": "6.00", "ansible_cmdline": { "KEYBOARDTYPE": "pc", .....
12.yum modules
使用yum包管理器管理軟件包
- name 軟件包的名稱, 同時也可以指定包的版本,如name-1.0。當指定state=latest,該選項指定參數*,這表示要更新所有的軟件包, 和運行命令yum -y update作用一致。該選項也可以指定一個rpm包的url鏈接,或者一個本地的rpm路徑。如果需要指定多個軟件包以','隔開即可。
-
state 指定對軟件包的操作行爲,刪除或者安裝。默認爲present表示安裝包。其他可以指定的值,present,latest都是安裝包, absent表示卸載包。update_cache 在state=present或state=latest是否強制更新緩存。
[root@web1 tmp]# ansible web -m yum -a "name=tree" 192.168.1.21 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "tree-1.5.3-3.el6.x86_64 providing tree is already installed" ] } [root@web1 tmp]# ansible web -m yum -a "name=tree state=absent" 192.168.1.21 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [