# How to get the first group defined in inventory for current inventory\_hostname involved in multiple groups

**URL:** <https://forum.ansible.com/t/how-to-get-the-first-group-defined-in-inventory-for-current-inventory-hostname-involved-in-multiple-groups/22230>\
**Category:** Ansible Project\
**Created:** [August 7, 2016, 2:22pm UTC](https://forum.ansible.com/t/how-to-get-the-first-group-defined-in-inventory-for-current-inventory-hostname-involved-in-multiple-groups/22230 "2016-08-07T14:22:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [August 7, 2016, 2:22pm UTC](https://forum.ansible.com/t/how-to-get-the-first-group-defined-in-inventory-for-current-inventory-hostname-involved-in-multiple-groups/22230/1 "2016-08-07T14:22:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 7, 2017, 7:04pm UTC](https://forum.ansible.com/t/how-to-get-the-first-group-defined-in-inventory-for-current-inventory-hostname-involved-in-multiple-groups/22230/2 "2017-03-07T19:04:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fabrice.perko](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fabrice.perko](https://forum.ansible.com/u/fabrice.perko)\
**Post date:** [March 8, 2017, 9:11am UTC](https://forum.ansible.com/t/how-to-get-the-first-group-defined-in-inventory-for-current-inventory-hostname-involved-in-multiple-groups/22230/3 "2017-03-08T09:11:08Z")

</div>

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
