# Iterate over hosts in a group with index

**URL:** <https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205>\
**Category:** Ansible Project\
**Created:** [July 27, 2013, 4:21am UTC](https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205 "2013-07-27T04:21:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Spaulding](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Matt\_Spaulding](https://forum.ansible.com/u/Matt_Spaulding)\
**Post date:** [July 27, 2013, 4:21am UTC](https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205/1 "2013-07-27T04:21:25Z")

</div>

Hi all,

So I’m trying to figure out how I can iterate over the hosts in a group along with an index value. I’m trying to do something like this:

- name: My Task  
shell: echo Host Number {{ item }} is {{ groups.mygroup[item] }}  
with\_sequence: start=0 end={{ groups.mygroup|count - 1 }}

But it seems that ‘groups.mygroup[item]’ does not get evaluated like I expect it to.

Any way to do this?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 27, 2013, 2:42pm UTC](https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205/2 "2013-07-27T14:42:51Z")

</div>

Probably because the sequence plugin is returning strings and you have to cast them, but we generally don’t like it when people have to program.

Things should be easier, as well as much more readable.

I would suggest we create a lookup plugin called “with\_indexed\_items”, so it could be as simple as:

- name: My Task  
shell: echo Host Number {{ item.0 }} is {{ item.1 }}  
with\_indexed\_items: groups.mygroup

It would be a small copy of “with\_items” with a one or two line change to get there.

---

<div class="post-metadata">

**Author:** ![Matt\_Spaulding](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Matt\_Spaulding](https://forum.ansible.com/u/Matt_Spaulding)\
**Post date:** [July 28, 2013, 6:06pm UTC](https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205/3 "2013-07-28T18:06:20Z")

</div>

I really like that idea, but with a small change:

- name: My Task  
shell: echo Host Number {{ item.index }} is {{ item.value }}  
with\_indexed\_items: groups.mygroup

What do you think? That way it’s clear which value is the index and which is the value from the items being iterated over. I’ve tweaked the ‘with\_items’ lookup plugin to do this and pretty happy with the result. Much cleaner than what I was originally attempting 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 28, 2013, 7:14pm UTC](https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205/4 "2013-07-28T19:14:55Z")

</div>

Yeah, I am not a fan of this because we already have a precedence for returning indexes when used with “with\_nested” and others, plus it would be consistent with “enumerate()” in Python.

---

<div class="post-metadata">

**Author:** ![Matt\_Spaulding](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Matt\_Spaulding](https://forum.ansible.com/u/Matt_Spaulding)\
**Post date:** [July 28, 2013, 9:33pm UTC](https://forum.ansible.com/t/iterate-over-hosts-in-a-group-with-index/13205/5 "2013-07-28T21:33:56Z")

</div>

Agreed. Consistency is generally a good idea. I’ve submitted a PR with the change you originally suggested.
