Tackling Ansible Scheduling with the `at` Command

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

Being able to schedule tasks in Ansible is a valuable skill. This is shown by the fact that it is an objective of the *Red Hat Certified Ansible Specialist* exam. The `at` command and its associated commands (`atq` and `atrm`) can be used to schedule tasks in Linux-based systems or those that support the use of the `at` command. The `at` command is useful for scheduling one-time tasks. In this hands-on lab, we will make use of the `at` module to create scheduled tasks and show how to set and remove a task from the list of jobs.

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

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Log in to the Ansible Control Node Server and Change to the `ansible` User
  1. Sign in to the Ansible Control Node server using the cloud_user credentials, and change to the ansible user.
  2. Test that Ansible is working:

    ansible all -m ping

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

Create and Run a Playbook Called `install-at.yml` that Installs, Enables, and Starts the `at` Service on All Nodes
  1. Create a playbook called install-at.yml that will install the at service on all nodes.
  2. Enable the atd service and start the service as part of the playbook.
  3. Run the playbook and ensure it installs, enables, and starts the service correctly.
Create a Playbook Called `at-scheduled-task.yml` to Perform a Task on the Nodes in 20 Minutes
  1. Using the at module, create a playbook called at-scheduled-task.yml that adds a scheduled task to the nodes.
  2. The job should run in 20 minutes. The task to run is df -h > /tmp/diskspace.
Run the `at-scheduled-task` Playbook and Test Each Node to Ensure the Task Is Scheduled
  1. Use the atq command to ensure the df -h task has been scheduled.
Create a Playbook Called `remove-at-task.yml` to Remove the Previously Scheduled Task
  1. The remove-at-task.yml playbook should remove the task that was set in the at-scheduled-task.yml playbook.
  2. We can test with the atq command to ensure the task has been removed.

    Note: If they have not, we must troubleshoot why they have not been removed.

Additional Resources

Your manager has been told that Ansible can be used to schedule one-off tasks on multiple servers at the same time. To show this is possible, you will need to use the at module in several Ansible playbooks to manage the scheduling of a one-time task.

To do this assignment, you will make use of the at module to create a scheduled task, which will help demonstrate you know how to set up and remove a task from the list of jobs.

Using an Ansible playbook requires you to perform the following steps:

  1. Create a playbook called install-at.yml that will install and start the atd service.
  2. Create a playbook called at-scheduled-task.yml to perform a task (df -h > /tmp/diskspace) on the nodes set to start in 20 minutes.
  3. Run the playbook, and ensure the job exists on the nodes.
  4. Create a playbook called remove-at-task.yml that removes the task.
  5. Run the playbook and confirm the task has been removed from the nodes.

Here are some important things to consider while completing each task:

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

For detailed instructions on how to complete these tasks, click the Guide tab above the video player.

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?