# Magic Variables: To access host within a group

**URL:** <https://forum.ansible.com/t/magic-variables-to-access-host-within-a-group/11588>\
**Category:** Ansible Project\
**Created:** [May 30, 2012, 1:25pm UTC](https://forum.ansible.com/t/magic-variables-to-access-host-within-a-group/11588 "2012-05-30T13:25:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kumar3](https://avatars.discourse-cdn.com/v4/letter/k/ebca7d/32.png) [@Kumar3](https://forum.ansible.com/u/Kumar3)\
**Post date:** [May 30, 2012, 1:25pm UTC](https://forum.ansible.com/t/magic-variables-to-access-host-within-a-group/11588/1 "2012-05-30T13:25:27Z")

</div>

Is there any way to access the list of hosts within each  
"groups" (magic) variable ?

{% if 'webserver' in groups %}  
&nbsp;&nbsp;&nbsp;# some part of a configuration file that only applies to webservers  
{% endif %}

Kumar

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 30, 2012, 1:42pm UTC](https://forum.ansible.com/t/magic-variables-to-access-host-within-a-group/11588/2 "2012-05-30T13:42:44Z")

</div>

Yep.

groups is already a (Python) list in this example, this example is a test to see if an item is in a list.

---

<div class="post-metadata">

**Author:** ![Kumar3](https://avatars.discourse-cdn.com/v4/letter/k/ebca7d/32.png) [@Kumar3](https://forum.ansible.com/u/Kumar3)\
**Post date:** [May 30, 2012, 2:15pm UTC](https://forum.ansible.com/t/magic-variables-to-access-host-within-a-group/11588/3 "2012-05-30T14:15:52Z")

</div>

Thanks Michael:

&nbsp;&nbsp;I think I was not clear in my prior email. In the example below, I  
am trying to print all the host name associated to the group  
'webserver'.  
I am assuming there might be a method on each group element in the  
list or some other way to access the list of hosts name associated to  
a group in the  
inventory file. I would like the script to dynamically pull all the  
hosts associated to a group.

Thanks,

Inventory file:

[webservers]  
foo.example.com  
bar.example.com  
xyz.example.com

{% if 'webserver' in groups %}

&nbsp;&nbsp;# print all the host names associated with the webservers groups  
&nbsp;&nbsp;# i.e foo.example.com, bar.example.com, xyz.example.com

{% endif %}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 30, 2012, 2:28pm UTC](https://forum.ansible.com/t/magic-variables-to-access-host-within-a-group/11588/4 "2012-05-30T14:28:28Z")

</div>

> I am assuming there might be a method on each group element in the  
> list or some other way to access the list of hosts name associated to  
> a group in the  
> inventory file. I would like the script to dynamically pull all the  
> hosts associated to a group.

Sounds like you are trying to read this from another application.

The most obvious answer here is you could use the ansible inventory API.
