Ansible Timeout

Ansible is a free and open-source automation tool that can help automate a wide variety of DevOps tasks such as application provisioning, configuration management, application deployment, and more.

Ansible works using modules which are specific units that can perform a specialized task on a given system. For example, you will encounter modules for package management with yum, apt, and more.

However, within Ansible’s vast array of modules, there’s a fascinating one called “wait_for”. This module can be handy in various situations where you need Ansible to pause the execution until certain conditions are met or just delayed for a given duration.

This post explores what this module does, how it works, and how we can use it in a real-world playbook.

Ansible Wait_For Module

As mentioned, the wait_for module allows us to pause the execution of a playbook until a  specified condition is met. Some common use case of this module is when:

  1. Waiting for a service to start or stop
  2. Waiting for a file to exist or not to exist
  3. Setting for a fixed delay duration

Think of it as the sleep or delay command in your automation playbooks. Therefore, it is an essential tool to control and synchronize the running tasks.

Module Parameters

The wait_for module offers parameters that allow us to specify what it does and how it affects the playbooks.

The following are some of the commonly used parameters for the module.

Host – This specifies a hostname or IP address to wait for.

Port – This defines a port number to poll.

Path – This sets the path to a file on the system, typically used when ensuring that a file is present or absent.

State – This defines the desired state; accepted values include:

  • Default
  • Stopped
  • Present
  • Absent

Delay – This sets the number of seconds to wait before starting to check.

Timeout – This defines a maximum number of seconds to wait for.


Let us now look at the examples of using the wait_for module in Ansible playbooks.

Example 1: Waiting for a Port

One everyday use case is ensuring that a given port is available. For example, if we start a service that listens on a given port, we must ensure that the port is available before moving on to the next task. We can accomplish this using the wait_for parameter.

- name: Wait for port 8000 to become available
    host: localhost
    port: 8000
    state: started
    timeout: 300

Example 2: Waiting for a File

We can also do the same with a file using the wait_for module. We can check whether a file exists before proceeding as follows:

- name: Wait for the configuration file to be present
    path: "/etc/nginx/nginx.conf"
    state: present
    timeout: 120

Example 3: Ansible Timeout

The most typical use of the wait_for module is introducing a fixed delay. This allows the playbook to sleep or wait for the specified delay value as shown in the following playbook example:

- name: Wait for 5 minutes
    timeout: 300

Example 4: Delegating the Wait

We may also encounter such scenarios where we need to check the condition from a different host than the one that we currently manage. For that, we can pair the wait_for module with the delagate_to directive as demonstrated in the following:

- name: Check port 80 on the web server from a monitoring host
    host: web_server
    port: 80
    state: started
    timeout: 300
  delegate_to: monitoring_host


You learned about the workings of the wait_for module in Ansible to introduce a delay for a given condition or a sleep duration in your playbook tasks. This feature can be handy when paired with other Ansible modules by introducing the modularity and requirements before proceeding to the next tasks.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list