Ansible Scheduled Tasks with cron
The cron module is idempotent in a way that is worth understanding, because the mechanism is visible in the crontab itself: it writes a comment containing the job's name and finds the job again by that comment. Change the schedule and it updates in place. Change the name and it has no idea the old one was yours.
Automating Administration Guide 34 of 45 Beginner
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 14 min
This lab is Ubuntu 26.04 and EX294 is a RHEL exam. cron is the same on both, though the package differs - cron on Debian and Ubuntu, cronie on RHEL. cron_file writes into /etc/cron.d/ and requires user: to be set, because a system crontab has a user field that a personal one does not.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
This guide includes
Use this when a cron job must not be created twice. This matters because the cron module identifies a job by its comment - so renaming one gives you two identical jobs rather than one renamed job.
- creating a cron job, and seeing the comment that identifies it
- renaming it, and ending up with two
- writing system-wide jobs and environment variables
- disabling a job rather than deleting it
Before you start
- A control node configured as shown in the first step.
- The session creates several cron jobs on the
webhost and removes all of them.
-
The configuration this guide assumes
-
A cron job, and the comment that identifies it
-
Rename it and get two
-
System-wide jobs and environment variables
-
Disabling rather than deleting