# inventory\_groupname variable (similar to inventory\_hostname)

**URL:** <https://forum.ansible.com/t/inventory-groupname-variable-similar-to-inventory-hostname/25133>\
**Category:** Ansible Project\
**Created:** [July 31, 2017, 2:49am UTC](https://forum.ansible.com/t/inventory-groupname-variable-similar-to-inventory-hostname/25133 "2017-07-31T02:49:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darshan\_Shroff](https://avatars.discourse-cdn.com/v4/letter/d/71c47a/32.png) [@Darshan\_Shroff](https://forum.ansible.com/u/Darshan_Shroff)\
**Post date:** [July 31, 2017, 2:49am UTC](https://forum.ansible.com/t/inventory-groupname-variable-similar-to-inventory-hostname/25133/1 "2017-07-31T02:49:02Z")

</div>

Ansible FAQ clarifies  
“‘inventory\_hostname’ is a magic variable that indicates the current host you are looping over in the host loop.”

Is there a similar concept of **inventory\_groupname** that can be made available to be used inside a playbook ?

e.g. If we are running a playbook in this manner

ansible-playbook -i inventory/prod site.yml --limit service\_a

And ‘prod’ inventory is defined as below

[service\_a]

host1

host2

[service\_b]

host2

host3

Then is it feasible to reference the group name - service\_a - inside site.yml in this manner - {{ inventory\_groupname }}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2017, 9:18am UTC](https://forum.ansible.com/t/inventory-groupname-variable-similar-to-inventory-hostname/25133/2 "2017-07-31T09:18:26Z")

</div>

> Ansible FAQ clarifies  
> "‘inventory\_hostname’ is a magic variable that indicates the current host  
> you are looping over in the host loop."
> 
> Is there a similar concept of \*inventory\_groupname\* that can be made  
> available to be used inside a playbook ?

Unfortunately not.

> e.g. If we are running a playbook in this manner
> 
> ansible-playbook -i inventory/prod site.yml --limit service\_a
> 
> And 'prod' inventory is defined as below
> 
> [service\_a]  
> host1  
> host2
> 
> [service\_b]  
> host2  
> host3
> 
> Then is it feasible to reference the group name - service\_a - inside  
> site.yml in this manner - {{ inventory\_groupname }}

You'll need to provide that with --extra-vars

ansible-playbook -i inventory/prod site.yml --limit service\_a --extra-vars mylimit=service\_a

I think this should work so you don't have to specify both limit and mylimit

ansible-playbook -i inventory/prod site.yml -e mylimit=service\_a

site.yml
