Skip to main content

Tackling Ansible Scheduling with the `at` Command

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

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 ones 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 will show how to set and how to remove a task from the list of jobs. The video included with this hands-on lab will also explain about the at command and show how it is used outside of Ansible. Note: Ansible has been setup and configured for use on the Control server and two nodes. This will save you time when doing the hands-on lab.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Tackling Ansible Scheduling with the at Command

Introduction

In this hands-on lab, we will use a playbook with the at module for job scheduling purposes.

Note: Ansible has been set up and configured already for use.

Solution

To access the lab environment, log in to the control node as cloud_user, using the IP address and login information provided in Credentials section of the hands-on lab page.

Sign onto the Ansible Control Node Server as cloud_user and Change to the ansible User

  1. Sign in to the server called Ansible Control Node using the cloud_user and change to the ansible user via the su - ansible command.
  2. Test that ansible is working via an ad-hoc command.
  3. We can use the following:

    ansible all -m ping

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:

    vim install-at.yml
  2. Enable that service atd and start the service as part of the playbook:

    hosts: all
    user: ansible
    become: yes
    gather_facts: no
    tasks:
    - name: install the at command for job scheduling
     action: yum name=at state=installed
    - name: Enable and Start service at if not started
     service:
       name: atd
       enabled: yes
       state: started
  3. Save and exit.

  4. Run the playbook and ensure that it installs, enables and starts the service correctly:

    ansible-playbook install-at.yml
  5. Run the following to view more information about our at command:

    man at
  6. We can clear our screen.

Create a Playbook Called at-scheduled-task.yml that Performs 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:
    vim at-scheduled-task.yml  

    Note: The job should run in 20 minutes.

  2. The task to run is df -h > /tmp/diskspace, which will appear like so:

    hosts: all
    user: ansible
    become: no
    gather_facts: no
    tasks:
    - name: Schedule a command to execute in 20 minutes as the ansible user
      at:
       command: df -h > /tmp/diskspace
       count: 20
       units: minutes
  3. Save and exit.
  4. Run the following:

    ansible-playbook at-scheduled-task.yml

Run the at-scheduled-task Playbook and Test Each Node to Ensure the Task has been Scheduled

  1. On each node we should test the df -h task has been scheduled. We can use the following command on each node to see if it has been scheduled:

    atq
    at -c 1
  2. We can clear the screen.

Create Playbook Called remove-at-task.yml That Will Remove the Previously Scheduled Task

  1. We will create it with:

    vim remove-at-task.yml
  2. The remove-at-task.yml playbook should remove the task that was set in the at-scheduled-task.yml playbook and the setup will look like this:

    hosts: all
    user: ansible
    become: no
    gather_facts: no
    tasks:
    
    - name: Match a command to an existing job and delete the job
    at:
      command: df -h > /tmp/diskspace
      state: absent
  3. Save and exit.
  4. Run our playbook with:

    ansible-playbook remove-at-task.yml
  5. We can log in to each node and test with the atq command to ensure the tasks have been removed.

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

Conclusion

Congratulations — you've completed this hands-on lab!