選項:node
chdir 在運行命令以前,先切換到指定目錄:mysql
[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 指定文件(目錄)名,若是文件存在,就不執行命令。ios
[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 後面指定一個文件(目錄)名,若是指定的文件(目錄)不存在,則不運行命令。nginx
[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
在遠程主機上運行本地腳本web
[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!" ] }
在遠程節點上執行命令,也能夠執行一個shell腳本,可是該腳本必須在遠程節點上存在。sql
chdir 、creates、removes、command模塊的參數同樣。shell
[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
複製本地文件到遠程路徑下。vim
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
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
將遠程主機中的文件拷貝到本機中,和copy模塊剛好相反。而且在保存的時候使用在主機名下的形式來進行保存。
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
檢索文件或者文件系統的狀態。
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 } }
指定文件的行,使用正則的後向引用替換某一行內容
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" }
將控制節點的模板文件作變量替換後,傳到遠程節點。
管理cron.d和crontab計劃任務
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
遠程管理服務模塊,支持這幾種服務模式: BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart
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" }
獲取遠程主機的facts信息。該模塊會自動調用playbooks獲取遠程節點的信息
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", .....
使用yum包管理器管理軟件包
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": [