Ansible Pilot

Time Management in Ansible Using Declarative Playbooks for Delayed Tasks

Mastering Delayed Execution and Message Display in Ansible Playbooks using wait_for Modules

August 21, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

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.

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

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

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases