# reference variables from inventory file in a jinja2 hostvars loop

**URL:** <https://forum.ansible.com/t/reference-variables-from-inventory-file-in-a-jinja2-hostvars-loop/24689>\
**Category:** Ansible Project\
**Created:** [June 8, 2017, 9:59pm UTC](https://forum.ansible.com/t/reference-variables-from-inventory-file-in-a-jinja2-hostvars-loop/24689 "2017-06-08T21:59:48Z")\
**Posts on this page:** 4\
**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 8, 2017, 9:59pm UTC](https://forum.ansible.com/t/reference-variables-from-inventory-file-in-a-jinja2-hostvars-loop/24689/1 "2017-06-08T21:59:48Z")

</div>

Hi all,

Can any one help me with the below, im trying to reference variables from my inventory file in a jinja2 hostvars loop. Ive copied the host file and jinja template below.

Template File

In my Jinja 2 template i have the following loop

{% for host in groups[‘redishosts’:‘haproxyhosts’] %}  
sentinel known-sentinel mymaster {{ hostvars[host][‘ansible\_eth0’][‘ipv4’][‘address’] }} 26379 {{ hostvars[host][‘sentinel\_id’].value }}  
{% endfor %}

The idea being that i want to pull the variables ie the systems ip and the sentinel\_id varaible from the host facts and inventory file variable, listed below

Inventory File

[redishosts]  
192.168.0.1 ansible\_ssh\_private\_key\_file=~/.ssh/id\_rsa redis\_role=master sentinel\_id=‘1234567’  
192.168.0.1 ansible\_ssh\_private\_key\_file=~/.ssh/id\_rsa redis\_role=slave sentinel\_id=‘7654321’

Thanks for any help

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 8, 2017, 10:03pm UTC](https://forum.ansible.com/t/reference-variables-from-inventory-file-in-a-jinja2-hostvars-loop/24689/2 "2017-06-08T22:03:27Z")

</div>

The problem isn’t with how you are accessing it really, it’s more of a problem with your `groups` in your for loop. That is invalid.

Instead of:

groups[‘redishosts’:‘haproxyhosts’]

You will likely want:

lookup(‘inventory\_hostnames’, ‘redishosts:haproxyhosts’)

Additionally, you don’t need the `.value` on the end.

So putting it together:

{% for host in lookup(‘inventory\_hostnames’, ‘redishosts:haproxyhosts’) %}  
sentinel known-sentinel mymaster {{ hostvars[host][‘ansible\_eth0’][‘ipv4’][‘address’] }} 26379 {{ hostvars[host][‘sentinel\_id’].value }}  
{% endfor %}

Alternatively, you may also be able to do:

{% for host in groups[‘redishosts’] + groups[‘haproxyhosts’] %}

---

<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:** [June 9, 2017, 7:36pm UTC](https://forum.ansible.com/t/reference-variables-from-inventory-file-in-a-jinja2-hostvars-loop/24689/3 "2017-06-09T19:36:49Z")

</div>

or  
{% for host in groups['redishosts']|union(groups['haproxyhosts'] %}

---

<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 9, 2017, 9:01pm UTC](https://forum.ansible.com/t/reference-variables-from-inventory-file-in-a-jinja2-hostvars-loop/24689/4 "2017-06-09T21:01:00Z")

</div>

Thanks both worked but I have a new error. Ansible undefined variable. Then it prints out the sentinel id. So it does see the content of the sentinel\_id host file variable .
