Scheduling Interval Configuration
Configuring the Scheduling Interval
The cron
module in Ansible provides several parameters to configure the scheduling interval for a cron job. These parameters allow you to specify the exact time and frequency at which the job should run.
Minute
The minute
parameter allows you to specify the minute of the hour when the cron job should run. You can use a single value (e.g., minute: "30"
) or a list of values (e.g., minute: ["0", "30"]
) to run the job at multiple minutes.
Hour
The hour
parameter allows you to specify the hour of the day when the cron job should run. You can use a single value (e.g., hour: "12"
) or a list of values (e.g., hour: ["0", "12"]
) to run the job at multiple hours.
Day
The day
parameter allows you to specify the day of the month when the cron job should run. You can use a single value (e.g., day: "1"
) or a list of values (e.g., day: ["1", "15"]
) to run the job on multiple days.
Month
The month
parameter allows you to specify the month of the year when the cron job should run. You can use a single value (e.g., month: "1"
) or a list of values (e.g., month: ["1", "7"]
) to run the job in multiple months.
Weekday
The weekday
parameter allows you to specify the day of the week when the cron job should run. You can use a single value (e.g., weekday: "0"
) or a list of values (e.g., weekday: ["0", "6"]
) to run the job on multiple days of the week.
Here's an example Ansible playbook that demonstrates how to configure the scheduling interval for a cron job:
- hosts: all
tasks:
- name: Schedule a cron job to run every 30 minutes
cron:
name: "Run a backup script"
minute: "0,30"
job: "/opt/scripts/backup.sh"
- name: Schedule a cron job to run at 2 AM and 2 PM every day
cron:
name: "Run a system update"
hour: ["2", "14"]
job: "/opt/scripts/system_update.sh"
- name: Schedule a cron job to run on the 1st and 15th of every month
cron:
name: "Generate monthly report"
day: ["1", "15"]
job: "/opt/scripts/generate_report.sh"
By using these scheduling interval parameters, you can create Ansible cron jobs that run at the desired frequency, ensuring that your critical tasks are executed on time and your infrastructure remains reliable and efficient.