1、首先创建一个shell脚本
[[email protected] ~]# vim /tmp/test.sh
#!/bin/bash
echo `date`>/tmp/ansible_test.txt
2、把该脚本分发到各个机器上
[[email protected] ~]# ansible testhosts -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
101.200.148.30 | SUCCESS => {
"changed": true,
"checksum": "e48cedf296d4e8e3db8c2d388b383936338eccb6",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "03b4ef60a7dbea69a1e7eefcf6075d0b",
"mode": "0755",
"owner": "root",
"size": 46,
"src": "/root/.ansible/tmp/ansible-tmp-1468616471.15-89974521316155/source",
"state": "file",
"uid": 0
}
127.0.0.1 | SUCCESS => {
"changed": true,
"checksum": "e48cedf296d4e8e3db8c2d388b383936338eccb6",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test.sh",
"size": 46,
"state": "file",
"uid": 0
}
3、批量执行脚本
[[email protected] ~]# ansible testhosts -m shell -a "/tmp/test.sh"
101.200.148.30 | SUCCESS | rc=0 >>
127.0.0.1 | SUCCESS | rc=0 >>
4、查看有没有执行成功
[[email protected] ~]# ls -lt /tmp/
total 20
-rw-r--r-- 1 root root 29 Jul 16 05:03 ansible_test.txt
-rwxr-xr-x 1 root root 46 Jul 16 05:01 test.sh
drwxr-xr-x 3 root root 4096 Jul 16 04:45 456
drwxr-xr-x 3 root root 4096 Jul 16 04:44 ansibletestowner=root
-rw-r--r-- 1 root root 1099 Jul 16 04:26 123
srwxrwxrwx 1 root root 0 Jul 15 23:23 qtsingleapp-aegisG-46d2
srwxrwxrwx 1 root root 0 Jul 15 23:23 Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
srwxrwxrwx 1 root root 0 Apr 21 21:08 qtsingleapp-aegiss-a5d2
[[email protected] ~]# cat /tmp/ansible_test.txt
Sat Jul 16 05:03:24 CST 2016