[admin@node1 ansible]$ ansible-doc -s cron
- name: Manage cron.d and crontab entries.
cron:
backup: # If set, create a backup of the crontab before it is modified. The location of the backup is returned in the `backup_file' variable by this module.
cron_file: # If specified, uses this file instead of an individual user's crontab. If this is a relative path, it is interpreted with respect to /etc/cron.d. (If it is absolute, it will
typically be /etc/crontab). Many linux distros expect (and some require) the filename portion to consist solely of upper- and lower-case letters,
digits, underscores, and hyphens. To use the `cron_file' parameter you must specify the `user' as well.
day: # Day of the month the job should run ( 1-31, *, */2, etc )
disabled: # If the job should be disabled (commented out) in the crontab. Only has effect if state=present
env: # If set, manages a crontab's environment variable. New variables are added on top of crontab. "name" and "value" parameters are the name and the value of environment variable.
hour: # Hour when the job should run ( 0-23, *, */2, etc )
insertafter: # Used with `state=present' and `env'. If specified, the environment variable will be inserted after the declaration of specified environment variable.
insertbefore: # Used with `state=present' and `env'. If specified, the environment variable will be inserted before the declaration of specified environment variable.
job: # The command to execute or, if env is set, the value of environment variable. The command should not contain line breaks. Required if state=present.
minute: # Minute when the job should run ( 0-59, *, */2, etc )
month: # Month of the year the job should run ( 1-12, *, */2, etc )
name: # Description of a crontab entry or, if env is set, the name of environment variable. Required if state=absent. Note that if name is not set and state=present, then a new crontab
entry will always be created, regardless of existing ones.
reboot: # If the job should be run at reboot. This option is deprecated. Users should use special_time.
special_time: # Special time specification nickname.
state: # Whether to ensure the job or environment variable is present or absent.
user: # The specific user whose crontab should be modified.
weekday: # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
案例
~~~
[admin@node1 ansible]$ ansible webserver -m cron -a "name='sync time from ntpserver' minute='*/5' job='/usr/sbin/ntpdate time1.aliyun.com &>/dev/null' state='present'" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.131 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time from ntpserver"
]
}
192.168.20.132 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time from ntpserver"
]
}
~~~
默認這個定時任務,不加user,默認是root用戶的,我們然后查看,顯示如下
~~~
[admin@node1 ansible]$ ansible webserver -a "crontab -l" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.131 | SUCCESS | rc=0 >>
#Ansible: sync time from ntpserver
*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
192.168.20.132 | SUCCESS | rc=0 >>
#Ansible: sync time from ntpserver
*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
~~~
第二:創建普通用戶的定時任務
~~~
[admin@node1 ansible]$ ansible webserver -m cron -a "name='sync time from ntpserver' minute='*/5' job='/usr/sbin/ntpdate time1.aliyun.com &>/dev/null' state='present' user=admin" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.131 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time from ntpserver"
]
}
192.168.20.132 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time from ntpserver"
]
}
[admin@node1 ansible]$ ansible webserver -a "crontab -l"
192.168.20.131 | SUCCESS | rc=0 >>
#Ansible: sync time from ntpserver
*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
192.168.20.132 | SUCCESS | rc=0 >>
#Ansible: sync time from ntpserver
*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
~~~
刪除任務
~~~
[admin@node1 ansible]$ ansible webserver -m cron -a "name='sync time from ntpserver' state='absent'" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.131 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
192.168.20.132 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[admin@node1 ansible]$ ansible webserver -a "crontab -l" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.131 | SUCCESS | rc=0 >>
192.168.20.132 | SUCCESS | rc=0 >>
~~~
- 第一章:Ansible基礎入門
- 第二章:Ansible系列手冊
- 第一節:Ansible系列之主機清單
- 第二節:Ansible系列之變量
- 第三節:Ansible系列之YAML
- 第四節:Ansible系列之條件判斷
- 第五節:Ansible系列之循環
- 第六節: Ansible系列之tags
- 第七節:Ansible系列之Jinja2
- 第三章:Ansible系列之模塊
- 第一節:user模塊
- 第二節:group模塊
- 第三節:cron模塊
- 第四節:copy模塊
- 第五節: file模塊
- 第六節:yum模塊
- 第七節:service模塊
- 第八節:shell模塊
- 第九節:script模塊
- 第十節:setup模塊
- 第十一節:filesystem和mount模塊
- 第十二節:synchronize模塊
- 第十三節: get_url模塊
- 第十四節: package模塊
- 第十五節:stat模塊
- 第十六節:unarchive模塊
- 第十七節: commang模塊
- 第四章:Ansible-playbook介紹
- 第五章:Ansible系統環境
- 第一節:Ansible Role 系統環境之epel設置