# Looping group\_vars into a template

**URL:** <https://forum.ansible.com/t/looping-group-vars-into-a-template/14566>\
**Category:** Ansible Project\
**Created:** [February 14, 2014, 3:02pm UTC](https://forum.ansible.com/t/looping-group-vars-into-a-template/14566 "2014-02-14T15:02:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jepper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jepper](https://forum.ansible.com/u/jepper)\
**Post date:** [February 14, 2014, 3:02pm UTC](https://forum.ansible.com/t/looping-group-vars-into-a-template/14566/1 "2014-02-14T15:02:59Z")

</div>

This is a bit over my head, so I’m going to ask.

I have several environments, with many variables defined using group\_vars, e.g.

test1/group\_vars/all  
test2/group\_vars/all

I have mule in each environment, and I need to tailor a file which tells must what libraries to load. We can it main.xml, one for each environment. Different teams should be able to include or exclude the libraries they wish to work with.

So I can’t have a global or completely static main.xml.j2 template. I do now, and I only replace a hostname, but this will not suffice.

I have to construct main.xml.

test1: main.xml might have

spring:beans  
\<spring:import resource=“a.xml”/\>  
\<spring:import resource=“b.xml”/\>  
\<spring:import resource=“c.xml”/\>  
\<spring:import resource=“d.xml”/\>  
\<spring:import resource=“e.xml”/\>

\</spring:beans\>

while test2 main.xml might have

spring:beans  
\<spring:import resource=“a.xml”/\>  
\<spring:import resource=“b.xml”/\>  
\<spring:import resource=“c.xml”/\>  
\</spring:beans\>

In group\_vars/all for each environment, I want  
test1/group\_vars/all  
spring: a,b,c,d,e

test2/group\_vars/all  
spring: a,b,c

How should I construct my template file for this to work?

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [February 14, 2014, 5:03pm UTC](https://forum.ansible.com/t/looping-group-vars-into-a-template/14566/2 "2014-02-14T17:03:13Z")

</div>

I may have some of the exact syntax wrong, I haven't as much with templates, but the following should be close.

group\_vars/all:

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 15, 2014, 6:23am UTC](https://forum.ansible.com/t/looping-group-vars-into-a-template/14566/3 "2014-02-15T06:23:49Z")

</div>

Depending on what your results should look like you have a couple of options…

I have group\_vars files for several different groups…

list\_one:

- item\_a
- item\_b
- item\_c

list\_two:

- { part\_one: value\_one\_a, part\_two: value\_two\_a }

- { part\_one: value\_one\_b, part\_two: value\_two\_b }

- { part\_one: value\_one\_c, part\_two: value\_two\_c }

Then in the template files I can use

blah blah blah  
{% for item in list\_one %}  
blah {{ item }}  
{% endfor %}  
blah blah blah

{% for item in list\_two %}  
blah {{ item.part\_one }}  
blah blah {{ item.part\_two }}  
{% endfor %}

blah blah blah

So in your case you would want to use something like this in your test1 group\_vars file you would have

spring:

- a
- b
- c
- d
- e

and your test 2 group\_vars file would have  
spring:

- a
- b
- c

And your template would look like this…  
spring:beans  
{% for bean in spring %}  
\<spring: import resource=“{{ bean }}.xml”/\>  
{% endfor %}  
\</spring:beans\>

The second example I gave at the top would be more useful if your variables have multiple related components…for example I use it for setting some information with a repeated paragraph with a server name / IP address combination in a template…

I hope that this helps.

Adam

---

<div class="post-metadata">

**Author:** ![jepper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jepper](https://forum.ansible.com/u/jepper)\
**Post date:** [February 17, 2014, 2:45pm UTC](https://forum.ansible.com/t/looping-group-vars-into-a-template/14566/4 "2014-02-17T14:45:11Z")

</div>

So simple, I’m almost ashamed I asked. Thanks!
