# Loading a varible

**URL:** <https://forum.ansible.com/t/loading-a-varible/13799>\
**Category:** Ansible Project\
**Created:** [October 29, 2013, 7:12pm UTC](https://forum.ansible.com/t/loading-a-varible/13799 "2013-10-29T19:12:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [October 29, 2013, 7:12pm UTC](https://forum.ansible.com/t/loading-a-varible/13799/1 "2013-10-29T19:12:04Z")

</div>

I am trying to load a variable with information derived from the inventory groups that servers are members of. I have an inventory file that looks like the following:

[group1]  
server1  
server2  
server3

[group2]  
server2

[group3]  
server1  
server2

I need to set the variable such that it indicates each of the groups that a server exists in. So for server1, the variable would be set like: ‘variable: “app1,app3”’ because it is a member of group1 and group3. For server2, it would be: ‘variable: “app1,app2,app3”’ because it is a member of all three groups, and for server3 it would be: ‘variable: “app1”’ because it is a member of just group1. The variable will be used in a template to set some parameters there before installing the resulting file on the servers.  
This is probably simple for someone with more experience than me, but I am just starting out with this and have not been able to understand the syntax enough to make it work.  
Any help would be greatly appreciated.  
-Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 30, 2013, 2:02am UTC](https://forum.ansible.com/t/loading-a-varible/13799/2 "2013-10-30T02:02:07Z")

</div>

So there’s already a free automatic variable you have that you don’t need to manually configure

group\_names

It’s always a list and contains the groups of a current host.

Also groups[‘webservers’] is the list of all machines in the webservers group.

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [October 31, 2013, 5:16pm UTC](https://forum.ansible.com/t/loading-a-varible/13799/3 "2013-10-31T17:16:34Z")

</div>

I have been looking at the group\_names variable to try to see how I can use that. I am thinking in the template I need to do something like the following:

{% for mygroup in group\_names %}  
{% if mygroup[application] %}  
applications = “applications mygroup[application]”  
{% endif %}  
{% endfor $}

So the intent is that “application” would be loaded in the groups\_vars/groupname files. But not all group files will have this variable set as some of those groups refer to environment groups, not application groups (i.e. development, production, qc, etc.), so I need to test to see if the application variable is loaded for a group. If it is, then it needs to be concatenated to the contents of the applications variable. So, my syntax above is likely wrong; what is the proper syntax if this is indeed possible?  
Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 1, 2013, 12:13am UTC](https://forum.ansible.com/t/loading-a-varible/13799/4 "2013-11-01T00:13:30Z")

</div>

{% if application in mygroup %}

{% endif %}

should be what you want

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 1, 2013, 8:13pm UTC](https://forum.ansible.com/t/loading-a-varible/13799/5 "2013-11-01T20:13:33Z")

</div>

Okay, I am having trouble access the variable in the group files from the template. I my group files I have the following:

group file1 -  
application: app1

group file 2 -  
application: app2

group file 3 -

Then in the template I have:

{{ application | pprint }}  
{% for mygroup in group\_names %}  
{{ mygroup | pprint }}  
{{ application | pprint }}  
{% if ‘application’ in mygroup %}  
{{ application | pprint }}  
{% endif %}  
{% endfor %}

The ‘mygroup’ pprint statement outputs the group names as expected. The outermost ‘application’ pprint always outputs the last group file’s value (app3), but the next ‘application’ pprint statement inside of the for loop always returns “Undefined”, and the innermost statement is never reached. I have tried things like mygroup.application and mygroup[application], but still nothing. So what is the proper way to get at the variables contained within a group file?  
Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 2, 2013, 2:54am UTC](https://forum.ansible.com/t/loading-a-varible/13799/6 "2013-11-02T02:54:00Z")

</div>

by “group file” do you mean, “group\_vars/”

I’m trying to read between the lines but having trouble if we’re not using the same terminology.

Also I don’t see where you have a top level variable named “application”

Reading between the lines some, I suspect you have a system in more than one group.

Variables come from all groups and will overwrite each other, so I would recommend the following structure for your group\_vars/groupname file

application\_foo:  
blarg: 12

application\_bar:  
blarg: 15

And then you can do “application\_bar.blarg”, etc

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 3, 2013, 4:44am UTC](https://forum.ansible.com/t/loading-a-varible/13799/7 "2013-11-03T04:44:16Z")

</div>

That’s correct, I was referring to the group\_var/groupname files, and yes, my servers can occupy more than one group. I was hoping for something like a hostvars structure, so that I could do something like mygroup[application] and get the value of application in that group. But it sounds like not. So, in each group\_vars/groupname file, I would need to define a variable unique to that group (file group1 contains variable group1\_app, file group2 contains variable group2\_app, file group3 contains, variable group3\_app, etc.), then in the templates I need to check for all possible applications:

{{ applications | default(“”) }}  
{% if ‘group1’ in group\_names and group1\_app is defined %}  
{% set applications = applications ~ group1\_app ~ " " %}  
{% endif %}  
{% if ‘group2’ in group\_names and group2\_app is defined %}  
{% set applications = applications ~ group2\_app ~ " " %}  
{% endif %}  
{% if ‘group3’ in group\_names and if group3\_app is defined %}  
{% set applications = applications ~ group3\_app ~ " " %}  
{% endif %}

And so on for each and every possible group I have in my inventory. The problem with that is that I have many, many groups, so this list can get quite long and ugly. And every time I add a new application to my inventory, I have to go through each of my template files to add a check for that application. Is there any easier method than this?  
Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 3, 2013, 4:45pm UTC](https://forum.ansible.com/t/loading-a-varible/13799/8 "2013-11-03T16:45:48Z")

</div>

" was hoping for something like a hostvars structure, so that I could do something like mygroup[application] and get the value of application in that group. But it sounds like not. "

We may be communicating on completely different wavelengths.

I view the group\_vars/ structure exactly like host\_vars/.

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 4, 2013, 3:43am UTC](https://forum.ansible.com/t/loading-a-varible/13799/9 "2013-11-04T03:43:22Z")

</div>

In the Ansible documentation there is discussion about “Magic Variables”. Among them are hostvars, group\_name, and groups. You can get at variables for other hosts with the structure {{ hostvars[‘server\_name’][‘host\_variable’] }}. I was hoping for something similar for group information: {{ groupvars[‘group\_name’][‘group\_variable’] }}. This would return the value of ‘group\_variable’ that is set within group\_vars/groupname. That would allow me to iterate through the groups that a server is a member of and collect the value of ‘group\_variable’ in those groups. Is there any way to do this?  
Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 4, 2013, 11:12pm UTC](https://forum.ansible.com/t/loading-a-varible/13799/10 "2013-11-04T23:12:47Z")

</div>

Sorry, this doesn’t exist, although variables set on the host are accessible directly

hostvars[hostname][group\_variable]

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 4, 2013, 11:52pm UTC](https://forum.ansible.com/t/loading-a-varible/13799/11 "2013-11-04T23:52:21Z")

</div>

Yeah, I finally figured that out. In my group\_vars/groupname files, I set a variable whose name is the same as the file name:

group\_vars/groupname:

groupname:  
app\_var: app1

Then in my template I can iterate over the groups:

{%- set applications = [‘’] %}  
{%- for mygroup in group\_names %}  
{%- if hostvars[inventory\_hostname][mygroup].app\_var is defined %}  
{%- do applications.insert(0, applications[0] ~ hostvars[inventory\_hostname][mygroup].app\_var) %}  
{%- do applications.pop() %}  
{%- endif %}  
{%- endfor %}

This finally does what I have been looking for (trying to dig the value of “applications” out of the middle of the for loop was yet another exercise in frustration!).  
Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 5, 2013, 4:09am UTC](https://forum.ansible.com/t/loading-a-varible/13799/12 "2013-11-05T04:09:19Z")

</div>

Yeah, I finally figured that out. In my /group\_vars/groupname files, I set a variable whose name is the same as the file name:

group\_vars/groupname:

groupname:  
app\_var: app1

Then in my template I can iterate over the groups:

{%- set applications = [‘’] %}  
{%- for mygroup in group\_names %}  
{%- if hostvars[inventory\_hostname][mygroup].app\_var is defined %}  
{%- do applications.insert(0, applications[0] ~ hostvars[inventory\_hostname][mygroup].app\_var) %}  
{%- do applications.pop() %}  
{%- endif %}  
{%- endfor %}

This finally does what I have been looking for (trying to dig the value of “applications” out of the middle of the for loop was yet another exercise in frustration!).  
Thanks,  
-Mark
