Ansible Pilot

Schedule a Cron Job task in Linux - Ansible module cron

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to automate the schedule of command execution to a specific minute, hour, day, month, weekday, and user in Linux using cron service.

I’m going to show you a live demo 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

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

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.

demo

Ansible schedule a Cron Job task in Linux.

code

---
- name: cron module demo
  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 demo] ***************************************************************************
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
[[email protected] ~]$ crontab -l
no crontab for devops
[[email protected] ~]$

after execution

$ ssh [email protected]
Last login: Wed Dec  8 15:30:58 2021 from 192.168.0.101
[[email protected] ~]$ crontab -l
#Ansible: test
*/2 * * * * logger "ansible-pilot"
[[email protected] ~]$ 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
[[email protected] ~]$ 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

Recap

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

Academy

Learn the Ansible automation technology with some real-life examples in my

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

Access the Complete Book and Learn Quick Ansible by 200+ Examples

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

December 23, 2021

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress