cron 模块
用来管理 crontab 计划任务
- 允许新建,更新和删除
- 管理必须包含关键字 “#Ansible: [name]”
- name 必须唯一
常用参数
参数名称 | 参数说明 | 默认值 |
---|---|---|
name | 计划任务的名称 | null |
minute | 运行的分钟数(0-59、、/2) | * |
hour | 小时 (0-23) | * |
day | 日(1-31) | * |
month | 月 | * |
weekday | 周(0-6) | * |
job | 需要执行的命令 | null |
user | 指定用户 | root |
backup | 是否备份 | yes |
示例
计划任务不存在时会新增任务
存在时会修改任务,前提时 name 值需一样
- 每周2周5 0点运行 ls -alh > /dev/null
- name: Ensure a job that runs at 2 and 5 exists.
cron:
name: "check dirs"
minute: "0"
hour: "5,2"
job: "ls -alh > /dev/null"
- 删除 “#Ansible:an old job” 的作业
- name: 'Removes any job that is prefixed by "#Ansible: an old job" from the crontab'
cron:
name: "an old job"
state: absent
backup: yes
当 backup 值为 yes 时
- 从返回信息或者ansible日志文件中的关键子 backup_file 获取备份文件
[root@demosrv001 ~]# ansible all -m cron \
> -a 'name="crontab test" user="root" minute="00" hour="15" \
> weekday="03" job="echo test" backup=yes'
testsrv001 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/tmp/crontabeOurDZ",
"changed": true,
"envs": [],
"jobs": [
"crontab test"
]
}