Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Get groups specific subgroups

I have the following groups defined in my inventory:

[webservers]

[server_storage]

[server_ws1]

[webservers:children]
server_storage
server_ws1

During my play, I'm in need of getting the names of the children groups of group['webservers'] ONLY (I think of theese as 'subgroups')

So, let's say I would need to set_fact a variable that contains in this case, the list of strings:

 - server_storage
 - server_ws1

This would have to be dynamic, so if I add group 'server_ws2' to group['webservers'], this should return

 - server_storage
 - server_ws1
 - server_ws2

I've been playing with the use of group_names, group['webservers'] (which doesn't return subgroups, but hostnames)

Basically, I need a simple way of getting a the list of subgroups of a specific group. Is this possible without the use of black magic?

UPDATE: The idea, is that the hosts could belong to more groups, but I need only the subgroups or children of webservers group. It's constant, no matter the host, the output should allways be the same.

By the way, this one didn't work How can I get a list of child groups in Ansible?, because it retuns all groups for the current host, I need only subgroups of the specified group.

like image 289
ndelucca Avatar asked Jun 04 '26 07:06

ndelucca


1 Answers

Q: "Get the names of the children groups of group['webservers']"

A: For example, the inventory

shell> cat hosts
[webservers]

[server_storage]
host_01

[server_ws1]
host_02

[webservers:children]
server_storage
server_ws1

and the playbook

shell> cat playbook.yml
- hosts: webservers
  tasks:

    - set_fact:
        subgroups: "{{ subgroups|default([]) + hostvars[item].group_names }}"
      loop: "{{ groups.webservers }}"
      run_once: true

    - debug:
        msg: "{{ subgroups|unique|difference(['webservers']) }}"
      run_once: true

give (abridged)

shell> ansible-playbook -i hosts playbook.yml 

  msg:
  - server_storage
  - server_ws1

As a workaround, it's possible to use ansible-inventory to list the structure of the inventory, store the output in a file, and use this file as needed, e.g. include_vars in a playbook. See the example below

shell> cat hosts
host_01
host_02

[webservers]

[server_storage]

[server_ws1]

[webservers:children]
server_storage
server_ws1
shell> ansible-inventory -i hosts --list --output hosts.json
shell> cat hosts.json
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped",
            "webservers"
        ]
    },
    "ungrouped": {
        "hosts": [
            "host_01",
            "host_02"
        ]
    },
    "webservers": {
        "children": [
            "server_storage",
            "server_ws1"
        ]
    }
like image 108
Vladimir Botka Avatar answered Jun 07 '26 10:06

Vladimir Botka