'Handle unreachable host in ansible
I am writing a playbook that will, as its final task, write a "summary" of sorts to a file on the localhost.
Here's the yaml:
---
- name: Summary
hosts: all
tasks:
- name: Register Ip Addr
command: "hostname -i"
register: ip_addr
- name: Write to file on localhost
shell: |
echo {{ ip_addr.stdout }} >> ./summary.txt
delegate_to: localhost
post_tasks:
- name: Post Task
shell:
cat ./summary.txt
register: cat_out
delegate_to: localhost
run_once: True
- name: Output cat out
debug:
msg: "{{ cat_out.stdout }}"
delegate_to: localhost
run_once: True
Here is my hosts file I use for my inventory.
[control]
ubuntu-c
[centos]
centos[1:3]
[ubuntu]
ubuntu[1:3]
[linux:children]
centos
ubuntu
When I run it I get this ...
ansible@ubuntu-c:~/summary$ ansible-playbook -i hosts summary.yaml
PLAY [Summary] ******************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [ubuntu1]
ok: [centos1]
ok: [centos2]
ok: [centos3]
ok: [ubuntu2]
ok: [ubuntu3]
fatal: [ubuntu-c]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ansible@ubuntu-c: Permission denied (publickey,password).", "unreachable": true}
TASK [Register Hostname] ********************************************************************************************************************
changed: [ubuntu2]
changed: [ubuntu1]
changed: [centos1]
changed: [centos2]
changed: [centos3]
changed: [ubuntu3]
TASK [Write to file on localhost] ***********************************************************************************************************
changed: [centos1 -> localhost]
changed: [centos2 -> localhost]
changed: [centos3 -> localhost]
changed: [ubuntu1 -> localhost]
changed: [ubuntu2 -> localhost]
changed: [ubuntu3 -> localhost]
TASK [Post Task] ****************************************************************************************************************************
changed: [centos1 -> localhost]
TASK [Output cat out] ***********************************************************************************************************************
ok: [centos1 -> localhost] => {
"msg": "172.20.0.6\n172.20.0.5\n172.20.0.7\n172.20.0.8\n172.20.0.3\n172.20.0.4"
}
PLAY RECAP **********************************************************************************************************************************
centos1 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
centos2 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
centos3 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu-c : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
ubuntu1 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu2 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu3 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible@ubuntu-c:~/summary$ cat summary.txt
172.20.0.6
172.20.0.5
172.20.0.7
172.20.0.8
172.20.0.3
172.20.0.4
I would like to be able to account for the unreachable host, ubuntu-c, in the output so that summary.txt would look like this:
ansible@ubuntu-c:~/summary$ cat summary.txt
172.20.0.6
172.20.0.5
172.20.0.7
ubuntu-c (unreachable)
172.20.0.8
172.20.0.3
172.20.0.4
UPDATE: Based on Jack's suggestion I have this playbook now:
ansible@ubuntu-c:~/summary$ cat summary.yaml
---
- name: Summary
hosts: all
# gather_facts: False
tasks:
- name: Register Hostname
command: "hostname -i"
register: echo_hostname
ignore_unreachable: True
ignore_errors: True
post_tasks:
- name: Show template
pause:
seconds: 1
prompt: |
{% for host in vars['ansible_play_hosts_all'] %}
"{{ host }}"{% if not loop.last %},{% endif %}
{% if hostvars[host]['echo_hostname'] is defined %}
{{ hostvars[host]['echo_hostname']['stdout'] }}
{% else %}
"{{ host }} unreachable."
{% endif %}
{% endfor %}
"-------------"
"end of file"
delegate_to: localhost
run_once: True
When I run the playbook ...
ansible@ubuntu-c:~/summary$ ansible-playbook -i hosts summary.yaml
...
PLAY [Summary] ******************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [ubuntu1]
ok: [centos1]
ok: [centos2]
ok: [centos3]
ok: [ubuntu2]
ok: [ubuntu3]
fatal: [ubuntu-c]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ansible@ubuntu-c: Permission denied (publickey,password).", "unreachable": true}
TASK [Register Hostname] ********************************************************************************************************************
changed: [ubuntu2]
changed: [ubuntu1]
changed: [centos2]
changed: [centos1]
changed: [centos3]
changed: [ubuntu3]
TASK [Show template] ************************************************************************************************************************
Pausing for 1 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
[Show template]
"ubuntu-c", "ubuntu-c unreachable."
"centos1", 172.20.0.6
"centos2", 172.20.0.8
"centos3", 172.20.0.5
"ubuntu1", 172.20.0.7
"ubuntu2", 172.20.0.3
"ubuntu3" 172.20.0.4
"-------------"
"end of file"
:
ok: [centos1 -> localhost]
PLAY RECAP **********************************************************************************************************************************
centos1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
centos2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
centos3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu-c : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
ubuntu1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ubuntu3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
