I have a playbook to run command on a list of servers and put the results in a file.
But, sometimes, I can't connect to those servers due to an incorrect login/password or because it is unreachable.
When it is unreachable, I have this on the console:
fatal: [fqdn]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname fqdn: Name or service not known", "unreachable": true}
When I have an incorrect login or password, I have this:
fatal: [fqdn]: UNREACHABLE! => {"changed": false, "msg": "Invalid/incorrect password: Permission denied, please try again.", "unreachable": true}
I would like to capture this message in the output console to write in a template Jinja.
In order to have kind of a report.
I have done this in the playbook
- name: Listing des packages
hosts: all
gather_facts: false
tasks:
- name: Collect only selected facts
setup:
gather_subset: min
- name: Total number of updates
shell: yum check-update | wc -l
register: nbupdates
- name: Output to html file
template:
src: ./jinja/src/tpl_dashboard_update.j2
dest: ./jinja/dst/dashboard_update.html
force: yes
delegate_to: localhost
run_once: true
and the Jinja template
{% for host in ansible_play_hosts_all %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ host | upper }}</td>
{% if hostvars[host]['ansible_distribution'] is not defined %}
<td colspan="4">Injoignable</td>
{% else %}
<td>{{ hostvars[host]['ansible_distribution'] }} {{ hostvars[host]['ansible_distribution_version'] }}</td>
<td> {% if hostvars[host].nbupdates.stdout_lines.0 is defined %} {{ hostvars[host].nbupdates.stdout_lines.0 }} {% else %} --- {% endif %} </td>
{% endif %}
</tr>
{% endfor %}
It is not working properly, I only have the result when it is a success in the file and not the result, when it fails.
How I can "capture" the error message when it's unreachable or when I have a login/password error?