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.
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"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With