034、wait_for_connection等待远程主机连接模块
本文最后更新于 66 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

wait_for_connection等待远程主机连接模块

1. 概要

2. 参数

参数 可选值 默认值 说明
connect_timeout 5 integer,在重试之前,等待成功连接到被控主机的最长秒数
delay 0 integer,开始轮询之前等待的秒数
sleep 1 integer,在两次检查之间休眠的秒数
timeout 600 integer,最长等待秒数

3. 官方示例

- 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:

- 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

检查并执行剧本:

[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查看启动时间:

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

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

谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇