Scheduling jobs with Ansible by using the Cron module is one of the key skills for success with using Ansible, and now it's actually an objective of the Red Hat Certified Ansible Specialist exam. In this hands-on lab we will use a playbook with the Cron module, and make schedule a task at a specified time. Then we will check when it was created properly, with
crontab command. Note: Ansible has been setup and configured for use on the Control server and two nodes. This will save time when doing the hands-on lab.
In this Hands-On Lab we will use a playbook with the archive module for backup purposes.
Note: Ansible has been setup and configured for your use.
To access the lab environment, log into the control node as
cloud_user, using the IP address and login credentials provided in the hands-on lab page.
Sign onto the Ansible Control Node Server as cloud_user and Change to the ansible User. Test to Ensure Ansible Has Been Set up for You
Sign into the server called Ansible Control Node, as
cloud_user, and become
su - ansible.
Test that Ansible is working via an ad-hoc command.
A possible command to use is the following:
ansible all -m ping
Create a Playbook Called
cron-tasks.yml That Performs a Task (
df -h >> /tmp/diskspace) on the Nodes at 5:00AM and 5:00PM
Using the cron module, create a playbook that adds a cron job to the nodes.
The job should run at 5AM and 5PM. The task to run is
df -h >> /tmp/diskspace.
--- - hosts: dbsystems user: ansible become: yes gather_facts: no tasks: - name: Ensure a job that runs at 5am and 5pm exists. Creates an entry like "0 5,17 * * * df -h >> /tmp/diskspace" cron: name: "Job 0001" minute: "0" hour: "5,17" job: "df -h >> /tmp/diskspace"
Run the Playbook and Test the Cron Job Exists on the Nodes
Run the playbook (with
ansible-playbook cron-tasks.yml) and then log into one of the nodes. Check that the cron job exists, with
sudo crontab -l.
It should look similar to this:
0 5,17 * * * df -h >> /tmp/diskspace
Create a Playbook to Remove the Cron Job. It Should Be Called
delete-crontask.yml and Should Only Delete That Cron Job
Now log out of the node, and let's create a new playbook. This new playbook should delete the cron job we just created, but leave any other cron jobs alone. This
delete-crontask.yml should look like this:
--- - hosts: dbsystems user: ansible become: yes gather_facts: no tasks: - name: 'Ensure an old job is no longer present. Removes any job that is prefixed by "#Ansible: Job 0001" from the crontab' cron: name: "Job 0001" state: absent
Before we go putting this into play, let's get back into
cron-tasks.yml. Change the time to 4:00AM, instead of 5:00, and change the
name: to be
0002. Now run that playbook again with
ansible-playbook cron-tasks.yml. Just like last time, let's log into one of the nodes and check. Run a
sudo crontab -l, and we should now see Job 0001 and Job 0002. Now we can exit out of this node.
Run the Playbook, Then Confirm That the Job Added Earlier Has Been Deleted
Back on the control machine, run the new playbook (
ansible-playbook delete-crontask.yml). Then, once again, log into a node and confirm that the original cron job has been deleted.
That's all folks. We've created an Ansible playbook that sets up a cron job, and another playbook that will delete a job. This is what we wanted. Congratulations!