# hopefully quick question about jinja2 for loops and variables

**URL:** <https://forum.ansible.com/t/hopefully-quick-question-about-jinja2-for-loops-and-variables/13028>\
**Category:** Ansible Project\
**Created:** [June 27, 2013, 2:44pm UTC](https://forum.ansible.com/t/hopefully-quick-question-about-jinja2-for-loops-and-variables/13028 "2013-06-27T14:44:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan1](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jonathan1](https://forum.ansible.com/u/Jonathan1)\
**Post date:** [June 27, 2013, 2:44pm UTC](https://forum.ansible.com/t/hopefully-quick-question-about-jinja2-for-loops-and-variables/13028/1 "2013-06-27T14:44:55Z")

</div>

Hi all,

I am trying to iterate over a set of hosts in a jinja2 template almost exactly like this example from the Advanced Playbooks page:

```auto
{% for host in groups['app_servers'] %}
   {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}

```

the difference is in my example the group name is generated from a variable. So instead of a known string like ‘app\_servers’ I have something like ‘app\_type\_$foo’. This syntax is not working and I never enter the for loop.

For debugging purposes I am creating a jinja2 variable

{% set group\_name = app\_type\_{{ foo }}’ %}

and this is being set correctly, but I can’t figure out the syntax for referencing the variable I have set in the jinja2 for loop, which is likely a side affect of my Python !awesomeness

{% for host in groups[‘group\_name’] %} doesn’t work, nor does

{% for host in groups[group\_name] %}

I am hoping that this is possible, but I have a syntax issue.  
any help would be most appreciated.

cheers,  
Jonathan

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 27, 2013, 2:48pm UTC](https://forum.ansible.com/t/hopefully-quick-question-about-jinja2-for-loops-and-variables/13028/2 "2013-06-27T14:48:27Z")

</div>

I am not sure your set statement is right actually.

Instead of:

{% set group\_name = app\_type\_{{ foo }}’ %}

I would do:

{% set group\_name = ‘app\_type’ + foo %}

And then in your loop, rather than doing ‘group\_name’ you want the unquoted group\_name:

{% for host in groups[group\_name] %}

You should probably check what “group\_name” got set to with your “set” statement and will probably find out it was not what you expected…

---

<div class="post-metadata">

**Author:** ![Jonathan1](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jonathan1](https://forum.ansible.com/u/Jonathan1)\
**Post date:** [June 27, 2013, 5:28pm UTC](https://forum.ansible.com/t/hopefully-quick-question-about-jinja2-for-loops-and-variables/13028/3 "2013-06-27T17:28:07Z")

</div>

Works.

Not entirely sure how you ever actually write code due to your list contributions 🙂 Many thanks!
