Bootstrap FreeKB - Ansible - Update a users cron table
Ansible - Update a users cron table

Updated:   |  Ansible articles

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.

---
- hosts: all
  tasks:
  - 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

 


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

---
- hosts: all
  tasks:
  - 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. The valid values for special_time are:

  • hourly
  • daily
  • weekly
  • monthly
  • annualy
  • reboot

 

---
- hosts: all
  tasks:
  - name: example.sh after 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 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.

---
- hosts: all
  tasks:
  - 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

 


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.

---
- hosts: all
  tasks:
  - 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

 


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.

---
- hosts: all
  tasks:
  - 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
...

 


By default, cron will send an email each time cron is invoked. cronvar can be used to define the target email.

---
- hosts: all
  tasks:
  - name: send email to john.doe@example.com when cron is invoked
    cronvar:
      name: MAILTO
      value: john.doe@example.com
      user: john.doe
...

 

Or value can be empty to not send an email when cron is invoked.

---
- hosts: all
  tasks:
  - name: do not send email when cron is invoked
    cronvar:
      name: MAILTO
      value: 
      user: john.doe
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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