# Position of a host inside an inventory group

**URL:** <https://forum.ansible.com/t/position-of-a-host-inside-an-inventory-group/19220>\
**Category:** Ansible Project\
**Created:** [September 10, 2015, 3:06am UTC](https://forum.ansible.com/t/position-of-a-host-inside-an-inventory-group/19220 "2015-09-10T03:06:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Comisario](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Alejandro\_Comisario](https://forum.ansible.com/u/Alejandro_Comisario)\
**Post date:** [September 10, 2015, 3:06am UTC](https://forum.ansible.com/t/position-of-a-host-inside-an-inventory-group/19220/1 "2015-09-10T03:06:12Z")

</div>

Hi everyone, so happy to be here, this is my first post.  
Here’s what i need.

I have two groups:

[group\_master]  
host1  
host2  
host3

[group\_slaves]  
host4  
host5  
host6

i need to create a template that reflect a pair of “couples” like  
host1 and host4  
host2 and host4  
host3 and host6

for that i thought going through the **group\_master** and then do something like this on the template.

possition\_in\_the\_group = ANSIBLE\_VARIABLE\_THAT\_DONT\_KNOW\_IF\_EXISTS  
master = {{ ansible\_hostname }}  
slave = {{ groups[‘group\_slaves’][possition\_in\_the\_group] }}

so, can i know through a simple variable and not having to jinja “script” to know it, the position of the host im running the task on, has in the group it belongs ?  
thanks so much !

---

<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:** [September 10, 2015, 1:35pm UTC](https://forum.ansible.com/t/position-of-a-host-inside-an-inventory-group/19220/2 "2015-09-10T13:35:22Z")

</div>

i think you want:  
&nbsp;&nbsp;&nbsp;&nbsp;groups['group\_slaves'][groups['group\_masters].index(inventory\_hostname)]

supposing you are using hosts: group\_masters, reverse it if you are  
looping through slaves.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Comisario](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Alejandro\_Comisario](https://forum.ansible.com/u/Alejandro_Comisario)\
**Post date:** [September 10, 2015, 2:43pm UTC](https://forum.ansible.com/t/position-of-a-host-inside-an-inventory-group/19220/3 "2015-09-10T14:43:10Z")

</div>

Brian, **that’s exactly what i needed !!**  
thanks so much !
