上一篇Ansible_自动化运维之《Ansible之初识-1》文章主要介绍了ansible的安装和主机组的配置,本篇Ansible_自动化运维之《Ansible之命令-2》将重点介绍 Ansible的ad-hoc临时命令的使用。
说明:本篇Ansible的ad-hoc临时命令使用,将会使用部分模块,例如[copy]、[ping]、[shell]、[command]等,可能会不便于理解,文章尽量详细说明。如果想尽快了解ansible的模块功能,请阅读下一篇《Ansible之模块-3》。
- 1.Ansible命令
- 1.1 Ad-hoc说明
Ansible中有一个很重要的功能就是可以执行ad-hoc命令,它表示即时、临时的意思,即表示一次性的命令。与之相对的是ansible playbook功能,playbook适用于批量部署环境,一般不用经常改动。而ad-hoc命令,利用ansible的模块功能,适用于业务变更、临时检查和维护等操作场景,比如批量推送一个配置文件,重启某个服务,安装一些包等等。
-
- 1.2 命令结构
ansible命令行执行格式一般为:
ansible [hosts] –m [module] –a [parameters]
其中:
[hosts] 指需要运行或者执行的主机或者主机组
[module] 指运行时需要使用的模块
[parameters] 指模块后跟的参数设置
例如: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
- 1.2 命令结构
-
- 1.3 执行测试
# 测试主机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.3 执行测试
- 2.Ansible AD-HOC 临时命令的使用
- 2.1 查看主机运行时间
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模块;
- -a 表示使用的命令或者参数,shell模块所使用的是命令,查看运行时间,所以使用"uptime"。
- -a 表示使用的命令或者参数,shell模块所使用的是命令,查看运行时间,所以使用"uptime"。
- 2.2 查看主机 / 分区使用情况
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 ~]#
- 2.3 查看主机组 /etc/chrony.conf配置文件的权限及详细信息
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 ~]#
- 2.4 推送test.conf 配置文件到 172.16.12.241 的 /etc/目录下,并修改文件名为 ttt.conf ,权限为600
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 ~]#
- 2.5 验证ttt.conf文件
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模块使用方法几乎相同。由此看见文件确实是推送成功,并且权限和文件名均已修改成功。
- 2.1 查看主机运行时间
总结:
本篇Ansible_自动化运维之《Ansible之命令-2》文章主要绍了Ansible的ad-hoc临时命令的使用。可能由于并未深入了解ansible的模块功能,所以部分命令内容不便于理解,下一篇 Ansible_自动化运维之《Ansible之模块-3》将重点介绍 Ansible的常用模块的使用,敬请关注。