1.5.11:cron 模块
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/cron_module.html#ansible-collections-ansible-builtin-cron-module
cron模块用于管理目标主机上的crontab计划任务。
1.5.11.1:常用参数
参数 | 说明 |
---|---|
minute | 0-59,默认为*。 |
hour | 0-23,默认为*。 |
day | 1-31,默认为*。 |
month | 1-12,默认为*。 |
weekday | 指定星期几,0/1/2/3/4/5/6代表星期天/一/二/三/四/五/六; 默认为*。 |
job | 指定需要执行的命令。(别名为value) |
name | contab任务的名称。 |
state | absent|present,移除还是创建,默认为present创建。 |
user | 为哪个用户设置计划任务,如不指定,则为当前用户。 |
special_time | 指定特殊时间,可用选项有: annually 每年 daily 每天 hourly 每小时 monthly 每月 reboot 重启时 weekly 每周 yearly 每年 |
disabled | yes|no,是否禁用指定的crontab任务,默认为no(只有在state=present时才有意义) |
env | yes|no,是否是在设置crontab环境变量,默认为no。 如果设为yes,那么name和value参数的值分别代表环境变量的名称和值。 |
1.5.11.2:示例
为websrvs组添加每5分钟向ntp服务器同步一次时间的计划任务:
[root@ansible ~]# ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &> /dev/null' name=ntp_sync"
root@node111:~# crontab -l
#Ansible: ntp_sync
*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null
禁用计划任务:
[root@ansible ~]# ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &> /dev/null' name=ntp_sync disabled=yes"
#计划任务被注释
root@node111:~# crontab -l
#Ansible: ntp_sync
#*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null
指定计划任务名称,移除计划任务:
[root@ansible ~]# ansible websrvs -m cron -a "name=ntp_sync state=absent"