Unreachable host breaks a template?

Hi all,

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

`

{% for i in ansible_play_hosts %}

{{ i }} {{ hostvars[i].ansible_os_name }} {{ hostvars[i].ansible_distribution }} {{ hostvars[i].ansible_env.NUMBER_OF_PROCESSORS }}


`

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,

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.

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 %}

{% if hostvars[i].check_finish is not defined %} {{ i }} Host is unreachable by Ansible {% else %} {% if hostvars[i].check_finish.failed is defined %} {{ i }} {{ hostvars[i].check_finish.msg }} {% else %} {{ i }} {{ hostvars[i].ansible_env.USERDOMAIN }} ... ... ...

{% endif %}
{% endif %}

{% endfor %}

`

Hopefully this fixes the issue going forward https://github.com/ansible/ansible/pull/18409