检测test组服务器存活

[root@ansible_master]# ansible test -m ping

192.168.1.10 | SUCCESS => {
  "changed": false, 
  "ping": "pong"
}
192.168.1.20 | SUCCESS => {
  "changed": false, 
  "ping": "pong"
}

 

查看test组服务器uptime时间

[root@ansible_master]# ansible test -m command -a 'uptime'

192.168.1.10 | SUCCESS | rc=0 >>
17:45:39 up 6 days, 20:09, 3 users, load average: 0.00, 0.00, 0.00

192.168.1.20 | SUCCESS | rc=0 >>
17:45:40 up 6 days, 20:09, 2 users, load average: 0.00, 0.00, 0.00

 

PS:command -a 后台可跟shell命令,如'netstat -ntlp'

如命令中带有多行命令,如netstat -ntlpa | grep EST| wc -l 需要使用shell模块

[root@ansible_master]# ansible test -m shell -a "netstat -ntlpa | grep EST | wc  -l"


也可指定某个IP执行命令,多个IP用,分割 ansible 192.168.1.10,192.168.1.20  -m ping

[root@ansible_master]# ansible 192.168.1.10 -m shell -a "netstat -ntlpa | grep EST | wc  -l"

192.168.1.10 | SUCCESS | rc=0 >>

108


也可指定某个域名执行命令

[root@ansible_master]# ansible node1.ansible.com -m shell -a "netstat -ntlpa | grep EST | wc  -l"

node1.ansible.com | SUCCESS | rc=0 >>
108

参数 -i 可指定主机组文件,指行相应命令,ansible 默认调用/etc/ansible/hosts配置文件

#/root/host1.cfg

[host1]

192.169.1.10

192.168.1.20


参数-o 可将输出结果显示在一行上

#ansible test -m command -a 'uptime' -o

192.169.1.10| SUCCESS | rc=0 | (stdout) 11:49:52 up 8 days, 14:13, 1 user, load average: 0.00, 0.00, 0.00
192.169.1.20| SUCCESS | rc=0 | (stdout) 11:49:52 up 8 days, 14:13, 1 user, load average: 0.00, 0.00, 0.00

[root@ansible_master]# ansible host1 -m shell -a "uptime" -i /root/host1.cfg

192.169.1.10 | SUCCESS | rc=0 >>
17:31:34 up 7 days, 19:55, 2 users, load average: 0.00, 0.00, 0.00

192.168.1.20| SUCCESS | rc=0 >>
17:31:34 up 7 days, 19:55, 2 users, load average: 0.00, 0.00, 0.00

 

同步文件

[root@ansible_master]# ansible test -m copy -a  "src=/opt/nginx/conf/nginx.conf dest=/opt/nginx/conf/nginx.conf"

 

192.168.1.30 | SUCCESS => {
"changed": true, 
"checksum": "01fd17457d7fe153c6927865c626450e72a94b0e", 
"dest": "/opt/nginx/conf/nginx.conf", 
"gid": 0, 
"group": "root", 
"md5sum": "4471bb4ba78230405b7af749d1633226", 
"mode": "0644", 
"owner": "root", 
"size": 410, 
"src": "/root/.ansible/tmp/ansible-tmp-1463566138.59-228850836844174/source", 
"state": "file", 
"uid": 0
}


PS:src 原文件地址,dest 目录文件地址

将/opt/nginx/conf/nginx.conf 同步到 test组下面服务器/opt/nginx/conf/nginx.conf


显示 test组内主机

#ansible test --list-hosts

 hosts (2):

192.168.1.10

192.168.1.20


使用yum 模块安装软件包

#ansible test -m yum -a "name=httpd state=lastest" -o

192.168.1.10 | success >> {"changed:false,"name":"httpd","state":"started"}

 

使用service 模块启动服务

#ansible test -m service-a "name=httpd state=started" -o