# Unreachable host breaks a template?

**URL:** <https://forum.ansible.com/t/unreachable-host-breaks-a-template/22967>\
**Category:** Ansible Project\
**Created:** [November 5, 2016, 9:17pm UTC](https://forum.ansible.com/t/unreachable-host-breaks-a-template/22967 "2016-11-05T21:17:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [November 5, 2016, 9:17pm UTC](https://forum.ansible.com/t/unreachable-host-breaks-a-template/22967/1 "2016-11-05T21:17:15Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 7, 2016, 11:06pm UTC](https://forum.ansible.com/t/unreachable-host-breaks-a-template/22967/2 "2016-11-07T23:06:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [November 8, 2016, 7:06am UTC](https://forum.ansible.com/t/unreachable-host-breaks-a-template/22967/3 "2016-11-08T07:06:35Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 8, 2016, 2:56pm UTC](https://forum.ansible.com/t/unreachable-host-breaks-a-template/22967/4 "2016-11-08T14:56:47Z")

</div>

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