Use Ansible wait_for Module for Ensuring Task Completion

Ansible Wait_for Module Example

The wait_for module allows Ansible to pause until a specific condition is met. For example, when automating a service’s deployment, you may need to ensure that a required port is open or a process has started before proceeding to the next task.

Some common use cases include:

  • Waiting for a web server to start listening on port 80.
  • Ensuring configuration files exist before applying further settings.
  • Confirming that a service has fully start.

This guide will walk you through practical examples of using the wait_for module to verify task completion in Ansible.

Basic Syntax

Below is the basic syntax:

- name: Wait for a condition
  ansible.builtin.wait_for:
    path: /path/to/file
    port: 80
    host: 127.0.0.1
    timeout: 300
    state: started
    delay: 10
    sleep: 5

Key Parameters

  • path: Wait for a file or directory to exist.
  • port: Wait for a TCP port to be open.
  • host: Specify the target host (default is 127.0.0.1).
  • timeout: Maximum time (in seconds) to wait for the condition.
  • state: Desired state (started, stopped, present, etc.).
  • delay: Initial delay before starting the check (in seconds).
  • sleep: Time (in seconds) between condition checks.

Example 1: Wait for a Port to Open

In this example, the playbook waits for a web server to start listening on port 80. It ensures the web server is fully operational before proceeding.

- name: Wait for a port to open
  hosts: web_servers
  tasks:
    - name: Wait for port 80 to be open
      ansible.builtin.wait_for:
        port: 80
        host: 127.0.0.1
        timeout: 300
        state: started
        sleep: 10
        delay: 5
      register: web_port_check

    - name: Print the port check result
      debug:
        msg: >
          "Port 80 is open: {{ web_port_check.changed }}. 
          Task elapsed time: {{ web_port_check.elapsed }} seconds."

Example 2: Wait for a File to Exist

This example waits for a specific configuration file to exist before proceeding with further tasks, ensuring that the application configuration has been created.

- name: Wait for a file to exist
  hosts: app_servers
  tasks:
    - name: Wait for the configuration file to be created
      ansible.builtin.wait_for:
        path: /etc/myapp/config.yaml
        timeout: 120
        state: present
        sleep: 5
      register: file_check

    - name: Print the file existence status
      debug:
        msg: >
          "Configuration file exists: {{ file_check.changed }}. 
          Task elapsed time: {{ file_check.elapsed }} seconds."

    - name: Perform next steps if the file exists
      ansible.builtin.shell: echo "File exists, proceeding with next steps."
      when: file_check.changed == False

Example 3: Wait for a Service to Start

This example waits for the MySQL service to start and ensures that the database server is ready to accept connections.

- name: Wait for a service to start
  hosts: db_servers
  tasks:
    - name: Wait for MySQL service to start listening on port 3306
      ansible.builtin.wait_for:
        host: 127.0.0.1
        port: 3306
        timeout: 180
        state: started
        sleep: 10
        delay: 5
      register: mysql_service_check

    - name: Print MySQL service status
      debug:
        msg: >
          "MySQL service status: {{ mysql_service_check.changed }}. 
          Task elapsed time: {{ mysql_service_check.elapsed }} seconds."

    - name: Verify database connectivity
      ansible.builtin.shell: mysql -u root -e "SHOW DATABASES;"
      when: mysql_service_check.changed == False

In this playbook:

  • The debug task prints the MySQL service status and task elapsed time.
  • The when clause ensures the database connectivity verification step only runs if the MySQL service is already running and no changes were needed (changed == False).

Example 4: Wait for Disk Space Availability

This example waits until sufficient disk space becomes available before proceeding with a deployment.

- name: Wait for disk space to become available
  hosts: all
  tasks:
    - name: Check for available disk space on /var
      ansible.builtin.wait_for:
        path: /var
        state: present
        timeout: 300
        sleep: 10
      register: disk_space_check

    - name: Print disk space check result
      debug:
        msg: >
          "Sufficient disk space available: {{ disk_space_check.changed }}. 
          Task elapsed time: {{ disk_space_check.elapsed }} seconds."

    - name: Continue deployment if space is available
      ansible.builtin.shell: echo "Starting deployment..."
      when: disk_space_check.changed == False

This playbook waits for sufficient disk space on /var, reports the check result and elapsed time, and proceeds with deployment only if no space changes are needed.

Conclusion

The Ansible wait_for is indispensable for ensuring tasks proceed only when a required condition is met. Whether you’re waiting for a port to open or a service to start, this module simplifies orchestrating dependent tasks in your playbooks. Using timeout, state, and delay parameters, you can customize the behavior of the module to suit your needs.

FAQs

1. Is the wait_for module idempotent?

Yes, the wait_for is idempotent, meaning it only performs tests and waits without altering the resource’s state.

2. What should I do if the wait_for module fails?

You can use the ignore_errors directive or add a conditional task to handle failures and proceed with alternative actions.

3. How do I handle long initialization times with the wait_for directive?

Increase the timeout value and adjust the sleep parameter to optimize for longer initialization periods without frequent verifications.

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva