ansible.builtin.wait_for module – Waits for a condition before continuing
Note
This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name wait_for even without specifying the collections keyword. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.wait_for for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.
Synopsis
- You can wait for a set amount of time
timeout, this is the default if nothing is specified or justtimeoutis specified. This does not produce an error. - Waiting for a port to become available is useful for when services are not immediately available after their init scripts return which is true of certain Java application servers.
- It is also useful when starting guests with the community.libvirt.virt module and needing to pause until they are ready.
- This module can also be used to wait for a regex match a string to be present in a file.
- In Ansible 1.6 and later, this module can also be used to wait for a file to be available or absent on the filesystem.
- In Ansible 1.8 and later, this module can also be used to wait for active connections to be closed before continuing, useful if a node is being rotated out of a load balancer pool.
- For Windows targets, use the ansible.windows.win_wait_for module instead.
Parameters
Parameter | Comments |
|---|---|
active_connection_states list / elements=string | The list of TCP connection states which are counted as active connections. Default: |
connect_timeout integer | Maximum number of seconds to wait for a connection to happen before closing and retrying. Default: |
delay integer | Number of seconds to wait before starting to poll. Default: |
exclude_hosts list / elements=string | List of hosts or IPs to ignore when looking for active TCP connections for |
host string | A resolvable hostname or IP address to wait for. Default: |
msg string | This overrides the normal error message from a failure to meet the required conditions. |
path path | |
port integer | |
search_regex string | Can be used to match a string in either a file or a socket connection. Defaults to a multiline regex. When inspecting a system log file and a static string, remember that Ansible by default logs its own actions there; see the notes and examples for information. |
sleep integer | Number of seconds to sleep between checks. Before Ansible 2.3 this was hardcoded to 1 second. Default: |
state string | Either When checking a port When checking for a file or a search string Choices:
|
timeout integer | Maximum number of seconds to wait for, when used with another condition it will force an error. When used without other conditions it is equivalent of just sleeping. Default: |
Attributes
Attribute | Support | Description |
|---|---|---|
check_mode | Support: none | Can run in check_mode and return changed status prediction without modifying target, if not supported the action will be skipped. |
diff_mode | Support: none | Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode |
platform | Platform: posix | Target OS/families that can be operated against |
Notes
Note
- Under some circumstances when using mandatory access control, a path may always be treated as being absent even if it exists, but can’t be modified or created by the remote user either.
- When waiting for a path, symbolic links will be followed. Many other modules that manipulate files do not follow symbolic links, so operations on the path using other modules may not work exactly as expected.
- When searching a static string within a system log file, it is important to account for potential self-matching against log entries generated by the Ansible modules. To prevent this, add a regular expression construct into the search string. For example, to match a literal string ‘this thing’, one could use a regular expression like ‘this t[h]ing’.
See Also
See also
- ansible.builtin.wait_for_connection
-
Waits until remote system is reachable/usable.
- ansible.windows.win_wait_for
-
Waits for a condition before continuing.
- community.windows.win_wait_for_process
-
Waits for a process to exist or not exist before continuing.
Examples
- name: Sleep for 300 seconds and continue with play
ansible.builtin.wait_for:
timeout: 300
delegate_to: localhost
- name: Wait for port 8000 to become open on the host, don't start checking for 10 seconds
ansible.builtin.wait_for:
port: 8000
delay: 10
- name: Waits for port 8000 of any IP to close active connections, don't start checking for 10 seconds
ansible.builtin.wait_for:
host: 0.0.0.0
port: 8000
delay: 10
state: drained
- name: Wait for port 8000 of any IP to close active connections, ignoring connections for specified hosts
ansible.builtin.wait_for:
host: 0.0.0.0
port: 8000
state: drained
exclude_hosts: 10.2.1.2,10.2.1.3
- name: Wait until the file /tmp/foo is present before continuing
ansible.builtin.wait_for:
path: /tmp/foo
- name: Wait until the string "completed" is in the file /tmp/foo before continuing
ansible.builtin.wait_for:
path: /tmp/foo
search_regex: completed
- name: Wait until the string "tomcat up" is in syslog, use regex character set to avoid self match
ansible.builtin.wait_for:
path: /var/log/syslog
search_regex: 'tomcat [u]p'
- name: Wait until regex pattern matches in the file /tmp/foo and print the matched group
ansible.builtin.wait_for:
path: /tmp/foo
search_regex: completed (?P<task>\w+)
register: waitfor
- ansible.builtin.debug:
msg: Completed {{ waitfor['match_groupdict']['task'] }}
- name: Wait until the lock file is removed
ansible.builtin.wait_for:
path: /var/lock/file.lock
state: absent
- name: Wait until the process is finished and pid was destroyed
ansible.builtin.wait_for:
path: /proc/3466/status
state: absent
- name: Output customized message when failed
ansible.builtin.wait_for:
path: /tmp/foo
state: present
msg: Timeout to find file /tmp/foo
# Do not assume the inventory_hostname is resolvable and delay 10 seconds at start
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"
ansible.builtin.wait_for:
port: 22
host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
search_regex: OpenSSH
delay: 10
timeout: 300
delegate_to: localhost
# Same as above but using config lookup for the target,
# most plugins use 'remote_addr', but ssh uses 'host'
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"
ansible.builtin.wait_for:
port: 22
host: "{{ lookup('config', 'host', plugin_name='ssh', plugin_type='connection') }}"
search_regex: OpenSSH
delay: 10
timeout: 300
delegate_to: localhost
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key | Description |
|---|---|
elapsed integer | The number of seconds that elapsed while waiting Returned: always Sample: |
match_groupdict dictionary | Dictionary containing all the named subgroups of the match, keyed by the subgroup name, as returned by https://docs.python.org/3/library/re.html#re.Match.groupdict Returned: always Sample: |
match_groups list / elements=string | Tuple containing all the subgroups of the match as returned by https://docs.python.org/3/library/re.html#re.Match.groups Returned: always Sample: |
Collection links
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/wait_for_module.html