knowledge/technology/tools/Ansible/modules/ansible.builtin.cron.md

12 KiB
Raw Blame History

ansible.builtin.cron

Use this module to manage crontab entries. This module allows you to create named crontab entries, update, or delete them.

Parameter

Parameter Type Default Description
backup boolean false 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 path - If specified, uses this file instead of an individual users crontab. The assumption is that this file is exclusively managed by the module, do not use if the file contains multiple entries, NEVER use for /etc/crontab. If this is a relative path, it is interpreted with respect to /etc/cron.d. Many linux distros expect (and some require) the filename portion to consist solely of upper- and lower-case letters, digits, underscores, and hyphens. Using this parameter requires you to specify the user as well, unless state is not present. Either this parameter or name is required
day string "*" Day of the month the job should run (1-31, *, */2, and so on).
disabled boolean false If the job should be disabled (commented out) in the crontab. Only has effect if state=present.
hour string "*" Hour when the job should run (0-23, *, */2, and so on).
job string - The command to execute.
minute string "*" Minute when the job should run (0-59, *, */2, and so on).
month string "*" Month of the year the job should run (1-12, *, */2, and so on).
name string / required - Description of a crontab entry.
special_time "annually"
"daily"
"hourly"
"monthly"
"reboot"
"weekly"
"yearly"
- Special time specification nickname.
state "absent"
"present"
"present" Whether to ensure the job is present or absent.
user string - The specific user whose crontab should be modified. When unset, this parameter defaults to the current user.
weekday string "*" Day of the week that the job should run (0-6 for Sunday-Saturday, *, and so on).

Examples

- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
  ansible.builtin.cron:
    name: "check dirs"
    minute: "0"
    hour: "5,2"
    job: "ls -alh > /dev/null"

- name: 'Ensure an old job is no longer present. Removes any job that is prefixed by "#Ansible: an old job" from the crontab'
  ansible.builtin.cron:
    name: "an old job"
    state: absent

- name: Creates an entry like "@reboot /some/job.sh"
  ansible.builtin.cron:
    name: "a job for reboot"
    special_time: reboot
    job: "/some/job.sh"

- name: Creates an entry like "PATH=/opt/bin" on top of crontab
  ansible.builtin.cron:
    name: PATH
    env: yes
    job: /opt/bin

- name: Creates an entry like "APP_HOME=/srv/app" and insert it after PATH declaration
  ansible.builtin.cron:
    name: APP_HOME
    env: yes
    job: /srv/app
    insertafter: PATH

- name: Creates a cron file under /etc/cron.d
  ansible.builtin.cron:
    name: yum autoupdate
    weekday: "2"
    minute: "0"
    hour: "12"
    user: root
    job: "YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
    cron_file: ansible_yum-autoupdate

- name: Removes a cron file from under /etc/cron.d
  ansible.builtin.cron:
    name: "yum autoupdate"
    cron_file: ansible_yum-autoupdate
    state: absent

- name: Removes "APP_HOME" environment variable from crontab
  ansible.builtin.cron:
    name: APP_HOME
    env: yes
    state: absent