-
Notifications
You must be signed in to change notification settings - Fork 54
/
handle_unreachable_hosts_method_1.yml
44 lines (38 loc) · 1.5 KB
/
handle_unreachable_hosts_method_1.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---
# in this method, no system will report as failed or unreachable
# unreachable systems identified in the first play
# any tasks/roles can be executed in the 2nd play on reachable systems
# result: all tasks are executed on all reachable systems
- name: ping hosts
hosts: all
gather_facts: no
tasks:
- block:
- wait_for:
timeout: 10 # you can increase this timeout as appropriate for your environment
host: "{{ inventory_hostname }}"
port: "{{ ansible_port }}"
delegate_to: localhost
rescue:
- set_fact:
unreachable: true
always:
- name: add reachable hosts to a new group
add_host:
name: "{{ inventory_hostname }}"
group: reachable_hosts
- debug:
msg: "failed hosts: {{ ansible_play_hosts | map('extract', hostvars) | list | json_query(query) }}"
run_once: true
vars:
query: "@[?unreachable].inventory_hostname"
# you can take any action here on behalf of failed hosts
# remember to use (run_once: true) and (delegate_to: localhost) as appropriate
# why delegate_to: localhost? you might ask. Imagine a scenario where all hosts failed.
# in that case, there are no hosts to execute any tasks on, you can delegate_to: localhost for any action needed
# for example send email, make api call to update a record somewhere etc.
- name: other play
hosts: reachable_hosts
tasks:
- debug:
msg: "{{ inventory_hostname }} is reachable"