# Access group members of a variable group name

**URL:** <https://forum.ansible.com/t/access-group-members-of-a-variable-group-name/36250>\
**Category:** Ansible Project\
**Created:** [January 26, 2022, 8:36am UTC](https://forum.ansible.com/t/access-group-members-of-a-variable-group-name/36250 "2022-01-26T08:36:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roshani](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Roshani](https://forum.ansible.com/u/Roshani)\
**Post date:** [January 26, 2022, 8:36am UTC](https://forum.ansible.com/t/access-group-members-of-a-variable-group-name/36250/1 "2022-01-26T08:36:27Z")

</div>

Hi everyone,  
I have many groups in my inventory like this:

[website-front-lb]  
192.168.1.2  
192.168.1.3

[proj1-subproj1-lb]  
192.168.1.22  
192.168.1.33

etc.

Now I want to access members of a specific group in my playbook. Both to show them or use them in conditionals.

I know that the nested {{}} notation is not possible. Also, {{}} can not be used in conditionals like when. I have tried to use “lookup” plugin without success.

Thank you for your help.

A pseudo playbook:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 26, 2022, 2:44pm UTC](https://forum.ansible.com/t/access-group-members-of-a-variable-group-name/36250/2 "2022-01-26T14:44:59Z")

</div>

Try the below suggestions

> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proj: 'website'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subproj: 'front'  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "Show group members"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: msg=?? # Something like: {{groups[{{proj}}-{{subproj}}-lb]}}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: var=groups[proj]~'-'~subproj~'-lb']

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "Conditional"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do something  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in {{groups[{{proj}}-{{subproj}}-lb]}}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "inventory\_hostname in groups[proj ~ '-' ~ subproj ~ '-lb']"

---

<div class="post-metadata">

**Author:** ![Roshani](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Roshani](https://forum.ansible.com/u/Roshani)\
**Post date:** [February 8, 2022, 12:44pm UTC](https://forum.ansible.com/t/access-group-members-of-a-variable-group-name/36250/3 "2022-02-08T12:44:02Z")

</div>

Thank you so much dick.  
It worked flawlessly.

There’s a just a typo:  
the extra closing bracket should be removed after proj:  
debug: var=groups[proj\*\*]\*\*~‘-’~subproj~‘-lb’] ------\> debug: var=groups[proj~‘-’~subproj~‘-lb’]
