Ansible Cron

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

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.

*This course is not approved or sponsored by Red Hat.*

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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 using the cloud_user and change to the ansible user via the su - ansible command.

Test that Ansible is working via an ad-hoc command.

A possible command to use is the following:

ansible all -m ping

Note: It may take several minutes after the start of the lab, for the test to work as expected.

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.

Run the Playbook and Test Whether the Cron Job Exists on the Nodes

Run the playbook and then log into the nodes and check that the cron job exists.

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

Create a new playbook. This new playbook should delete the cron job you just created,
but leave any other cron jobs there.

Run the Playbook, Then Confirm That the Job Added Earlier Has Been Deleted

Run the playbook and confirm that the original cron job has been deleted.

Additional Resources

Our manager wants us to use Ansible for scheduling a task across all of our servers, using the Cron module. This is to show management that Ansible can be used for this task.

In this hands-on lab, we will make use of the Cron module to create scheduled tasks. Then we will show how to set up and remove a task from crontab.

To summarize, using an Ansible playbook we must do the following:

  1. Create a playbook called cron-tasks.yml that performs a task (df -h >> /tmp/diskspace) on the nodes at 5am and 5pm.
  2. Run the playbook and test that the cron job exists on the nodes.
  3. Create a playbook that removes that task.
  4. Run the playbook and confirm that the cron job has been removed from the nodes.

Here are some important things to take into consideration while completing each task:

  • For convenience, Ansible is already on the control node. If we connect to the server by clicking on the Public IP address in a browser, we need to make sure we change to the ansible user with the su - ansible command.
  • The user ansible is present on all servers, with appropriate shared keys for access to managed servers from the control node. Make sure to use the ansible user to complete the commands.
  • The ansible user has the same password as cloud_user.
  • The default Ansible inventory has been configured for with the appropriate hosts and groups.
  • /etc/hosts entries are present on control1 for the managed servers.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?