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.

Join 50+ hours of courses in our exclusive community

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:

---
- 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 the ansible.builtin.wait_for module. This module introduces a delay in task execution, effectively pausing the playbook’s progress for 30 seconds. The timeout parameter specifies the duration of the delay. Additionally, the delegate_to: localhost directive 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, the msg parameter contains a message indicating that 30 seconds have been waited.

Execution

ansible-playbook -i inventory wait_for.yml

Output

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 orchestrate and automate tasks across a distributed environment with remarkable precision. This showcased playbook highlights the importance of delayed task execution and message display, showcasing how to leverage the wait_for module to introduce delays and the debug module to communicate important information.

Incorporating such time management capabilities into your Ansible workflows can prove invaluable when orchestrating complex sequences of tasks or when synchronization between different components is critical. As you explore Ansible further, you’ll find that its adaptable nature caters to various scenarios, enabling you to handle tasks from the simplest configurations to the most intricate automation challenges.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza