I’m going to show you a live Playbook and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible schedule a Cron Job task in Linux

  • ansible.builtin.cron
  • Manage cron.d and crontab entries

Today we’re talking about Ansible module cron. The full name is ansible.builtin.cron, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years and it works in a different variety of operating systems. It manages cron.d and crontab entries. For Windows targets, use the ansible.windows.win_scheduled_task module instead.

Parameters

  • name string - crontab name
  • state string - present/absent
  • job string - command to execute
  • user string - defaults to the current user
  • minute, hour, day, month, weekday string - ‘*’, ‘1–31’, ‘*/2’
  • special_time - annually/daily/hourly/monthly/reboot/weekly/yearly
  • cron_file - NEVER use for /etc/crontab

This module has some parameters to perform any tasks. The only required is “name”, where you specify the description of a crontab entry. The parameter “state” sets whether the cron job is present or not in the target host. The parameter “job” sets the command to execute or, if env is set, the value of the environment variable. The parameter “user” sets the specific user for the crontab, when unset, this parameter defaults to the current user. The most important part is the moment to run the crontab, specifically: “minute”, “hour”, “day”, “month”, “weekday”. In this field, you could use the star operator “*” to specify all the minutes, hours, weekdays, days, and months. You could be more specific with a single number, range, or intervals. There are also some special times already defined in the parameter “special_time”. The options are: annually, daily, hourly, monthly, reboot, weekly, yearly. Let me also highlight that we could also specify the “cron_file” if you want a specific name and not under the user.

Join 50+ hours of courses in our exclusive community

Playbook

Ansible schedule a Cron Job task in Linux.

code

  • cron.yml
---
- name: cron module Playbook
  hosts: all
  tasks:
   - name: "example cronjob"
     ansible.builtin.cron:
      name: "test"
      state: present
      minute: "*/2"
      hour: "*"
      day: "*"
      month: "*"
      weekday: "*"
      job: 'logger "ansible-pilot"'

execution

$ ansible-playbook -i virtualmachines/demo/inventory schedule\ cron\ job\ task/cron.yml
PLAY [cron module Playbook] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [example cronjob] ****************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

before execution

$ ssh [email protected]
Last login: Wed Dec  8 15:28:44 2021 from 192.168.0.101
[devops@demo ~]$ crontab -l
no crontab for devops
[devops@demo ~]$

after execution

$ ssh [email protected]
Last login: Wed Dec  8 15:30:58 2021 from 192.168.0.101
[devops@demo ~]$ crontab -l
#Ansible: test
*/2 * * * * logger "ansible-pilot"
[devops@demo ~]$ sudo tail -f /var/log/cron
Dec  8 15:26:47 rhel8 crond[905]: (CRON) INFO (Syslog will be used instead of sendmail.)
Dec  8 15:26:47 rhel8 crond[905]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 43% if used.)
Dec  8 15:26:47 rhel8 crond[905]: (CRON) INFO (running with inotify support)
Dec  8 15:26:47 rhel8 CROND[916]: (root) CMD (bash -c '/bin/updatedb ; rm --force /etc/cron.d/updatedb')
Dec  8 15:28:52 rhel8 crontab[5139]: (devops) LIST (devops)
Dec  8 15:30:10 rhel8 crontab[5191]: (devops) LIST (devops)
Dec  8 15:30:45 rhel8 crontab[5453]: (devops) LIST (devops)
Dec  8 15:30:45 rhel8 crontab[5455]: (devops) REPLACE (devops)
Dec  8 15:30:58 rhel8 crontab[5706]: (devops) LIST (devops)
Dec  8 15:31:18 rhel8 crontab[5749]: (devops) LIST (devops)
Dec  8 15:32:01 rhel8 CROND[5760]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:34:01 rhel8 CROND[5766]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:36:01 rhel8 CROND[5770]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:38:01 rhel8 CROND[5775]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:40:01 rhel8 CROND[5781]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:42:01 rhel8 CROND[5787]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:44:01 rhel8 CROND[5791]: (devops) CMD (logger "ansible-pilot")
Dec  8 15:46:01 rhel8 CROND[5796]: (devops) CMD (logger "ansible-pilot")
^C
[devops@demo ~]$ sudo tail -f /var/log/messages 
Dec  8 15:41:43 rhel8 systemd[1]: Started Cleanup of Temporary Directories.
Dec  8 15:42:01 rhel8 systemd[1]: Started Session 18 of user devops.
Dec  8 15:42:01 rhel8 devops[5787]: ansible-pilot
Dec  8 15:42:01 rhel8 systemd[1]: session-18.scope: Succeeded.
Dec  8 15:44:01 rhel8 systemd[1]: Started Session 19 of user devops.
Dec  8 15:44:01 rhel8 devops[5791]: ansible-pilot
Dec  8 15:44:01 rhel8 systemd[1]: session-19.scope: Succeeded.
Dec  8 15:46:01 rhel8 systemd[1]: Started Session 20 of user devops.
Dec  8 15:46:01 rhel8 devops[5796]: ansible-pilot
Dec  8 15:46:01 rhel8 systemd[1]: session-20.scope: Succeeded.
Dec  8 15:48:01 rhel8 systemd[1]: Started Session 21 of user devops.
Dec  8 15:48:01 rhel8 devops[5807]: ansible-pilot
Dec  8 15:48:01 rhel8 systemd[1]: session-21.scope: Succeeded.
Dec  8 15:50:01 rhel8 systemd[1]: Starting system activity accounting tool...
Dec  8 15:50:01 rhel8 systemd[1]: sysstat-collect.service: Succeeded.
Dec  8 15:50:01 rhel8 systemd[1]: Started system activity accounting tool.
Dec  8 15:50:01 rhel8 systemd[1]: Started Session 22 of user devops.
Dec  8 15:50:01 rhel8 devops[5814]: ansible-pilot
Dec  8 15:50:01 rhel8 systemd[1]: session-22.scope: Succeeded.
Dec  8 15:52:01 rhel8 systemd[1]: Started Session 23 of user devops.
Dec  8 15:52:01 rhel8 devops[5818]: ansible-pilot
Dec  8 15:52:01 rhel8 systemd[1]: session-23.scope: Succeeded.
Dec  8 15:54:01 rhel8 systemd[1]: Started Session 24 of user devops.
Dec  8 15:54:01 rhel8 devops[5823]: ansible-pilot
Dec  8 15:54:01 rhel8 systemd[1]: session-24.scope: Succeeded.
Dec  8 15:56:01 rhel8 systemd[1]: Started Session 25 of user devops.
Dec  8 15:56:01 rhel8 devops[5827]: ansible-pilot
Dec  8 15:56:01 rhel8 systemd[1]: session-25.scope: Succeeded.

code with ❤️ in GitHub

Conclusion

Now you know how to schedule a Cron Job task in Linux with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza