23fwerw 发表于 2016-4-22 09:48:53

ansible远程执行脚本

首先谢谢创建一个shell脚本
vim /tmp/test.sh //加入内容
#!/bin/bash
echo `date`>/tmp/ansible_test.txt
然后把该脚本分发到各个机器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mod=0755"
最后是批量执行该shell脚本
ansible testhost -m shell -a “/tmp/test.sh”
shell模块,还支持远程执行命令并且带管道
ansible testhost-m shell -a "cat/etc/passwd|wc-l"

1
2
3
4
5
6
7
# vim /tmp/test.sh   编写一个脚本
#!/bin/bash
d=`date`
echo $d > /tmp/an_test.txt


把脚本分发到指定机器,权限是755





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# ansible web10.gz.com -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
web10.gz.com | SUCCESS => {
    "changed": true,
    "checksum": "fdf0a328dc5c042f9221e43bbc516213abb07031",
    "dest": "/tmp/test.sh",
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/test.sh",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 49,
    "state": "file",
    "uid": 0
}


批量执行脚本





1
2
# ansible web10.gz.com -m shell -a "/tmp/test.sh"
web10.gz.com | SUCCESS | rc=0 >>






页: [1]
查看完整版本: ansible远程执行脚本