# Using a variable inside a jinja loop

**URL:** <https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [June 24, 2014, 2:48pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533 "2014-06-24T14:48:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [June 24, 2014, 2:48pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533/1 "2014-06-24T14:48:03Z")

</div>

Hi there,

I have a play with a role that creates a load balancer template. I’ve successfully tested it in my dev environment, using

{% for host in groups[‘dev-web’] %}  
server {{ host }} {{ host }}:80  
{% endfor %}

the ‘dev-web’ group is defined in my inventory, as well as ‘stage-web’, ‘prod-web’, etc. What I’d like to do is change this template so that it can be used in any environment. I am already passing a parameter upon calling the play, --extra-vars “deploy\_env=dev”. In the play itself I can use this variable to determine on which balancer the template gets applied, by using something like this:

- hosts: [‘{{ deploy\_env }}-bal’]  
serial: 1  
vars\_files:
- /var/lib/ansible/vars/{{ deploy\_env }}.yml  
roles:
- role: “haproxy-webbal”

However, if I try something similar in the template, e.g.

{% for host in groups[‘{{ deploy\_env }}-web’] %}  
server {{ host }} {{ host }}:80  
{% endfor %}

I get this error:

fatal: [[stage-webbal-06.aws.primal.com](http://stage-webbal-06.aws.primal.com)] =\> {‘msg’: “One or more undefined variables: ‘dict object’ has no attribute ‘{{ deploy\_env }}-varnish’”, ‘failed’: True}

It seems that {{ deploy\_env }} is not being expanded, and it’s being taken as a literal. Is this because it’s within the {% %} section? If so, then what is the correct syntax for doing this? Alternatively, is there a cleaner way to approach this?

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [June 24, 2014, 3:05pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533/2 "2014-06-24T15:05:08Z")

</div>

When inside the {% %} part you are inside of jinja and don't need  
special syntax to tell it that your using jinja variables. Something  
like this should work:

{% for host in groups[deploy\_env + '-web'] %}  
&nbsp;&nbsp;&nbsp;&nbsp;server {{ host }} {{ host }}:80  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [June 24, 2014, 3:11pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533/3 "2014-06-24T15:11:14Z")

</div>

Access your var inside the “{% %}” block without a “{{ }}” surrounding and use string concatenation like this:

---

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [June 24, 2014, 3:13pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533/4 "2014-06-24T15:13:29Z")

</div>

d’oh! I should have known that. Thanks a bunch!

---

<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:** [June 24, 2014, 4:08pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533/5 "2014-06-24T16:08:57Z")

</div>

"

- hosts: [‘{{ deploy\_env }}-bal’]

"

This is gross and I would recommend not doing this.

Instead consider seperating your different environments into different inventory files (-i production), and having the host group just named “bal” in this case. You can still put nodes in different groups and keep your group\_vars/ and host\_vars/ directories all together.

---

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [June 25, 2014, 6:05pm UTC](https://forum.ansible.com/t/using-a-variable-inside-a-jinja-loop/15533/6 "2014-06-25T18:05:45Z")

</div>

I like that idea.

Right now we’re in a transition phase because I’ve inherited a horrible Puppet configuration that does unspeakably stupid/evil things. I actually have a rudimentary inventory script that polls puppetdb - eventually I’ll put that monstrosity to rest, but for now it’s convenient. Right now I still need to pass in the deploy\_env variable because some configurations require it and I haven’t gotten around to having the inventory script define them, but once that’s in place I’ll definitively go with your suggestion and make environment-specific inventories.
