Managing the scheduled tasks across multiple different servers that can quickly become tedious and prone to errors. This is where the Ansible cron module plays its part. It allows you to automate the creation, modification, and removal of cron jobs using multiple simple YAML playbooks.
This guide will walk you through everything that you need to know about Ansible cron modules, from the syntax to real world use cases.
Why Use Ansible for Cron Job Management?
Even though Linux provides similar cron jobs with the crontab and is quite effective for scheduled tasks, it does become difficult to scale when working across multi-server environments. Ansible cron Module can help out with the following:
- Automate cron job deployment across multiple hosts.
- Maintain consistency in job configuration with the version-controlled playbooks.
- Easily modify the jobs or remove them without logging into each server individually.
- Apply the changes based on the environment to ensure that tasks are only changed if absolutely necessary.
- Audit and document the scheduled tasks into YAML format.
With Ansible, cron job management becomes repeatable, scalable, and part of your infrastructure-as-code workflow.
Prerequisites for Using Ansible cron Module
Before using the Ansible cron module, ensure the following setup:
- Make sure that you have Ansible installed on your control node (pip install ansible).
- SSH communication to the remorse servers with Ansible.
- Most Linux distributions already have Python and cron installed, make sure that yours does too.
- Define the hosts that you will manage in the Ansible inventory.
- Also make sure that you have root or sudo privileges.
Related Article: Discovering Cron Jobs in Linux: A Guide to Finding Scheduled Tasks
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
Basic Syntax and Parameters
The Ansible cron module uses key-value pairs to define cron jobs declaratively. Here’s the basic structure:
– name: Create a scheduled task
ansible.builtin.cron:
name: “job name”
job: “/path/to/command”
minute: “0”
hour: “2”
day: “*”
month: “*”
weekday: “*”

user: root
Common Parameters:
- name (required): A unique label for the cron job (used for identification).
- job (required): The command to execute.
- minute, hour, day, month, weekday: Time schedule fields.
- user: The user under whom the cron job runs (default is the current user).
- state: Use present (default) to add/modify a job or absent to remove it.
- disabled: Set to yes to disable a job without deleting it.
- env, variable, value: For adding environment variables.
Creating a New cron Job with Ansible
Here’s an example of how to create a basic cron job using Ansible:
– name: Schedule daily backup at 2 AM
ansible.builtin.cron:
name: “Daily Backup”
minute: “0”
hour: “2”
job: “/usr/local/bin/backup.sh”
user: root
This playbook will add a cron job that will run the backup.sh script everyday at 2AM as a Root user. Once it is deployed, it will automatically insert into the correct user crontab.
You can run this playbook with:
ansible-playbook cron-backup.yml -i inventory.ini
Modifying an Existing cron Job
To update an Ansible cron job, reuse the same name value and change into the required parameters. Ansible will then identify the jobs and filter them based on the name field, so it is super simple to update the job schedule.
Example: Change the backup time to 3:30 AM
– name: Update backup cron job time
ansible.builtin.cron:
name: “Daily Backup”
job: “/usr/local/bin/backup.sh”
minute: “30”
hour: “3”
user: root
This updates the existing cron job without creating a duplicate.
Removing a cron Job Using Ansible
To delete or to get rid of an Ansible cron module, set the state parameter to absent. This will remove the job from the specified user’s crontab.
Example: Remove an old cleanup task
– name: Remove legacy cleanup job
ansible.builtin.cron:
name: “Old Cleanup Task”
state: absent
Make sure the name matches the one originally assigned to the job, otherwise Ansible won’t find and remove it.
Disabling and Enabling cron Jobs
Sometimes you might need to disable a cron job for a specific time without actually removing or deleting it. You can use the disabled parameter within Ansible to do this.
Example: Disable a job
– name: Disable log rotation cron job
ansible.builtin.cron:
name: “Log Rotate”
job: “/usr/local/bin/logrotate.sh”
disabled: yes
To re-enable the job, set disabled: no or remove the disabled line entirely.
Using Environment Variables in cron
Ansible cron module allows you to set environment variables in the user’s crontab with env, variable, and value parameters.
Example: Set the MAILTO variable
– name: Set MAILTO for cron notifications
ansible.builtin.cron:
env: yes
variable: “MAILTO”
value: “[email protected]”
This ensures that cron job outputs (stdout/stderr) are sent to the specified email address.
Error Handling and Idempotency in Ansible cron Modules
Aspect | Behavior |
Idempotency | Ensures playbooks can run multiple times without duplicating cron jobs. |
Job Detection | Matches cron jobs using the name field; updates only if values differ. |
No-Change Safety | If no change is required, Ansible skips the task and logs it as “ok.” |
Safe Deletion | Removing a non-existent job does not produce an error. |
Automatic Recovery | If a managed node temporarily fails, rerunning the playbook applies missing jobs only. |
Real-World Use Cases To Use an Ansible cron Module
Here are a few of the best and most practical Ansible cron Job scenarios:
- Applications where you need to schedule automated backups for files across multiple servers.
- Run cleanup scripts to remove or archive any logs on a regular basis.
- Run health check scripts and trigger the alerts based on the results.
- Schedule secure file syncs between staging and production stages.
- Send custom email alerts via cron-triggered scripts.
These use cases highlight how automation improves reliability and reduces manual overhead in system administration.
Conclusion – Ansible Cron Module
The Ansible cron module is one of the most powerful tools for automation of scheduled tasks across the Linux systems. By understanding the power, you can create consistency, traceability, and scalability in your systems.
FAQs
How do I create a cron job with Ansible?
Use the ansible.builtin.cron
module in your playbook and define parameters like name
, job
, minute
, hour
, and user
. Ansible ensures the job is created idempotently.
Does Ansible ensure idempotency when managing cron jobs?
Yes. Ansible applies changes only when needed and avoids duplicating or reapplying identical cron jobs, making it safe to run playbooks multiple times.
Can I use environment variables in Ansible cron jobs?
Yes, by setting env: yes
along with variable
and value
, you can define environment variables like MAILTO
or PATH
directly in the crontab.