# Jinja 2 template syntax error

**URL:** <https://forum.ansible.com/t/jinja-2-template-syntax-error/24738>\
**Category:** Ansible Project\
**Created:** [June 15, 2017, 4:47pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738 "2017-06-15T16:47:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Owen\_Corcoran](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Owen\_Corcoran](https://forum.ansible.com/u/Owen_Corcoran)\
**Post date:** [June 15, 2017, 4:47pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/1 "2017-06-15T16:47:09Z")

</div>

Hi,

Im having an issue with the following Jinja2 template and I just cant figure out how to get it to display the ip4 addresses of the hosts from the hostvars for the hosts in the inventory file below

{% for host\_system in groups['hosts'] %}

{% if "ansible\_eth0" in hostvars[host\_system] -%}

sentinel known-sentinel mymaster {{ hostvars[host\_system]['ansible\_eth0']['ipv4']['address'] }} 26379 {{ hostvars[host\_system]['sentinel\_id'] }}

{%- endif -%}

{% endfor %}

Inventory

[hosts:children]

haproxyhosts

redishosts

[haproxyhosts]

Hahost1

Hahost2

[redishosts]

Rhost1

Rhost2

Error

fatal: [10.193.45.158]: FAILED! =\> {

&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,

&nbsp;&nbsp;&nbsp;&nbsp;"failed": true,

&nbsp;&nbsp;&nbsp;&nbsp;"invocation": {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"daemon\_reload": false,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"enabled": true,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"masked": null,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "redis-sentinel",

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "restarted",

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user": false

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;},

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Unable to restart service redis-sentinel: Job for redis-sentinel.service failed because the control process exited with error code. See \"systemctl status redis-sentinel.service\" and \"journalctl -xe\" for details.\n"

}

fatal: [10.193.45.157]: FAILED! =\> {

&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,

&nbsp;&nbsp;&nbsp;&nbsp;"failed": true,

&nbsp;&nbsp;&nbsp;&nbsp;"invocation": {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"daemon\_reload": false,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"enabled": true,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"masked": null,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "redis-sentinel",

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "restarted",

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user": false

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;},

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Unable to restart service redis-sentinel: Job for redis-sentinel.service failed because the control process exited with error code. See \"systemctl status redis-sentinel.service\" and \"journalctl -xe\" for details.\n"

}

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 15, 2017, 5:01pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/2 "2017-06-15T17:01:33Z")

</div>

What does the generated config look like?  
The error is just redis-sentinel crashing.

---

<div class="post-metadata">

**Author:** ![Owen\_Corcoran](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Owen\_Corcoran](https://forum.ansible.com/u/Owen_Corcoran)\
**Post date:** [June 15, 2017, 5:13pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/3 "2017-06-15T17:13:17Z")

</div>

Looks like I pastes the wrong error. The error is undefined variable ansible\_eth0. It’s works when I only use one group rather than the combined group. Plus it doesn’t seem to see the host var properties for the hosts

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [June 15, 2017, 5:37pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/4 "2017-06-15T17:37:21Z")

</div>

Hi,

15 juin 2017 19:13 “Owen Corcoran” \<cyperplex@gmail.com\> a écrit:

---

<div class="post-metadata">

**Author:** ![Owen\_Corcoran](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Owen\_Corcoran](https://forum.ansible.com/u/Owen_Corcoran)\
**Post date:** [June 15, 2017, 6:47pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/5 "2017-06-15T18:47:40Z")

</div>

What the play book is running is

{% for host\_system in groups[‘hosts’] %}  
sentinel known-sentinel mymaster {{ hostvars[host\_system][‘ansible\_eth0’][‘ipv4’][‘address’] }} 26379 {{ hostvars[host\_system][‘sentinel\_id’] }}  
{% endfor %}

The error im getting implied the host\_system variable isnt getting a value that hostvars can process to determine the values im trying to reference

“changed”: false,  
“failed”: true,  
“invocation”: {  
“module\_args”: {  
“backup”: “yes”,  
“dest”: “/etc/redis-sentinel.conf”,  
“group”: “redis”,  
“mode”: “0644”,  
“owner”: “redis”,  
“src”: “templates/redis-sentinel.conf.j2”  
},  
“module\_name”: “template”  
},  
“msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ansible\_eth0’”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 15, 2017, 7:45pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/6 "2017-06-15T19:45:32Z")

</div>

The template looks correct, so two things can result in this message, you are not running facts and/or the hosts doesn't have a interface name eth0.

You can easily check if they have eth0 with ad-hoc Ansible  
ansible hosts -m setup -a "filter=ansible\_eth0"

---

<div class="post-metadata">

**Author:** ![Owen\_Corcoran](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Owen\_Corcoran](https://forum.ansible.com/u/Owen_Corcoran)\
**Post date:** [June 15, 2017, 8:16pm UTC](https://forum.ansible.com/t/jinja-2-template-syntax-error/24738/7 "2017-06-15T20:16:02Z")

</div>

Checked all hosts, and they all have eth0 interfaces.

The loop in the template works when i limit it to one host group . I think its just not getting the vars the error is

AnsibleUnderDefinedVariable: dict object has no attribute ansible\_eth0
