Unreachable host breaks a template?

168 views
Skip to first unread message

aut...@gmail.com

unread,
Nov 7, 2016, 9:15:51 AM11/7/16
to Ansible Project
Hi all,

I'm building an HTML report based on a template that looks something like this:

{% for i in ansible_play_hosts %}
  <tr>
    <td class="tg-o8wf">{{ i }}</td>
    <td class="tg-o8wf">{{ hostvars[i].ansible_os_name }}</td>
    <td class="tg-o8wf">{{ hostvars[i].ansible_distribution }}</td>
    <td class="tg-o8wf">{{ hostvars[i].ansible_env.NUMBER_OF_PROCESSORS }}</td>
...
...

In case one of the hosts is unreachable, template is not being generated and the following error is being generated:

TASK [xxx] *************************************
task path: /etc/ansible/playbooks/xxx.yml:6
fatal: [xxx -> localhost]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_args": {"dest": "/etc/ansible/playbooks/xxx.txt", "src": "/etc/ansible/playbooks/xxx.j2"}, "module_name": "template"}, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_env'"}
...ignoring

Any idea how to overcome this? Skip the unreachable host but do successfully generate "dest" file?

Thanks,

Brian Coca

unread,
Nov 7, 2016, 6:06:27 PM11/7/16
to ansible...@googlegroups.com
What is failing is that you are attempting to access data you don't have (you failed to gather it).  There seems to be a bug in ansible_play_hosts that is not currently (2.2) excluding failed hosts from the list.


----------
Brian Coca

aut...@gmail.com

unread,
Nov 8, 2016, 2:06:36 AM11/8/16
to Ansible Project
Hi,

Thanks for the heads up,

Meanwhile I managed to overcome this with bunch of IF's in my template which looks something like:

{% for i in ansible_play_hosts %}
  <tr>
{% if hostvars[i].check_finish is not defined %}
    <td>{{ i }}</td>
    <td>Host is unreachable by Ansible</td>
{% else %}
{% if hostvars[i].check_finish.failed is defined %}
    <td class="tg-o8wf">{{ i }}</td>
    <td class="tg-o8wf" colspan="7">{{ hostvars[i].check_finish.msg }}</td>
{% else %}
    <td>{{ i }}</td>
    <td>{{ hostvars[i].ansible_env.USERDOMAIN }}</td>
...
...
...
{% endif %}
{% endif %}
{% endfor %}

Brian Coca

unread,
Nov 8, 2016, 9:57:01 AM11/8/16
to ansible...@googlegroups.com
Hopefully this fixes the issue going forward https://github.com/ansible/ansible/pull/18409

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages