There is also another way where you process the file as text file with bash commands like awk
If file is with contents like
cat /tmp/test.ini
[group1]
host1
host2
[group2]
host3
host4
[first_two_groups:children]
group1
group2
[group3]
host5
host6
[group4]
host7
host8
Then you can use one command like this for when file has Linux EOL:
awk "/first_two_groups\:children/,/^$/" /tmp/test.ini | grep -v children
group1
group2
Where group to find children of is called first_two_groups
and it can be anywhere in the file. Command works as long as after the group definition there is empty line which is used as anchor to end the awk stream. I think most people add empty line in an ansible inventory file for readability and we can leverage that fact.
If the file happens to have Windows EOL then command is
awk "/first_two_groups\:children/,/^[\\r]$/" /tmp/test.ini | grep -v children
with ansible example something like this:
- name: get group children
shell: awk "/first_two_groups\:children/,/^$/" /tmp/test.ini | grep -v children
register: chlldren
#returns list of the children groups to loop through later
- debug: var=children.stdout_lines
Above is not fully tested but if an issue then it could be at the most with the escaping of special chars in the shell module. REMEMBER - when you pass special chars in ansible shell module like colon use jinja expansion - instead of :
use {{ ':' }}
Also for consistent bash behavior is recommended to pass explicitly the executable by tacking at the end of the task
args:
executable: /bin/bash
with_items: "{{ groups['satellites'] }}"
? – MarcheseHow can I get a list of the groups that have satellites as their parent?
, it will not give the groups. It give the hosts inside that group – Kristiankristiansand