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:
- Waiting for a service to start or stop
- Waiting for a file to exist or not to exist
- 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.
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:
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.
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:
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:
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:
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.