# Nested variables and "with\_items: groups"

**URL:** <https://forum.ansible.com/t/nested-variables-and-with-items-groups/17250>\
**Category:** Ansible Project\
**Created:** [January 13, 2015, 3:28pm UTC](https://forum.ansible.com/t/nested-variables-and-with-items-groups/17250 "2015-01-13T15:28:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Goodall](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@David\_Goodall](https://forum.ansible.com/u/David_Goodall)\
**Post date:** [January 13, 2015, 3:28pm UTC](https://forum.ansible.com/t/nested-variables-and-with-items-groups/17250/1 "2015-01-13T15:28:08Z")

</div>

Hi,

I used to have this:  
with\_items: groups[‘masters’]

I have now changed the group name to reflect the project name, which is entered into the “project\_name” variable when the playbook is run.  
e.g. if the project is “test”, the group will be “test-masters”

In the “with\_items:” statement, I would like to be able to replace “masters” with something like {{ project\_name }}-masters.

The latest attempt is:  
with\_items: groups[‘“ansible\_” + project\_name + “-masters”’]

I have been trawling this group and have tried various incantations, all to no avail so far.

For bonus points, I also have this in a jinja2 template:

{% for host in groups[‘masters’] %}

- which will need the same treatment…

Any help appreciated!  
Best regards,  
David

---

<div class="post-metadata">

**Author:** ![David\_Goodall](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@David\_Goodall](https://forum.ansible.com/u/David_Goodall)\
**Post date:** [January 13, 2015, 4:43pm UTC](https://forum.ansible.com/t/nested-variables-and-with-items-groups/17250/2 "2015-01-13T16:43:34Z")

</div>

OK, figured it out finally:

with\_items: “{{ groups[project\_name + ‘-masters’] }}”

{% for host in groups[project\_name + ‘-masters’] %}
