查看cron的参数
ansible-doc -s cron
backup:对远程主机上的原计划任务做修改之前备份
cron_file:如果制定该选项,则用该文件替换原称主机上的cron.d目录下的用户计划任务
day:(`1-31', `*', `*/2', and so on)天
hour:Hour when the job should run (`0-23', `*', `*/2',and so on).
job:要执行的任务
name:计划任务描述
minute:(`0-59', `*',# `*/2', and so on).分钟取值
month: (`1-12', `* `*/2', and so on).月取值
state:计划任务状态
user:运行该计划任务的用户
disabled:是否应在 crontab 中禁用(注释掉)该作业。仅当state=present才有效。
special_time:特殊时间(reboot,weekly, yearly。。。。)
创建计划任务,没有定义,默认是*
mew@mew:/etc/ansible$ ansible all -m cron -a "job='echo hello' minute='*/10' name='test cron' state=present"
192.168.1.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"test cron"
]
}
查看计划任务
root@mew:~# crontab -l
#Ansible: test cron
*/10 * * * * echo hello
注释计划任务
disabled: # If the job should be disabled (commented out) in
the crontab. Only
has effect if
`state=present'.
仅有当 state=present时才可以禁用计划任务
$ ansible all -m cron -a "job='echo hello' minute='*/10' name='test cron' state=present disabled=true”
root@mew:~# crontab -l
#Ansible: test cron
#*/10 * * * * echo hello
添加计划任务,每天2,5小时运行一次df -h命令
ops@ops:/etc/ansible$ ansible all -m cron -a 'name="test cron module" job="df -h" hour="2,5" minute="0" state=present'
192.168.0.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"test cron module"
]
}
#每天2,5小时运行一次df -h命令
ops@ops:/etc/ansible$ ansible all -m shell -a 'crontab -l'
192.168.0.106 | CHANGED | rc=0 >>
#Ansible: test cron module
0 2,5 * * * df -h
关闭计划任务disabled=true用法
ops@ops:/etc/ansible$ ansible all -m cron -a 'name="test cron module" job="df -h" disabled=true'
192.168.0.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"test cron module"
]
}
ops@ops:/etc/ansible$ ansible all -m shell -a 'crontab -l'
192.168.0.106 | CHANGED | rc=0 >>
#Ansible: test cron module
#* * * * * df -h
state=absent删除计划任务
ops@ops:/etc/ansible$ ansible all -m cron -a 'name="test cron module" state=absent'
192.168.0.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": []
}
ops@ops:/etc/ansible$ ansible all -m shell -a 'crontab -l'
192.168.0.106 | CHANGED | rc=0 >>
cron_file的用法,在/etc/cron.d/增加一个计划任务文件,-b因为要提权,不然没权限创建
ops@ops:/etc/ansible$ ansible all -m cron -a 'name="test cron module" job="df -h" cron_file=ansible-test user=root' -b
192.168.0.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"cron_file": "ansible-test",
"envs": [],
"jobs": [
"test cron module"
]
}
ops@ops:/etc/ansible$ ansible all -m shell -a 'ls /etc/cron.d/'
192.168.0.106 | CHANGED | rc=0 >>
ansible-test
e2scrub_all
ops@ops:/etc/ansible$ ansible all -m shell -a 'cat /etc/cron.d/ansible-test'
192.168.0.106 | CHANGED | rc=0 >>
#Ansible: test cron module
* * * * * root df -h
删除cron_file创建的文件
ops@ops:/etc/ansible$ ansible all -m cron -a 'name="test cron module" job="df -h" cron_file=ansible-test state=absent' -b
192.168.0.106 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"cron_file": "ansible-test",
"state": "absent"
}
ops@ops:/etc/ansible$ ansible all -m shell -a 'ls /etc/cron.d/'
192.168.0.106 | CHANGED | rc=0 >>
e2scrub_all