# Is there a way to access the host index in a group?  I.e. want number in the hosts group am i?

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693>\
**Category:** Ansible Project\
**Created:** [November 5, 2015, 4:43am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693 "2015-11-05T04:43:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [November 5, 2015, 4:43am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/1 "2015-11-05T04:43:06Z")

</div>

I’m sure this must’ve come up before but I couldn’t find an answer.

I have a list of hosts in a group. I want to write an index number on each of those hosts in a file. E.g. the first host has a file that contains 1, the second host has a file that contains 2 etc.

Now I know I can access the hosts using the groups variable E.g. groups[“mygroup”] but i can’t find any reference to an index so the host can find out which number it is.

Example play

…

- name: Add instance to new host group

add\_host:

name: “{{ item }}”

group: zk

with\_items:

- server1

- server2

- server3

- name: deploy to zk group

hosts: zk

tasks:

- name: write a myid file based on the position in the group

copy:

content: {{ some\_magic\_variable??? }}

dest: /zookeeper-data/myid

In the above server i would expect to end up with three servers each with a myid file that contains 0, 1 or 2

Anyone know if that magic variable exists?

---

<div class="post-metadata">

**Author:** ![Mirko\_Friedenhagen](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mirko\_Friedenhagen](https://forum.ansible.com/u/Mirko_Friedenhagen)\
**Post date:** [November 5, 2015, 5:29am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/2 "2015-11-05T05:29:14Z")

</div>

Hello Rob,

maybe [http://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-a-list-with-an-index](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-a-list-with-an-index)  
might help.

Regards  
Mirko  
Regards Mirko

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [November 5, 2015, 5:37am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/3 "2015-11-05T05:37:37Z")

</div>

Unfortunately not. I’m not iterating over a list. Ansible is just making it’s way through a group of hosts so i need Ansible to tell me which host it is operating on.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 5, 2015, 12:26pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/4 "2015-11-05T12:26:41Z")

</div>

A few ways potentially:

{{ play\_hosts.index(inventory\_hostname) }}

If you want to know host index within a play

Or:

{{ groups.somegroup.index(inventory\_hostname) }}

For getting the index of a host within a group.

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [November 6, 2015, 6:22am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/5 "2015-11-06T06:22:38Z")

</div>

Awesome Matt!

I knew it had to exist somewhere.

Thanks for sharing.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Gaglione](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Giovanni\_Gaglione](https://forum.ansible.com/u/Giovanni_Gaglione)\
**Post date:** [September 7, 2017, 8:22pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/6 "2017-09-07T20:22:51Z")

</div>

Hi Matt,

This solution does not work well when a group has multiple hostname that are all the same.

E.g.

> [my-hosts]  
> 192.168.1.1  
> 192.168.1.1  
> 192.168.1.1  
> 192.168.1.1

In this case, do you recommend a way to get the index of the current host?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Gaglione](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@Giovanni\_Gaglione](https://forum.ansible.com/u/Giovanni_Gaglione)\
**Post date:** [September 7, 2017, 9:05pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-the-host-index-in-a-group-i-e-want-number-in-the-hosts-group-am-i/19693/7 "2017-09-07T21:05:41Z")

</div>

I’ve solved it this way - [https://github.com/ansible/ansible/issues/12086#issuecomment-327927275](https://github.com/ansible/ansible/issues/12086#issuecomment-327927275)
