# magic variable for target group?

**URL:** <https://forum.ansible.com/t/magic-variable-for-target-group/34264>\
**Category:** Ansible Project\
**Created:** [December 1, 2020, 4:41pm UTC](https://forum.ansible.com/t/magic-variable-for-target-group/34264 "2020-12-01T16:41:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Matz1](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guy\_Matz1](https://forum.ansible.com/u/Guy_Matz1)\
**Post date:** [December 1, 2020, 4:41pm UTC](https://forum.ansible.com/t/magic-variable-for-target-group/34264/1 "2020-12-01T16:41:20Z")

</div>

Hi! Does anyone know if there’s a variable set for the target group of a play?

For example, for this:

- hosts: **prime**  
roles:

- nginx

- postfix

Does any variable get set to “prime” in a deterministic way?

Thanks!  
Guy

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 1, 2020, 6:15pm UTC](https://forum.ansible.com/t/magic-variable-for-target-group/34264/2 "2020-12-01T18:15:55Z")

</div>

No. There is no such "special variable"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

There is only variable \*group\_names\*

&nbsp;&nbsp;"List of groups the current host is part of"

and variable "groups"

&nbsp;&nbsp;"A dictionary/map with all the groups in inventory and each group  
&nbsp;&nbsp;has the list of hosts that belong to it"

Ansible doesn't store the structure of inventory groups. Quoting from  
"How variables are merged"  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#how-variables-are-merged](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-variables-are-merged)

&nbsp;&nbsp;"... This keeps Ansible focused on the Host and Task, so groups  
&nbsp;&nbsp;don’t really survive outside of inventory and host matching. ..."

Do you have any use-case which needs such variable?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 1, 2020, 9:40pm UTC](https://forum.ansible.com/t/magic-variable-for-target-group/34264/3 "2020-12-01T21:40:01Z")

</div>

> \> \> Hi! Does anyone know if there's a variable set for the target group of a  
> \> \> play?  
> \> \> For example, for this:  
> \> \> - hosts: \*prime\*  
> \> \> roles:  
> \> \> - nginx  
> \> \> - postfix  
> \> \> Does any variable get set to "prime" in a deterministic way?

> \> No. There is no such "special variable"  
> \> [https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)  
> \>  
> \> ... variable "groups"  
> \>  
> \> "A dictionary/map with all the groups in inventory and each group  
> \> has the list of hosts that belong to it"  
> \> ...  
> \> Do you have any use-case which needs such variable?

> Yes. I would like to have common roles, e.g. httpd, that could be  
> "app-aware" based on the target group.

What do you mean by "'app-aware' role based on the target group"?
