Access group members of a variable group name

87 views
Skip to first unread message

Roshani

unread,
Jan 26, 2022, 9:29:42 AM1/26/22
to Ansible Project
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:

---
- hosts: all

  # inventory:
  # [website-front-lb]
  # 192.168.1.2
  # 192.168.1.3

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

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



Dick Visser

unread,
Jan 26, 2022, 9:45:24 AM1/26/22
to ansible...@googlegroups.com
Try the below suggestions

On Wed, 26 Jan 2022 at 15:29, Roshani <m.ro...@gmail.com> wrote:

> 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' ]"


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Roshani

unread,
Feb 8, 2022, 10:39:37 AM2/8/22
to Ansible Project
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']
Reply all
Reply to author
Forward
0 new messages