Introduction
Ansible, a versatile automation tool, empowers system administrators to achieve operational excellence through streamlined workflows and simplified configurations. This article delves into a specific Ansible playbook that showcases the art of managing time-sensitive tasks using declarative playbooks. By understanding and implementing delayed execution, administrators can effortlessly control when and how tasks are executed across a cluster of hosts.
Links
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html
Understanding the Playbook
In the following Ansible playbook snippet, we’ll explore how to introduce a delay in task execution and display a message after the delay:
``yaml
---
- name: Wait 30 seconds
hosts: all
tasks:
- name: Sleep for 30 seconds
ansible.builtin.wait_for:
timeout: 30
delegate_to: localhost
- name: Display message
ansible.builtin.debug:
msg: "Waited 30 seconds"
`
Breaking Down the Playbook
1. Playbook Name: The playbook initiates with a descriptive name, “Wait 30 seconds,” signifying its purpose.
2. Target Hosts: The playbook is configured to apply its tasks to all hosts listed in the Ansible inventory (hosts: all), ensuring consistency across the specified hosts.
3. Tasks Section: The tasks: section encapsulates the series of tasks to be sequentially executed on the designated hosts.
- Sleep for 30 Seconds: The initial task, named “Sleep for 30 seconds
,” employs theansible.builtin.wait_formodule. This module introduces a delay in task execution, effectively pausing the playbook’s progress for 30 seconds. Thetimeoutparameter specifies the duration of the delay. Additionally, thedelegate_to: localhostdirective instructs Ansible to perform this task on the control machine rather than the remote hosts.
- Display Message: The subsequent task, named “Display message,” employs the ansible.builtin.debug
module. This module is used to present debug information. In this case, themsgparameter contains a message indicating that 30 seconds have been waited.
Execution
`bash
ansible-playbook -i inventory wait_for.yml
`
Output
`bash
PLAY [Wait 30 seconds]
TASK [Gathering Facts]
ok: [localhost]
TASK [Sleep for 30 seconds] *
ok: [localhost]
TASK [Display message]
ok: [localhost] => {
"msg": "Waited 30 seconds"
}
PLAY RECAP **
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
``
Conclusion
By utilizing Ansible’s declarative playbooks, administrators can orch