| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: wait_for_connection |
| 12 |
short_description: Waits until remote system is reachable/usable |
| 13 |
description: |
| 14 |
- Waits for a total of O(timeout) seconds. |
| 15 |
- Retries the transport connection after a timeout of O(connect_timeout). |
| 16 |
- Tests the transport connection every O(sleep) seconds. |
| 17 |
- This module makes use of internal ansible transport (and configuration) and the M(ansible.builtin.ping)/M(ansible.windows.win_ping) |
| 18 |
modules to guarantee correct end-to-end functioning. |
| 19 |
- This module is also supported for Windows targets. |
| 20 |
version_added: '2.3' |
| 21 |
options: |
| 22 |
connect_timeout: |
| 23 |
description: |
| 24 |
- Maximum number of seconds to wait for a connection to happen before closing and retrying. |
| 25 |
type: int |
| 26 |
default: 5 |
| 27 |
delay: |
| 28 |
description: |
| 29 |
- Number of seconds to wait before starting to poll. |
| 30 |
type: int |
| 31 |
default: 0 |
| 32 |
sleep: |
| 33 |
description: |
| 34 |
- Number of seconds to sleep between checks. |
| 35 |
type: int |
| 36 |
default: 1 |
| 37 |
timeout: |
| 38 |
description: |
| 39 |
- Maximum number of seconds to wait for. |
| 40 |
type: int |
| 41 |
default: 600 |
| 42 |
extends_documentation_fragment: |
| 43 |
- action_common_attributes |
| 44 |
- action_common_attributes.flow |
| 45 |
attributes: |
| 46 |
action: |
| 47 |
support: full |
| 48 |
async: |
| 49 |
support: none |
| 50 |
bypass_host_loop: |
| 51 |
support: none |
| 52 |
check_mode: |
| 53 |
support: none |
| 54 |
diff_mode: |
| 55 |
support: none |
| 56 |
platform: |
| 57 |
details: As long as there is a connection plugin |
| 58 |
platforms: all |
| 59 |
seealso: |
| 60 |
- module: ansible.builtin.wait_for |
| 61 |
- module: ansible.windows.win_wait_for |
| 62 |
- module: community.windows.win_wait_for_process |
| 63 |
author: |
| 64 |
- Dag Wieers (@dagwieers) |
| 65 |
""" |
| 66 |
|
| 67 |
EXAMPLES = r""" |
| 68 |
- name: Wait 600 seconds for target connection to become reachable/usable |
| 69 |
ansible.builtin.wait_for_connection: |
| 70 |
|
| 71 |
- name: Wait 300 seconds, but only start checking after 60 seconds |
| 72 |
ansible.builtin.wait_for_connection: |
| 73 |
delay: 60 |
| 74 |
timeout: 300 |
| 75 |
|
| 76 |
# Wake desktops, wait for them to become ready and continue playbook |
| 77 |
- hosts: all |
| 78 |
gather_facts: no |
| 79 |
tasks: |
| 80 |
- name: Send magic Wake-On-Lan packet to turn on individual systems |
| 81 |
community.general.wakeonlan: |
| 82 |
mac: '{{ mac }}' |
| 83 |
broadcast: 192.168.0.255 |
| 84 |
delegate_to: localhost |
| 85 |
|
| 86 |
- name: Wait for system to become reachable |
| 87 |
ansible.builtin.wait_for_connection: |
| 88 |
|
| 89 |
- name: Gather facts for first time |
| 90 |
ansible.builtin.setup: |
| 91 |
|
| 92 |
# Build a new VM, wait for it to become ready and continue playbook |
| 93 |
- hosts: all |
| 94 |
gather_facts: no |
| 95 |
tasks: |
| 96 |
- name: Clone new VM, if missing |
| 97 |
community.vmware.vmware_guest: |
| 98 |
hostname: '{{ vcenter_ipaddress }}' |
| 99 |
name: '{{ inventory_hostname_short }}' |
| 100 |
template: Windows 2012R2 |
| 101 |
customization: |
| 102 |
hostname: '{{ vm_shortname }}' |
| 103 |
runonce: |
| 104 |
- cmd.exe /c winrm.cmd quickconfig -quiet -force |
| 105 |
delegate_to: localhost |
| 106 |
|
| 107 |
- name: Wait for system to become reachable over WinRM, polling every 10 seconds |
| 108 |
ansible.builtin.wait_for_connection: |
| 109 |
timeout: 900 |
| 110 |
sleep: 10 |
| 111 |
|
| 112 |
- name: Gather facts for first time |
| 113 |
ansible.builtin.setup: |
| 114 |
""" |
| 115 |
|
| 116 |
RETURN = r""" |
| 117 |
elapsed: |
| 118 |
description: The number of seconds that elapsed waiting for the connection to appear. |
| 119 |
returned: always |
| 120 |
type: float |
| 121 |
sample: 23.1 |
| 122 |
""" |