您的位置:首页 > 其它

ansible 远程执行脚本和执行任务计划

2017-03-06 14:43 323 查看
一 远程执行脚本
l首先创建一个shell脚本
lvim /tmp/test.sh //加入内容
l#!/bin/bash
lecho `date` > /tmp/ansible_test.txt
l然后把该脚本分发到各个机器上
ansible ip -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
l最后是批量执行该shell脚本
ansible ip -m shell -a "/tmp/test.sh"
lshell模块,还支持远程执行命令并且带管道
ansible testhost -m shell -a "cat /etc/passwd|wc -l "
最后去远程机器上面看/tmp目录下是否有test.sh这个文件即可

二 任务计划设置
1)添加任务计划
lansible ip -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6“
##每周六在/tmp/目录下面创建1212.txt文件
实例:

[root@web9 tmp]# ansible 192.168.253.131 -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6"
192.168.253.131 | success >> {
"changed": true,
"jobs": [
"test cron"
]
}
2) 删除任务计划
若要删除该cron 只需要加一个字段 state=absent
ansible ip -m cron -a "name='test cron' state=absent" ##删除任务计划
例子
[root@web9 tmp]#  ansible 192.168.253.131  -m cron -a "name='test cron' state=absent"
192.168.253.131 | success >> {
"changed": true,
"jobs": []
}
最后在客户端里面查看 crontab -l 会被删除
l其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  脚本 远程执行