- name:Wait 600 seconds for target connection to become reachable/usablewait_for_connection:- name:Wait 300 seconds, but only start checking after 60 secondswait_for_connection:delay:60timeout:300## Wake desktops, wait for them to become ready and continue playbook- hosts:allgather_facts:notasks:- name:Send magic Wake-On-Lan packet to turn on individual systemswakeonlan:mac:'{{ mac }}'broadcast:192.168.0.255delegate_to:localhost- name:Wait for system to become reachablewait_for_connection:- name:Gather facts for first timesetup:## Build a new VM, wait for it to become ready and continue playbook- hosts:allgather_facts:notasks:- name:Clone new VM, if missingvmware_guest:hostname:'{{ vcenter_ipaddress }}'name:'{{ inventory_hostname_short }}'template:Windows 2012R2customization:hostname:'{{ vm_shortname }}'runonce:- powershell.exe -ExecutionPolicy Unrestricted -File C:\Windows\Temp\ConfigureRemotingForAnsible.ps1 -ForceNewSSLCert -EnableCredSSPdelegate_to:localhost- name:Wait for system to become reachable over WinRMwait_for_connection:timeout:900- name:Gather facts for first timesetup:
4. 剧本的使用
编写重启远程主机的剧本wait_for_connection.yml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- hosts:node1tasks:- name:Reboot the machine (Wait for 5 min)reboot:reboot_timeout:300msg:Reboot by Ansiblebecome:yes- name:Wait for the machine to come back onlinewait_for_connection:connect_timeout:60sleep:5delay:5timeout:300
检查并执行剧本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[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 for5 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=3changed=1unreachable=0failed=0skipped=0rescued=0ignored=0[ansible@master ansible_playbooks]$
可以看到任务Reboot the machine (Wait for 5 min)已经执行重启主机成功,Wait for the machine to come back online在第1次检查时就发现远程主机可连接了!!