|
上一篇Ansible_自动化运维之《Ansible之初识-1》文章主要介绍了ansible的安装和主机组的配置,本篇Ansible_自动化运维之《Ansible之命令-2》将重点介绍 Ansible的ad-hoc临时命令的使用。
说明:本篇Ansible的ad-hoc临时命令使用,将会使用部分模块,例如[copy]、[ping]、[shell]、[command]等,可能会不便于理解,文章尽量详细说明。如果想尽快了解ansible的模块功能,请阅读下一篇《Ansible之模块-3》。
Ansible中有一个很重要的功能就是可以执行ad-hoc命令,它表示即时、临时的意思,即表示一次性的命令。与之相对的是ansible playbook功能,playbook适用于批量部署环境,一般不用经常改动。而ad-hoc命令,利用ansible的模块功能,适用于业务变更、临时检查和维护等操作场景,比如批量推送一个配置文件,重启某个服务,安装一些包等等。
- 1.3 执行测试
[iyunv@ansible ~]# ansible 172.16.12.241 -m ping
172.16.12.241 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[iyunv@ansible ~]
- 2.Ansible AD-HOC 临时命令的使用
- 2.1 查看主机运行时间
[iyunv@ansible ~]# ansible all -m shell -a "uptime"
172.16.12.241 | SUCCESS | rc=0 >>
21:54:13 up 1:52, 2 users, load average: 0.00, 0.01, 0.05
172.16.12.243 | SUCCESS | rc=0 >>
21:54:12 up 1:52, 1 user, load average: 0.00, 0.01, 0.01
172.16.12.242 | SUCCESS | rc=0 >>
21:54:12 up 1:52, 1 user, load average: 0.00, 0.01, 0.02
[iyunv@ansible ~]#
说明: -m shell表示使用shell模块;
- -a 表示使用的命令或者参数,shell模块所使用的是命令,查看运行时间,所以使用"uptime"。
- 2.2 查看主机 / 分区使用情况
[iyunv@ansible ~]# ansible all -m shell -a "df -h /"
172.16.12.241 | SUCCESS | rc=0 >>
Filesystem >
/dev/mapper/centos_wjh--centos7-root 48G 3.2G 45G 7% /
172.16.12.242 | SUCCESS | rc=0 >>
Filesystem >
/dev/mapper/centos_wjh--centos7-root 48G 2.6G 45G 6% /
172.16.12.243 | SUCCESS | rc=0 >>
Filesystem >
/dev/mapper/centos_wjh--centos7-root 48G 2.6G 45G 6% /
[iyunv@ansible ~]#
- 2.3 查看主机组 /etc/chrony.conf配置文件的权限及详细信息
[iyunv@ansible ~]# ansible all -m shell -a "ls -l /etc/chrony.conf"
172.16.12.241 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 1161 Feb 13 03:28 /etc/chrony.conf
172.16.12.243 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 25 Feb 13 03:28 /etc/chrony.conf
172.16.12.242 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 25 Feb 13 03:28 /etc/chrony.conf
[iyunv@ansible ~]#
- 2.4 推送test.conf 配置文件到 172.16.12.241 的 /etc/目录下,并修改文件名为 ttt.conf ,权限为600
[iyunv@ansible ~]# ansible 172.16.12.241 -m copy -a "src=test.conf dest=/etc/ttt.conf mode=0600 "
172.16.12.241 | SUCCESS => {
"changed": true,
"checksum": "dff19e48593efc79207494d625ddc4a22769ec2d",
"dest": "/etc/ttt.conf",
"gid": 0,
"group": "root",
"md5sum": "d0bab2e27173ed4bc5195da29d21920f",
"mode": "0600",
"owner": "root",
"size": 1062,
"src": "/root/.ansible/tmp/ansible-tmp-1487041285.77-71823315308297/source",
"state": "file",
"uid": 0
}
[iyunv@ansible ~]#
- 2.5 验证ttt.conf文件
[iyunv@ansible ~]# ansible 172.16.12.241 -m command -a "ls -l /etc/ttt.conf"
172.16.12.241 | SUCCESS | rc=0 >>
-rw------- 1 root root 1062 Feb 13 22:01 /etc/ttt.conf
[iyunv@ansible ~]#
#说明:
#command与shell模块使用方法几乎相同。
由此看见文件确实是推送成功,并且权限和文件名均已修改成功。
总结:
本篇Ansible_自动化运维之《Ansible之命令-2》文章主要绍了Ansible的ad-hoc临时命令的使用。可能由于并未深入了解ansible的模块功能,所以部分命令内容不便于理解,下一篇 Ansible_自动化运维之《Ansible之模块-3》将重点介绍 Ansible的常用模块的使用,敬请关注。 |
|
|