Tackling Ansible Scheduling with the `at` Command

Training Architect
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.
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
- Sign in to the server called Ansible Control Node using the
cloud_user
and change to theansible
user via thesu - ansible
command. - Test that ansible is working via an
ad-hoc
command. 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
Create a playbook called
install-at.yml
that will install the at service on all nodes:vim install-at.yml
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
Save and exit.
Run the playbook and ensure that it installs, enables and starts the service correctly:
ansible-playbook install-at.yml
Run the following to view more information about our
at
command:man at
We can
clear
our screen.
Create a Playbook Called at-scheduled-task.yml
that Performs a Task on the Nodes in 20 Minutes
- Using the
at
module, create a playbook calledat-scheduled-task.yml
that adds a scheduled task to the nodes:vim at-scheduled-task.yml
Note: The job should run in 20 minutes.
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
- Save and exit.
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
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
- We can
clear
the screen.
Create Playbook Called remove-at-task.yml
That Will Remove the Previously Scheduled Task
We will create it with:
vim remove-at-task.yml
The
remove-at-task.yml
playbook should remove the task that was set in theat-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
- Save and exit.
Run our playbook with:
ansible-playbook remove-at-task.yml
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!