Explaining the Ansible wait_for

Explaining the  Ansible wait_for
Written by Nilima PaulFebruary 8, 2022
11 min read
Nilima Paul

Technology Security Analyst

In this article, We will know About Ansible wait_for.

Introduction to Ansible wait_for

In Ansible, we face numerous circumstances where an undertaking is reliant upon an assistance, port, or framework's state. However, that state is relied upon to happen after some time because of an association reset, server reboot, execution time taken by a code, or time taken to begin assistance. So we want to sit tight for it to occur. Presently Ansible is so wealthy in its module treasure that it has a module named wait_for which empowers our playbook to hang tight for a state or errand culmination on remote objective machines. Utilizing this module we will trust that a condition will happen before going on with the next undertakings of our playbook.'

What is Ansible wait_for?

Ansible wait_for module can be utilized with or without a condition on remote objective hosts and it checks for that condition's state following a predefined time right away.

While utilizing module, one should keep in mind underneath focuses:

  • On the off chance that you simply need to stand by and not check any condition, then, at that point, utilize just break boundary with time in seconds to pause. This works on your suspicion of required holding up time.
  • This module can be utilized for conditions like for beginning a visitor to with virt module, File to exits, match a regex in the record.
  • For Microsoft Windows remote objective machines, use a module named win_wait_for.
  • This module is having in reverse similarity support going on in the next variant, by the Ansible center group.

How does Ansible wait_for work?

To work with the Ansible wait_for module, we should be aware of the satisfactory boundaries and their adequate choices. A portion of the boundaries have default values set, so this must likewise be noted.

Given beneath is a rundown of boundaries:

1. break: Default is 300 seconds. The greatest number of seconds to sit tight for. This shouldn't be utilized with different circumstances, since then it will fizzle.

2. state: Below are the adequate states:

  • begun: regardless of whether the port is open.
  • halted: regardless of whether the port is shut.
  • depleted: Whether the association is dynamic.
  • present: Whether a string is available in a record or string.
  • missing: regardless of whether the record is eliminated or missing.

3. rest: Default is 1 second. This is to rest between checks.

4. search_regex: To look at a string in document or attachment association.

5. port: Port number to check.

6. way: Path of a record which should be existing before proceeding.

7. msg: This is the message which will be printed instead of a normal error message in case of failures.

8. host: The IP or hostname to wait for.

9. exclude_hosts: The list of hosts, not to check when looking for active connections (TCP).

10. delay: Default is 0. This represents the number of seconds to wait for the next polling.

11. connect_timeout: Default is 5 seconds. This is to specify the time to wait for a connection to establish before closing and trying again.

12. active_connection_states: This is the list of connections (TCP) that can be counted as an active session. Default values are “TIME_WAIT”, “FIN_WAIT2, “FIN_WAIT1”, “ESTABLISHED”, “SYN_SENT”, “SYN_RECV”.

The unique return values by this module will enable us to know the actual execution happethat ned on remote target systems.

Given below are such unique return values:

1. elapsed: The seconds elapsed while waiting.

2. match_groupdict: This is the dictionary containing all the subgroup's names matched by the key given.

3. match_groups: Tuple containing all the subgroups of the match.

Examples of Ansible wait_for

Given beneath are the models referenced:

Here we have one Ansible regulator hub named as ansible-regulator. As target hubs, we have two remote machines. The first machine is a Red Hat Enterprise Linux machine named host tone and the second machine is a Ubuntu machine named host-two. We will run our playbooks on the Ansible regulator machine and make changes on remote objective machines.

Example #1

In this model, we have a playbook like beneath, utilizing which we will attempt to reboot the remote machines and the utilization wait_for module to sit tight for 30 seconds, and afterward, we look at the uptime on the same machines.


-name: Here we are rebooting the remote machines using Ansible reboot module reboot:
-name: Here we will wait for 30 seconds and then continue with the next wait_for:
timeout: 30
-name: Here we are checking the uptime on the shell: uptime
register: var_uptime
-name: Here we are printing the output of debugging:
var: var_uptime.stdout_lines

Now running this playbook like below, we have below output where we can see that servers were rebooted, and then we waited for 30 seconds, and then uptime was displayed.

ansible-playbook ansible_wait_for_reboot.YAML -v



Example #2

In this model, we have a playbook underneath where we are checking for n httpd.conf record, if this document exists, alter this document. On the off chance that this document doesn't exist then, at that point, sit tight for it. For this, we have a playbook underneath.


-hosts: host-one tasks:
-name: Here we check for file httpd.conf
existance wait_for:
path: /etc/httpd/conf/httpd.conf
state: present
name: Here we are placing line in this file using lineinfile
module lineinfile:
path: /etc/httpd/conf/httpd.conf regexp: '^Listen '
insertafter: '^#Listen ' line: Listen 8443

Here, at first, we didn't have the httpd bundle introduced on having one, so the playbook was hanging tight for the httpd.conf document, as this record is given by the httpd bundle. Then, at that point, while it was sitting tight for httpd to be introduced and a document to be made, we went to have one and introduced the httpd bundle in the foundation. Presently in yield, you can see the slipped by time is 77 seconds which shows it sat tight for 77 seconds.

ansible-playbook ansible_wait_for_file.yaml -v




As we found in this article, this module is truly useful in commonsense or constant situations, where you truly need to sit tight for quite a while with the goal that a condition can finish on remote objective frameworks. Tasks like fixing, provisioning use it well indeed. In any case, as you probably are aware we will be exceptionally mindful with regards to its accessible choices before utilizing it. So learn it first and afterward use it.

Was this blog helpful?
You must be Logged in to comment
Code Block
Nilima Paul
Technology Security Analyst
201 Blog Posts
0 Discussion Threads
Trending Technologies
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Penetration Testing16

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance