Ansible Cron Jobs
Ansible's cron module allows you to manage cron jobs across multiple servers declaratively. It's ideal for infrastructure-as-code approaches to scheduled tasks.
How to Set Up Cron Jobs in Ansible
1
Basic cron job with Ansible
Create a cron job using the cron module
Code
# playbook.yml
- name: Configure cron jobs
hosts: webservers
tasks:
- name: Run backup every day at 2am
ansible.builtin.cron:
name: "daily backup"
minute: "0"
hour: "2"
job: "/opt/scripts/backup.sh"2
Cron job with special time
Use special_time for common schedules
Code
- name: Run cleanup weekly
ansible.builtin.cron:
name: "weekly cleanup"
special_time: weekly
job: "/opt/scripts/cleanup.sh"
# special_time options:
# reboot, yearly, annually, monthly,
# weekly, daily, hourly3
Manage environment variables
Set cron environment variables
Code
- name: Set cron environment
ansible.builtin.cron:
name: PATH
env: yes
value: "/usr/local/bin:/usr/bin"
- name: API sync with env vars
ansible.builtin.cron:
name: "api sync"
minute: "*/5"
job: "/opt/scripts/sync.sh"
user: deployAnsible Cron Limitations vs CronUptime
Ansible Limitations
- •Still requires server maintenance
- •Cron jobs tied to server uptime
- •No centralized monitoring across servers
- •Debugging failures requires SSH access
- •Managing across many servers gets complex
CronUptime Advantages
- ✓No server infrastructure needed
- ✓Central dashboard for all scheduled tasks
- ✓Works with any HTTP endpoint
- ✓Built-in failure notifications
- ✓Reduces server management overhead
Why Use CronUptime Instead?
While Ansible cron jobs work for basic use cases, managing infrastructure for scheduled tasks adds complexity. CronUptime offers a simpler, serverless alternative.
No Infrastructure
We handle execution. No servers to maintain.
Reliable Timing
Built on Cloudflare for 99.9%+ uptime.
Any Endpoint
Works with any HTTP endpoint on any platform.
Frequently Asked Questions
Use the ansible.builtin.cron module with name, minute, hour, day, month, weekday, and job parameters. The name parameter makes the job idempotent.
Set state: absent with the same name: ansible.builtin.cron: name: 'job name' state: absent. The name must match the original.
Yes, use the 'user' parameter: ansible.builtin.cron: name: 'backup' job: '/backup.sh' user: postgres. Default is the Ansible connection user.
Quick Reference
Cron Expression:
*/5 * * * *Human Readable:
every 5 minutes
Cron Jobs on Other Platforms
Try CronUptime Free
Schedule HTTP requests without managing Ansible infrastructure.
Try it now - Free
Create a cron job running every 5 minutes. No sign-up required.