Tackling Ansible Scheduling with the `at` Command
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.
at command and its associated commands
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
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.
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
- Sign in to the server called Ansible Control Node using the
cloud_userand change to the
ansibleuser via the
su - ansiblecommand.
- Test that ansible is working via an
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.ymlthat will install the at service on all nodes:
Enable that service
atdand 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:
Run the following to view more information about our
Create a Playbook Called
at-scheduled-task.yml that Performs a Task on the Nodes in 20 Minutes
- Using the
atmodule, create a playbook called
at-scheduled-task.ymlthat adds a scheduled task to the nodes:
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:
at-scheduled-task Playbook and Test Each Node to Ensure the Task has been Scheduled
On each node we should test the
df -htask 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
Create Playbook Called
remove-at-task.yml That Will Remove the Previously Scheduled Task
We will create it with:
remove-at-task.ymlplaybook should remove the task that was set in the
at-scheduled-task.ymlplaybook 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:
We can log in to each node and test with the
atqcommand to ensure the tasks have been removed.
Note: If they have not, we must troubleshoot why they have not been removed.
Congratulations â you've completed this hands-on lab!