Ansible Cron Module: Manage Cron Jobs on Remote Systems
The Ansible cron module is used to automate the management of cron jobs on remote systems. A cron job is a scheduled task that runs at specific intervals defined by the cron daemon. The cron module allows you to manage these tasks programmatically within your Ansible playbooks.
For example, if you need to schedule a script to run daily at midnight to back up a MySQL database, the cron module allows you to define the schedule and the command directly in a playbook, ensuring consistent and repeatable configuration across all servers. This eliminates the need for manual cron entry updates, making it ideal for managing large-scale systems or ensuring compliance with backup policies.
In this article, we'll explore how to use the cron module to create, modify, and delete scheduled tasks in cron.
Basic Syntax of the Ansible Cron Module
The cron module provides a wide range of options to define cron jobs. Below is the basic structure of a task using the module:
- name: Manage a cron job
ansible.builtin.cron:
name: "<Job_Name>"
user: "<Username>"
minute: "<Minute>"
hour: "<Hour>"
day: "<Day>"
month: "<Month>"
weekday: "<Weekday>"
job: "<Command_to_execute>"
state: "<present|absent>"
Important Parameters:
name
: A unique identifier for the cron job. This helps Ansible track and manage the job.user
: Specifies the user for whom the job is created.minute
,hour
,day
,month
,weekday
: Define the schedule using standard cron syntax.job
: The command or script to be executed.state
: Determines if the job should be present or absent.
Now, let's dive a deep to use the cron module in real-world scenarios.
Adding a New Cron Job
Adding a cron job involves specifying the task name, schedule, and the command to execute. Let’s add a cron job that performs a daily backup at 2 AM.
- name: Add a daily backup cron job
hosts: all
tasks:
- name: Schedule daily backup
ansible.builtin.cron:
name: "Daily Backup"
user: "root"
minute: "0"
hour: "2"
job: "/usr/bin/backup.sh"
state: present
This setup is ideal for automating system backups. For instance, if your organization requires nightly database dumps, this cron job can ensure backups are performed consistently.
Removing a Cron Job
Removing outdated or unnecessary cron jobs is essential to keep the system organized and avoid redundant tasks. Here's how to remove a cron job:
- name: Remove a daily backup cron job
hosts: all
tasks:
- name: Remove backup job
ansible.builtin.cron:
name: "Daily Backup"
user: "root"
state: absent
In this playbook:
state: absent
: Ensures the specified cron job is removed from the crontab.
You can also include the Ansible’s debug module to verify removal:
- name: Verify removal
debug:
msg: "The cron job 'Daily Backup' has been removed."
Modifying an Existing Cron Job
Schedules often need adjustments due to changes in operational requirements. Let’s modify the daily backup job to run at 3 AM instead of 2 AM.
- name: Modify backup cron job
hosts: all
tasks:
- name: Change backup schedule to 3 AM
ansible.builtin.cron:
name: "Daily Backup"
user: "root"
minute: "0"
hour: "3"
job: "/usr/bin/backup.sh"
state: present
In this playbook:
hour: "3"
: Adjusts the execution time from 2 AM to 3 AM.state: present
: Ensures the modified cron job exists.
Creating a Cron Job with Special Timing
Sometimes tasks need to run at more frequent intervals, such as every 15 minutes. Here’s how to schedule such a job:
- name: Add a job to run every 15 minutes
hosts: all
tasks:
- name: Schedule a monitoring script
ansible.builtin.cron:
name: "System Monitoring"
user: "monitor"
minute: "*/15"
job: "/usr/bin/monitor.sh"
state: present
In this playbook:
minute: "*/15"
: Cron syntax to run the job every 15 minutes.job: "/usr/bin/monitor.sh"
: Executes a monitoring script.
Disabling a Cron Job
Instead of removing a cron job, you might want to disable it temporarily. Here’s how:
- name: Disable a cron job
hosts: all
tasks:
- name: Disable the monitoring job
ansible.builtin.cron:
name: "System Monitoring"
user: "monitor"
job: "/usr/bin/monitor.sh"
disabled: yes
In this playbook:
disabled: yes
: Comments out the cron job in the crontab file without removing it. This ensures the job remains in the system for future reactivation.
Conclusion
The Ansible cron module is an essential tool for automating and managing scheduled tasks in Linux environments. By leveraging its idempotent nature and flexible syntax, you can easily create, update, and remove cron jobs across multiple servers.