How to get the first group defined in inventory for current inventory_hostname involved in multiple groups

263 views
Skip to first unread message

jean-christophe manciot

unread,
Aug 7, 2016, 10:22:13 AM8/7/16
to Ansible Project
Ansible 2.1.1.0

Several hierarchical groups can be defined for a particular device in inventory.
Since hostvars[inventory_hostname].group_names lists all device groups listed in alphabetical order, hostvars[inventory_hostname].group_names[0] does not necessarily point to the right answer.

Does someone have a suggestion?

Brian Coca

unread,
Mar 7, 2017, 2:05:03 PM3/7/17
to Ansible Project
Outside of inventory group hierarchy doesn't exist, in inventory it
disappears after vars are loaded and hosts are assigned.


----------
Brian Coca

Fabrice Perko

unread,
Mar 8, 2017, 4:11:08 AM3/8/17
to Ansible Project
Hello Jean-Christophe

I use Ansible for 1 year now, and in my experience:
- the multiple inheritance is absolutely to be avoided.
- you can make simple inheritance on several levels (a host is part of a group that is part of a group, etc.)
- when multiple inheritance is really needed, I create several inventories, with some groups used in both inventories, ans some other not. That way I can share some vars.

I don't know what is your final need, but I'm pretty sure you can achieve it otherway.

Regarding your specific needs, I would create a variable (ex: nom_groupe) in the differents groups, whose value would be overloaded.

Regards,

Fabrice Perko
Reply all
Reply to author
Forward
0 new messages