The last CRON job file that I read was more than 30 lines. It was hideous, difficult to understand and had lengthy comments. Luckily, Ansible provides a “cron” module. This is a featured pack module which comes with several parameters that make managing commands very easy. Cron module will write the commands directly into the crontab of the user mentioned. Working with the cron module is easy as it comes with multiple useful parameters.
Example: Creating a Cron command in Ansible
If you are going to create a command to run at 03:30 every day, you would need to create a task like the one below:
- cron: name: "Clear Cache"; user: "root"; minute: 30; hour: 3; job: "php /var/www/html/app/artisan cache:clear";
The name field is important because it would be written as a comment in your crontab and Ansible will use it to delete or modify the command.
Ansible will convert the above, into a valid CRON syntax and write it to the crontab of the user “root”. This makes it very efficient. To view the above result, you should be logged as root user and execute “crontab -e” .
You would find the below commands in your crontab window:
#Ansible: Clear Cache 30 03 * * * php /var/www/html/artisan cache:clear
If you compare the command to the one written in Ansible, it is clearly clear that writing commands in Ansible are much clearer and manageable.
If you are not familiar with crontab, you could find out more about it on Ubuntu‘s manpages crontab introduction. In short, crontab is a private cronjob file that is associated with a user. Every user has their own crontab and any commands will be executed on behalf of the user.
In addition, this module provides few special parameters which make it even more interesting such as:
- special_time: With this parameter, you could specify the frequency of your commands example:
- state: This parameter takes in two choices (present or absent). If you set it to “Absent”, it will remove the command. Otherwise, it will create the command if it has not been created.
- cron_file: If you maintain your commands in a file, this parameter is useful for you. By specifying the path to your cron file, Ansible will use the file instead of writing to the crontab.
- backup: Sometimes, you may want to create a backup of every crontab prior to modifying it. By setting back up to yes, Ansible will create a backup of the crontab, and return it back in the backup_file variable. The backup file will be of the user you mention.
How to remove commands
Sometimes you have to delete command. In this example, we are going to delete the command which was created above.
- name: disable clear cache cron: name: Clear Cache user: root state: absent
Unlike removing, disabling comments out the command on crontab which can be performed as following:
- name: disable clear cache cron: name: Clear cache user: root disabled: yes
Cronvar a module to handle variables in the crontab
MAILTO is one of the most used variables in a cron file. You could easily update its variable using the cronvar module. This module, let’s you update variables in the crontab easily.
For example: in the code below we are going to update the mailto variable.
- name: update mailto variable cronvar: name: MAILTO value: firstname.lastname@example.org user: root
If you view the crontab of root, you would find the variable “MAILTOemail@example.com”
Ansible is without a doubt a helpful software. By using Ansible, maintaining cron tasks will be very easy. I hope you have found this article interesting and keen to work on Ansible
If you like to send emails Ansible offers this functionality