上一篇Ansible_自動化運維之《Ansible之初識-1》文章主要介紹了ansible的安裝和主機組的配置,本篇Ansible_自動化運維之《Ansible之命令-2》將重點介紹 Ansible的ad-hoc臨時命令的使用。web
說明:本篇Ansible的ad-hoc臨時命令使用,將會使用部分模塊,例如[copy]、[ping]、[shell]、[command]等,可能會不便於理解,文章儘可能詳細說明。若是想盡快了解ansible的模塊功能,請閱讀下一篇《Ansible之模塊-3》。shell
Ansible中有一個很重要的功能就是能夠執行ad-hoc命令,它表示即時、臨時的意思,即表示一次性的命令。與之相對的是ansible playbook功能,playbook適用於批量部署環境,通常不用常常改動。而ad-hoc命令,利用ansible的模塊功能,適用於業務變動、臨時檢查和維護等操做場景,好比批量推送一個配置文件,重啓某個服務,安裝一些包等等。centos
ansible命令行執行格式通常爲:網絡
ansible [hosts] –m [module] –a [parameters]
其中:
[hosts] 指須要運行或者執行的主機或者主機組
[module] 指運行時須要使用的模塊
[parameters] 指模塊後跟的參數設置
例如:app
1 #指定主機IP 2 #ansible 192.168.100.2 –m copy –a "src=/tmp/test.txt dest=/tmp/test1.txt"
3 #此命令是將ansible控制機上的 /tmp/test.txt 拷貝到 主機192.168.100.2的/tmp/目錄下,且名稱是test1.txt。
#說明:
#-m 後面跟使用的模塊 copy;
#-a 後面跟所使用的參數,src 表示源路徑,dest表示目標主機路徑
4 #指定主機組 5 #ansible webserver –m copy –a "src=/tmp/test.txt dest=/tmp/test2.txt"
6 #此命令是將ansible控制機上的 /tmp/test.txt 拷貝到 主機組webserver的/tmp/目錄下,且名稱是test2.txt
# 測試主機172.16.12.241是否網絡連通,可使用以下命令測試:
1 [root@ansible ~]# ansible 172.16.12.241 -m ping 2 172.16.12.241 | SUCCESS => { 3 "changed": false, 4 "ping": "pong" 5 } 6 [root@ansible ~]
1 [root@ansible ~]# ansible all -m shell -a "uptime" 2 172.16.12.241 | SUCCESS | rc=0 >> 3 21:54:13 up 1:52, 2 users, load average: 0.00, 0.01, 0.05 4 5 172.16.12.243 | SUCCESS | rc=0 >> 6 21:54:12 up 1:52, 1 user, load average: 0.00, 0.01, 0.01 7 8 172.16.12.242 | SUCCESS | rc=0 >> 9 21:54:12 up 1:52, 1 user, load average: 0.00, 0.01, 0.02 10 11 [root@ansible ~]#
說明: -m shell表示使用shell模塊;運維
1 [root@ansible ~]# ansible all -m shell -a "df -h /" 2 172.16.12.241 | SUCCESS | rc=0 >> 3 Filesystem Size Used Avail Use% Mounted on 4 /dev/mapper/centos_wjh--centos7-root 48G 3.2G 45G 7% / 5 6 172.16.12.242 | SUCCESS | rc=0 >> 7 Filesystem Size Used Avail Use% Mounted on 8 /dev/mapper/centos_wjh--centos7-root 48G 2.6G 45G 6% / 9 10 172.16.12.243 | SUCCESS | rc=0 >> 11 Filesystem Size Used Avail Use% Mounted on 12 /dev/mapper/centos_wjh--centos7-root 48G 2.6G 45G 6% / 13 14 [root@ansible ~]#
1 [root@ansible ~]# ansible all -m shell -a "ls -l /etc/chrony.conf" 2 172.16.12.241 | SUCCESS | rc=0 >> 3 -rw-r--r--. 1 root root 1161 Feb 13 03:28 /etc/chrony.conf 4 5 172.16.12.243 | SUCCESS | rc=0 >> 6 -rw-r--r--. 1 root root 25 Feb 13 03:28 /etc/chrony.conf 7 8 172.16.12.242 | SUCCESS | rc=0 >> 9 -rw-r--r--. 1 root root 25 Feb 13 03:28 /etc/chrony.conf 10 11 [root@ansible ~]#
1 [root@ansible ~]# ansible 172.16.12.241 -m copy -a "src=test.conf dest=/etc/ttt.conf mode=0600 " 2 172.16.12.241 | SUCCESS => { 3 "changed": true, 4 "checksum": "dff19e48593efc79207494d625ddc4a22769ec2d", 5 "dest": "/etc/ttt.conf", 6 "gid": 0, 7 "group": "root", 8 "md5sum": "d0bab2e27173ed4bc5195da29d21920f", 9 "mode": "0600", 10 "owner": "root", 11 "size": 1062, 12 "src": "/root/.ansible/tmp/ansible-tmp-1487041285.77-71823315308297/source", 13 "state": "file", 14 "uid": 0 15 } 16 [root@ansible ~]#
1 [root@ansible ~]# ansible 172.16.12.241 -m command -a "ls -l /etc/ttt.conf" 2 172.16.12.241 | SUCCESS | rc=0 >> 3 -rw------- 1 root root 1062 Feb 13 22:01 /etc/ttt.conf 4 5 [root@ansible ~]#
#說明:
#command與shell模塊使用方法幾乎相同。
由此看見文件確實是推送成功,而且權限和文件名均已修改爲功。測試
總結:
本篇Ansible_自動化運維之《Ansible之命令-2》文章主要紹了Ansible的ad-hoc臨時命令的使用。可能因爲並未深刻了解ansible的模塊功能,因此部分命令內容不便於理解,下一篇 Ansible_自動化運維之《Ansible之模塊-3》將重點介紹 Ansible的經常使用模塊的使用,敬請關注。ui