Automating repetitive tasks is one of the essential steps in large-scale environments. The Ansible cron module simplifies this by allowing administrators to manage cron jobs on remote systems. Cron jobs are scheduled tasks that run periodically based on time intervals. Using Ansible, you can easily create, modify, or remove cron jobs on multiple systems without logging into each server.
This guide will explore the Ansible cron module in-depth, including practical examples.
Table of Contents
What is the Ansible Cron Module?
The cron module in Ansible is used to manage the crontab entries of users. It allows you to:
- Add new cron jobs
- Remove existing cron jobs
- Disable or enable specific jobs
- Specify commands, times, and additional options
The cron module is a part of Ansible’s core modules, making it readily available for most installations.
Basic Syntax
The basic syntax of the Ansible cron module is as follows:
- name: Manage a cron job
ansible.builtin.cron:
name: "Job Name"
minute: "*"
hour: "*"
day: "*"
month: "*"
weekday: "*"
job: "/path/to/command"
state: "present"
Example 1. Adding a Cron Job
This example demonstrates how to add a cron job to run a script daily at midnight.
- name: Add a cron job to run a backup script
hosts: all
tasks:
- name: Add a cron job for daily backups
ansible.builtin.cron:
name: "Daily Backup Job"
minute: "0"
hour: "0"
job: "/usr/local/bin/backup.sh"
state: present
This playbook adds a daily cron job on all hosts to run a backup script at midnight.
Example 2. Removing a Cron Job
To delete an existing cron job, create a playbook with the following content:
- name: Remove a specific cron job
hosts: all
tasks:
- name: Remove the daily backup job
ansible.builtin.cron:
name: "Daily Backup Job"
state: absent
This will remove a specific cron job named “Daily Backup Job” from all hosts.
3. Updating an Existing Cron Job
You can update a cron job by specifying the same name and modifying its properties.
- name: Update an existing cron job
hosts: all
tasks:
- name: Change the time for the backup job
ansible.builtin.cron:
name: "Daily Backup Job"
minute: "30"
hour: "1"
job: "/usr/local/bin/backup.sh"
state: present
The above example updates the “Daily Backup Job” cron job on all hosts to run at 1:30 AM.
Example 4. Adding Cron Jobs with Environment Variables
You can add environment variables to a cron job.
- name: Add a cron job with environment variables
hosts: all
tasks:
- name: Add a job with environment variables
ansible.builtin.cron:
name: "Job with Environment Variables"
minute: "0"
hour: "2"
job: "/usr/local/bin/report.sh"
env:
PATH: "/usr/local/bin:/usr/bin:/bin"
LANG: "en_US.UTF-8"
state: present
This playbook adds a cron job named “Job with Environment Variables” on all hosts to run at 2:00 AM, including custom environment variables like PATH and LANG.
Example 5. Disabling a Cron Job
Sometimes, you may want to disable a cron job temporarily.
- name: Disable a specific cron job
hosts: all
tasks:
- name: Disable the daily backup job
ansible.builtin.cron:
name: "Daily Backup Job"
state: disabled
You can use state: disabled to disable the “Daily Backup Job” cron job on all hosts without removing it.
Example 6. Adding a Cron Job for a Specific User
You can specify a different user for the cron job.
- name: Add a cron job for a specific user
hosts: all
tasks:
- name: Add a cron job for user 'john'
ansible.builtin.cron:
name: "User-Specific Job"
minute: "15"
hour: "3"
job: "/home/john/scripts/cleanup.sh"
user: "john"
state: present
This playbook adds a cron job named “User-Specific Job” for the user “john” to run a cleanup script at 3:15 AM on all hosts.
Example 7. Managing Multiple Cron Jobs in a Playbook
You can manage multiple cron jobs in a single playbook.
- name: Manage multiple cron jobs
hosts: all
tasks:
- name: Add a daily cleanup job
ansible.builtin.cron:
name: "Daily Cleanup"
minute: "0"
hour: "4"
job: "/usr/local/bin/cleanup.sh"
state: present
- name: Add a weekly report generation job
ansible.builtin.cron:
name: "Weekly Report"
minute: "30"
hour: "5"
weekday: "7"
job: "/usr/local/bin/report.sh"
state: present
This playbook adds two cron jobs on all hosts: a daily cleanup job at 4:00 AM and a weekly report generation job at 5:30 AM on Sundays.
Conclusion
The Ansible cron module is a powerful tool for managing scheduled tasks across multiple systems. With its flexibility and ease of use, it ensures consistency and reduces manual effort in cron job management.
You can explore the debug module to validate cron job tasks and the when statement to add conditional logic for dynamic scheduling.
FAQs
1. How do I verify if a cron job was successfully created?
Use the cron command on the managed node or the debug module in Ansible to check the task output.
2. What happens if I set overlapping cron jobs with the same name?
Ansible will overwrite the existing job with the new parameters, as jobs are identified by their name.
3. How do I create a cron job that runs multiple commands?
Use ; or && in the job parameter to include multiple commands in a single cron job.