# Playbook action fails when template host fact host not available

**URL:** <https://forum.ansible.com/t/playbook-action-fails-when-template-host-fact-host-not-available/12622>\
**Category:** Ansible Project\
**Created:** [April 3, 2013, 3:33pm UTC](https://forum.ansible.com/t/playbook-action-fails-when-template-host-fact-host-not-available/12622 "2013-04-03T15:33:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TextEditor](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@TextEditor](https://forum.ansible.com/u/TextEditor)\
**Post date:** [April 3, 2013, 3:33pm UTC](https://forum.ansible.com/t/playbook-action-fails-when-template-host-fact-host-not-available/12622/1 "2013-04-03T15:33:57Z")

</div>

I have a playbook for installing and configuring Collectd. This has a firewall script which has this jinja2 template for loop:

{% for host in groups[‘collectd-clients’] %}  
{% if hostvars[host][‘ansible\_default\_ipv4’][‘address’] %}

## Host {{ hostvars[host][‘inventory\_hostname’] }}

{{ iptables\_path }} -A INPUT -p udp -s {{ hostvars[host][‘ansible\_default\_ipv4’][‘address’] }} --dport {{ collectd\_port }} -j ACCEPT  
{% endif %}  
{% if hostvars[host][‘ansible\_default\_ipv6’][‘address’] %}

## Host {{ hostvars[host][‘inventory\_hostname’] }}

{{ ip6tables\_path }} -A INPUT -p udp -s {{ hostvars[host][‘ansible\_default\_ipv6’][‘address’] }} --dport {{ collectd\_port }} -j ACCEPT  
{% endif %}  
{% endfor %}

However, when a host is not available but is in the client list (because for example downtime):

fatal: [vps24] =\> failed to transfer file to /setup:

ssh: connect to host vps24: Connection refused  
Connection closed

The step which sets the template fails with the following error:

fatal: [vps21] =\> {‘msg’: “‘dict object’ has no attribute ‘ansible\_default\_ipv4’”, ‘failed’: True}

I understand that it does not have the variable available, but is it a bug or a feature that the templating step fails? And, how can I work around it, except ignore\_failure?

This is the playbook template step, for reference:

- template: src=config/iptables.sh dest=/tmp/iptables-collectd.sh owner=root group=root mode=0755  
only\_if: “‘collectd-servers’ in $group\_names”  
tags:
- firewall

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 3, 2013, 3:43pm UTC](https://forum.ansible.com/t/playbook-action-fails-when-template-host-fact-host-not-available/12622/2 "2013-04-03T15:43:19Z")

</div>

Yep, of course it does, if you need to use a variable that may not be present, you should set a default.

group\_vars/all (or group\_vars/groupname) is a great way to do that.
