ansible hosts file - group_names issue

16 Views Asked by At

following is the content of my "hosts" file. how to print which groups "web_svr1" is part of? it should print all_servers and web_servers.

[web_servers]
web_svr1
web_svr2

[app_servers]
app_svr1

[all_servers:children]
web_servers
app_servers

I tried below code to print group names but it doesn't work.

  - name: Debug task to print group names
    ansible.builtin.debug:
      msg: "Host belongs to group:"
    loop: "{{ group_names }}"
1

There are 1 best solutions below

0
code.coderpy On

i figured it out, {{ item }} will iterate through group_names

msg: "Host belongs to group: {{ item }}"

group_names[0] will be all_servers group_names[1] will be web_servers