ansible.builtin.cron (v2.3.2.0-1) — module

Manage cron.d and crontab entries.

| "added in version" 0.9 of ansible.builtin"

Authors: Dane Summers (@dsummersl), Mike Grozak, Patrick Callahan, Evan Kaufman (@EvanK), Luca Berruti (@lberruti)

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.3.2.0.post1

Description

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

When crontab jobs are managed: the module includes one line with the description of the crontab entry C("#Ansible: <name>") corresponding to the "name" passed to the module, which is used by future ansible/module calls to find/check the state. The "name" parameter should be unique, and changing the "name" value will result in a new cron task being created (or a different one being removed).

When environment variables are managed: no comment line is added, but, when the module needs to find/check the state, it uses the "name" parameter to find the environment variable definition line.

When using symbols such as %, they must be properly escaped.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Ensure a job that runs at 2 and 5 exists.
# Creates an entry like "0 5,2 * * ls -alh > /dev/null"
- cron:
    name: "check dirs"
    minute: "0"
    hour: "5,2"
    job: "ls -alh > /dev/null"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Ensure an old job is no longer present. Removes any job that is prefixed
# by "#Ansible: an old job" from the crontab
- cron:
    name: "an old job"
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Creates an entry like "@reboot /some/job.sh"
- cron:
    name: "a job for reboot"
    special_time: reboot
    job: "/some/job.sh"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Creates an entry like "PATH=/opt/bin" on top of crontab
- cron:
    name: PATH
    env: yes
    value: /opt/bin
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Creates an entry like "APP_HOME=/srv/app" and insert it after PATH
# declaration
- cron:
    name: APP_HOME
    env: yes
    value: /srv/app
    insertafter: PATH
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Creates a cron file under /etc/cron.d
- cron:
    name: yum autoupdate
    weekday: 2
    minute: 0
    hour: 12
    user: root
    job: "YUMINTERACTIVE: 0 /usr/sbin/yum-autoupdate"
    cron_file: ansible_yum-autoupdate
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Removes a cron file from under /etc/cron.d
- cron:
    name: "yum autoupdate"
    cron_file: ansible_yum-autoupdate
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Removes "APP_HOME" environment variable from crontab
- cron:
    name: APP_HOME
    env: yes
    state: absent

Inputs

    
day:
    aliases:
    - dom
    default: '*'
    description:
    - Day of the month the job should run ( 1-31, *, */2, etc )
    required: false

env:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - 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.
    required: false
    version_added: '2.1'
    version_added_collection: ansible.builtin

job:
    aliases:
    - value
    default: null
    description:
    - The command to execute or, if env is set, the value of environment variable. Required
      if state=present.
    required: false

hour:
    default: '*'
    description:
    - Hour when the job should run ( 0-23, *, */2, etc )
    required: false

name:
    default: null
    description:
    - 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.
    required: false

user:
    default: root
    description:
    - The specific user whose crontab should be modified.
    required: false

month:
    default: '*'
    description:
    - Month of the year the job should run ( 1-12, *, */2, etc )
    required: false

state:
    choices:
    - present
    - absent
    default: present
    description:
    - Whether to ensure the job or environment variable is present or absent.
    required: false

backup:
    choices:
    - 'yes'
    - 'no'
    default: false
    description:
    - If set, create a backup of the crontab before it is modified. The location of the
      backup is returned in the C(backup_file) variable by this module.
    required: false

minute:
    default: '*'
    description:
    - Minute when the job should run ( 0-59, *, */2, etc )
    required: false

reboot:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - If the job should be run at reboot. This option is deprecated. Users should use
      special_time.
    required: false
    version_added: '1.0'
    version_added_collection: ansible.builtin

weekday:
    aliases:
    - dow
    default: '*'
    description:
    - Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
    required: false

disabled:
    default: false
    description:
    - If the job should be disabled (commented out) in the crontab. Only has effect if
      state=present
    required: false
    version_added: '2.0'
    version_added_collection: ansible.builtin

cron_file:
    default: null
    description:
    - 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). To use the C(cron_file) parameter you must specify
      the C(user) as well.
    required: false

insertafter:
    default: null
    description:
    - Used with C(state=present) and C(env). If specified, the environment variable will
      be inserted after the declaration of specified environment variable.
    required: false
    version_added: '2.1'
    version_added_collection: ansible.builtin

insertbefore:
    default: null
    description:
    - Used with C(state=present) and C(env). If specified, the environment variable will
      be inserted before the declaration of specified environment variable.
    required: false
    version_added: '2.1'
    version_added_collection: ansible.builtin

special_time:
    choices:
    - reboot
    - yearly
    - annually
    - monthly
    - weekly
    - daily
    - hourly
    default: null
    description:
    - Special time specification nickname.
    required: false
    version_added: '1.3'
    version_added_collection: ansible.builtin