rgfdgdf 发表于 2017-3-7 13:08:02

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

一 远程执行脚本
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文件
实例:



1
2
3
4
5
6
7
# 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"   ##删除任务计划
例子

1
2
3
4
5
#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


页: [1]
查看完整版本: ansible 远程执行脚本和执行任务计划