Ansible - cron module

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The cron module is used to manage a users cron table. This is based off of the crontab command.

On some systems (such as Fedora), cron is not included with the install of the operating system. In this scenario, the dnf module can be used to install cronie, and the systemd module can be used to start and enable crond.


Create a users cron table entry

When the state parameter is not used, or when the state parameter is set to present (state: present), if the user's cron table does not contain the job, the job will be created. On the other hand, if the user's cron table contains the job, the job will not be created in the user's cron table.

- name: "ensure cron contains the following: 00 01 * * * /etc/cron.d/example.sh"
  cron:
    name: Invoke example.sh at 1:00 am
    user: root
    job: /etc/cron.d/example.sh
    hour: '01'
    minute: '00'
    state: present

 

In this example, the user's cron table would have the following.

#Ansible: Invoke example.sh at 1:00 am
00 01 * * * /etc/cron.d/example.sh

 


Shared cron table entry

The cron_file option can be used to create a shared cron table entry.

- name: "ensure /etc/cron.d/foo contains the following: 00 01 * * * /etc/cron.d/example.sh"
  cron:
    name: Invoke example.sh at 1:00 am
    user: john.doe
    cron_file: /etc/cron.d/foo
    job: /etc/cron.d/example.sh
    hour: '01'
    minute: '00'
    state: present

 

Notice in this example that john.doe is the user that invokes example.sh in the shared /etc/cron.d/foo file.

#Ansible: Invoke example.sh at 1:00 am
00 01 * * * john.doe /etc/cron.d/example.sh

 


Hourly, Daily, Weekly, Monthly, Annualy, Reboot

The special_time option can be used to run a command or script once every hour, once every day, once every week, once every month, or when the server is restarted. In this example, the example.sh script will be run after a system reboot.

- name: example.sh reboot
  cron:
    name: Invoke example.sh after reboot
    special_time: reboot
    job: /etc/cron.d/example.sh

 

The cron table should have something like this.

@reboot /etc/cron.d/example.sh

 


Variables

Variables can be defined in the cron table. In this example, the FOO variable contains a value of bar. In this example, bar would be appended to out.txt once every minute.

- name: Create the "FOO=bar" variable
  cron:
    env: yes
    name: FOO
    job: bar

- name: append bar to out.txt once every minute
  cron:
    name: append bar to out.txt once every minute
    user: root
    job: echo $FOO >> out.txt
    state: present

 

The cron table should then have the following.

FOO="bar"
#Ansible: append bar to out.txt once every minute
* * * * * echo $FOO >> out.txt

 


Disable (but don't delete) a cron table entry

When state: absent and disabled: yes are used, if the user's cron table contains the job, the job will be commented out from the user's cron table.

- name: "ensure cron does not contains the following: 00 01 * * * /etc/cron.d/example.sh"
  cron:
    name: Invoke example.sh at 1:00 am
    user: root
    job: /etc/cron.d/example.sh
    hour: '01'
    minute: '00'
    state: absent
    disabled: yes

 

In this example, the user's cron table would have the following.

#Ansible: Invoke example.sh at 1:00 am
#00 01 * * * root /etc/cron.d/example.sh

 


Delete a cron table entry

When state: absent is used and the disabled parameter is not used, if the user's cron table contains the job, the job will be removed from the user's cron table.

- name: "ensure cron does not contains the following: 00 01 * * * /etc/cron.d/example.com"
  cron:
    name: Invoke example.sh at 1:00 am
    user: root
    job: /etc/cron.d/example.sh
    hour: '01'
    minute: '00'
    state: absent

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 8f02f in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |