Skip to content

wait_for_connection等待远程主机连接模块

1. 概要

2. 参数

参数可选值默认值说明
connect_timeout5integer,在重试之前,等待成功连接到被控主机的最长秒数
delay0integer,开始轮询之前等待的秒数
sleep1integer,在两次检查之间休眠的秒数
timeout600integer,最长等待秒数

3. 官方示例

yaml
- name: Wait 600 seconds for target connection to become reachable/usable
  wait_for_connection:

- name: Wait 300 seconds, but only start checking after 60 seconds
  wait_for_connection:
    delay: 60
    timeout: 300

# Wake desktops, wait for them to become ready and continue playbook
- hosts: all
  gather_facts: no
  tasks:
  - name: Send magic Wake-On-Lan packet to turn on individual systems
    wakeonlan:
      mac: '{{ mac }}'
      broadcast: 192.168.0.255
    delegate_to: localhost

  - name: Wait for system to become reachable
    wait_for_connection:

  - name: Gather facts for first time
    setup:

# Build a new VM, wait for it to become ready and continue playbook
- hosts: all
  gather_facts: no
  tasks:
  - name: Clone new VM, if missing
    vmware_guest:
      hostname: '{{ vcenter_ipaddress }}'
      name: '{{ inventory_hostname_short }}'
      template: Windows 2012R2
      customization:
        hostname: '{{ vm_shortname }}'
        runonce:
        - powershell.exe -ExecutionPolicy Unrestricted -File C:\Windows\Temp\ConfigureRemotingForAnsible.ps1 -ForceNewSSLCert -EnableCredSSP
    delegate_to: localhost

  - name: Wait for system to become reachable over WinRM
    wait_for_connection:
      timeout: 900

  - name: Gather facts for first time
    setup:

4. 剧本的使用

编写重启远程主机的剧本wait_for_connection.yml:

yaml
- hosts: node1
  tasks:
    - name: Reboot the machine (Wait for 5 min)
      reboot:
        reboot_timeout: 300
        msg: Reboot by Ansible
      become: yes

    - name: Wait for the machine to come back online
      wait_for_connection:
        connect_timeout: 60
        sleep: 5
        delay: 5
        timeout: 300

检查并执行剧本:

sh
[ansible@master ansible_playbooks]$ ansible-lint wait_for_connection.yml
[ansible@master ansible_playbooks]$ ansible-playbook wait_for_connection.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [node1] ***********************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [node1]

TASK [Reboot the machine (Wait for 5 min)] *****************************************************************************
changed: [node1] => {"changed": true, "elapsed": 38, "rebooted": true}

TASK [Wait for the machine to come back online] ************************************************************************
ok: [node1] => {"changed": false, "elapsed": 5}

PLAY RECAP *************************************************************************************************************
node1                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[ansible@master ansible_playbooks]$

可以看到任务Reboot the machine (Wait for 5 min)已经执行重启主机成功,Wait for the machine to come back online在第1次检查时就发现远程主机可连接了!!

登陆远程主机,使用uptime查看启动时间:

sh
[root@node1 ~]# uptime
 23:01:26 up 3 min,  1 user,  load average: 0.15, 0.17, 0.08
[root@node1 ~]#

可以看到,才启动一会儿,说明我们重启正常,wait_for_connection检查远程主机连接也是正常的!!

本首页参考 https://notes.fe-mm.com/ 配置而成