# Get groupname(s) from inventory

**URL:** <https://forum.ansible.com/t/get-groupname-s-from-inventory/20579>\
**Category:** Ansible Project\
**Created:** [February 15, 2016, 6:55am UTC](https://forum.ansible.com/t/get-groupname-s-from-inventory/20579 "2016-02-15T06:55:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rene\_Gallati](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@Rene\_Gallati](https://forum.ansible.com/u/Rene_Gallati)\
**Post date:** [February 15, 2016, 6:55am UTC](https://forum.ansible.com/t/get-groupname-s-from-inventory/20579/1 "2016-02-15T06:55:06Z")

</div>

Hello all,

is it possible to get the groupname(s) where a host is placed from inventory to use later? I have a situation like this:

`  
[setA]  
HostA  
HostB

[setB]  
HostC  
HostD

[setC]  
HostE  
HostF  
`

So when I’m running on HostC, it should somehow know it is in setB so that I can iterate over all other hosts in the same group and do something specific with that.  
I know how to do it manually, but it looks very redundant in the config file to add a custom variable like

`  
[setN]  
HostXX set\_name=setN  
HostXY set\_name=setN

`

just to be able to get at the group name.

Thanks for any hints

---

<div class="post-metadata">

**Author:** ![dennis.benzinger](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dennis.benzinger](https://forum.ansible.com/u/dennis.benzinger)\
**Post date:** [February 15, 2016, 5:10pm UTC](https://forum.ansible.com/t/get-groupname-s-from-inventory/20579/2 "2016-02-15T17:10:52Z")

</div>

Hello René,

you can use the group\_names and groups variables: [http://docs.ansible.com/ansible/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

Hope this helps,  
Dennis Benzinger | SAP Hybris

---

<div class="post-metadata">

**Author:** ![Rene\_Gallati](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@Rene\_Gallati](https://forum.ansible.com/u/Rene_Gallati)\
**Post date:** [February 19, 2016, 11:10pm UTC](https://forum.ansible.com/t/get-groupname-s-from-inventory/20579/3 "2016-02-19T23:10:13Z")

</div>

Thanks, that was exactly what I needed! I seem to have skipped over that part as I’ve read that page before. In any case, I can do now what I need. Thanks!
