# Accessing Variables within Inventory Vars using Jinja2

**URL:** <https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257>\
**Category:** Ansible Project\
**Created:** [May 16, 2014, 7:09pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257 "2014-05-16T19:09:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 7:09pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/1 "2014-05-16T19:09:43Z")

</div>

I have been having a really tough time trying to use variables that are defined within an Inventory file.

For Example:

Inventory File:

[WebApp\_Login]  
server1  
server2  
server3

[WebApp\_Login:vars]  
route=login

[WebApp\_Sweetness]  
server2

[WebApp\_Sweetness:vars]  
route=sweetness

[WebApp\_ReallyAwesomeSweetness]  
server3

[WebApp\_ReallyAwesomeSweetness:vars]  
route=reallyawesomesweetness

J2 Template:

{% for service\_name in groups|sort %} {% for host in groups[service\_name]|sort %} {{ host }} {% endfor %} {% endfor %}

I need to fill in the part that is bolded in the J2 Template. Any help would be greatly appreciated. I feel like there should be something called “groupvars” to be able to access, but there doesn’t appear to be.

Thank You!

---

<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:** [May 16, 2014, 7:41pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/2 "2014-05-16T19:41:09Z")

</div>

groupvars are applied directly to the hosts that reside within that group.

So you should just be able to use {{route}} as long as the server resides within a group that contains the “route” variable.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 16, 2014, 7:41pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/3 "2014-05-16T19:41:11Z")

</div>

the problem is that group\_vars are not 'by group' when you access them,  
they are collapsed into host. This should give you what you want.

route={{hostvars[groups[service\_name][0]].route}}

^ might need to ensure all {} and match

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 16, 2014, 7:43pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/4 "2014-05-16T19:43:59Z")

</div>

I should explain what the monster in my previous answer does:

hostvars[]. \<= gives you access to the variable (varname) for a ‘non current host’ (hostname), otherwise you could just use .

groups[service\_name][0] \<== this retrieves the first hostname from the current groupname in the service\_name variable.

compose both and your template ‘should’ work  
​

---

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 7:48pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/5 "2014-05-16T19:48:07Z")

</div>

Thanks a lot for the explanation. I will give it a try!

---

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 7:52pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/6 "2014-05-16T19:52:35Z")

</div>

I changed my template to:

{% for service\_name in groups|sort %} {% if service\_name not in ['all', 'ungrouped'] %} {% if hostvars[groups[service\_name][0]].route %} {% else %} {% endif %} {% for host in groups[service\_name]|sort %} {{ host }} {% endfor %} {% endif %} {% endfor %}

The idea is to have a route next to id in the xml it’s generating if it has a route, otherwise, do not add a route to the xml.

I get an odd error back stating:

[localhost] =\> {‘msg’: 'host not found: ', ‘failed’: True}

Keep in mind, my hosts I am running this against is localhost. The idea behind this playbook is to just generate an xml file via the services inventory file.

---

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 8:00pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/7 "2014-05-16T20:00:42Z")

</div>

Adding [0] after the groups[service\_name] command is what is causing it to blow up.

---

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 8:05pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/8 "2014-05-16T20:05:34Z")

</div>

I figured out what was wrong. Some of my groups did not have servers.

It now runs, but everything in the generated xml has the same route. I will continue working on this, but the help so far was huge. Much further than I was before.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 16, 2014, 8:13pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/9 "2014-05-16T20:13:16Z")

</div>

removing the [0] should return a list, not a single host, see if this is a bit cleaner

{% for service\_name in groups|sort|difference([‘all’, ‘ungrouped’]) %}  
{% set first\_host = groups[service\_name][0] %}  
{% if ‘route’ in hostvars[first\_host] %}  
  
​

this also assumes that the first host in the group has the correct route var (it can be overridden by individual hosts)

---

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 8:28pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/10 "2014-05-16T20:28:25Z")

</div>

Understood. The issue is all of the groups have the same host. What seems to be happening is the last route I assign to that host is what is used for all three. I will have to make the routevariables unique for each and call them all separately. Maybe something like this:

Inventory:

[WebApp\_Login:vars]  
WebApp\_Login\_route=login

[WebApp\_number2]  
KSQ-B-WEB-D01

[WebApp\_number2:vars]  
WebApp\_number2\_route=exposureindexbuilder

[WebApp\_number3]  
KSQ-B-WEB-D01

[WebApp\_number3:vars]  
WebApp\_number3\_route=marketdataadministration

{% for service\_name in groups|sort|difference([‘all’, ‘ungrouped’]) %}  
{% set first\_host = groups[service\_name][0] %}  
{% if ‘service\_name\_route’ in hostvars[first\_host] %}

This is what I am working on now. Thanks again for all of your help.

I realize I am doing something out of the ordinary here. (Not using this inventory file as a typical inventory file.)

---

<div class="post-metadata">

**Author:** ![Adam\_Keech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@Adam\_Keech](https://forum.ansible.com/u/Adam_Keech)\
**Post date:** [May 16, 2014, 8:44pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/11 "2014-05-16T20:44:14Z")

</div>

This is what I came up with and it’s now working great! Thanks a lot for the help!

{% set route = service\_name + “\_route” %}  
{% if hostvars[groups[service\_name][0]][route] %}  
  
{% else %}  
  
{% endif %}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 16, 2014, 11:28pm UTC](https://forum.ansible.com/t/accessing-variables-within-inventory-vars-using-jinja2/15257/12 "2014-05-16T23:28:10Z")

</div>

Good deal!

FYI – It looks like for each group of hosts in the list “groups” the OP wants to do print some things. If so, this first outer loop should just be

{% for service\_name in group\_names|sort %}

Versus “groups” above. Else service\_name isn’t a string, it’s a list of hosts if you had “groups” instead.
