I am writing a playbook that will, as its final task, write a "summary" of sorts to a file on the controller (localhost).
Here is the playbook:
---
- 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 inventory:
[control]
ubuntu-c
[centos]
centos[1:3]
[ubuntu]
ubuntu[1:3]
[linux:children]
centos
ubuntu
When I run it I get this:
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
And the summary.txt file contains
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:
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
Based on Jack's suggestion I have this playbook now:
---
- 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 :
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
I just completed a play like that.
I do a connection test first, but ignore the unreachable hosts:
- name: ping hosts
ping:
register: connection_test
ignore_errors: yes
ignore_unreachable: yes
Then I create a file with the connection info with a Jinja template:
- name: Create start notification
template:
src: start_notification.j2
dest: "{{ role_path }}/files/start_notification.html"
mode: 0644
backup: yes
run_once: yes
delegate_to: localhost
become: no
And then I take those hosts out of the play:
- name: Remove failed and unreachable hosts from play_hosts
meta: end_host
when: connection_test.unreachable is defined or connection_test.rc != 0
Your template will loop through play_hosts, which will include the unreachable ones, examine the value of the connection_test, and report accordingly.
Sample Jinja2 template:
{% for target in play_hosts %}
{% if hostvars[target].connection_test.unreachable is defined %}
{% set test_result = "UNREACHABLE: " + hostvars[target].connection_test.msg %}
{% else %}
{% set test_result = "CONNECTION TEST SUCCEEDED" %}
{% endif %}
<TR>
<TD nowrap>{{ target }}</TD>
<TD>{{ test_result }}</TD>
</TR>
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With