Access group members of a variable group name

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:

Try the below suggestions

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

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

          - name: "Conditional"
            do something
            when: inventory_hostname in {{groups[{{proj}}-{{subproj}}-lb]}}

             when: "inventory_hostname in groups[proj ~ '-' ~ subproj ~ '-lb' ]"

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’]