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

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?

Outside of inventory group hierarchy doesn't exist, in inventory it
disappears after vars are loaded and hosts are assigned.

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