Lets say I have this type of inventory which consists similar names for subgroups under the main group but with different server ip's at the end. The names are similar because thats the naming convention that I would like to use and I want only the server ip's at the end to be different.
[maingroup]
group1
group2
[group1]
subgroup1
subgroup2
subgroup3
[group2]
subgroup1
subgroup2
subgroup3
[subgroup1]
server1
server2
server3
[subgroup2]
server4
server5
server6
I would like to have a python/shell script which accepts some params e.g:
python test.py maingroup group1 subgroup1
then run an ansible role on the specific host while passing vars(not sure if correct):
ansible-playbook -i host.yml --extra-vars "variable_maingroup=maingroup variable_group=group variable_subgroup=subgroup"
I would like to make sure that I'm running the ansible only on servers with this path: maingroup>group1>subgroup1 and pass it to my ansible role as hosts to run on.
So my question is, how I would be able to target only the servers in subgroup1 which is under maingroup.group1 same as the order of params passed to script and not in group2 and what do I need to change in my task to pass this host as var?
I know I could pass the host as an extra variable but how I would be able to achieve this specific structure?
had something like this in mind:
- hosts: {{ hostvars[groups['maingroup']['group1']['subgroup1'] }}
Thanks alot!