一 远程执行脚本web
l首先建立一个shell脚本shell
lvim /tmp/test.sh //加入内容vim
l#!/bin/bashbash
lecho `date` > /tmp/ansible_test.txtide
l而后把该脚本分发到各个机器上spa
ansible ip -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"xml
l最后是批量执行该shell脚本crontab
ansible ip -m shell -a "/tmp/test.sh"ip
lshell模块,还支持远程执行命令而且带管道it
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