# Looping over a set of host in a playbook?

**URL:** <https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012>\
**Category:** Ansible Project\
**Created:** [April 14, 2014, 3:59pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012 "2014-04-14T15:59:15Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 14, 2014, 3:59pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/1 "2014-04-14T15:59:15Z")

</div>

Hello,

I’m trying to loop over a set of host, get their IP and then append the result to a file.

Currently the action looks like this:

- name: build rings  
command: swift-ring-builder {{ item.service }}.builder add z1-{{ hostvars[inventory\_hostname][“ansible\_bond1.2108”].ipv4.address }}:{{ item.port }}/sdb1 100  
chdir=/etc/swift  
with\_items:
- { service: ‘account’, port: ‘6002’ }
- { service: ‘container’, port: ‘6001’ }
- { service: ‘object’, port: ‘6000’ }
- groups.storages

Basically I’d like to look over ‘groups.storages’ in {{ hostvars[inventory\_hostname][“ansible\_bond1.2108”].ipv4.address }.

Is it doable?

This looks like this [https://coderwall.com/p/w5o6eq](https://coderwall.com/p/w5o6eq), althought it doesn’t seem to work with the new convention “{{ }}”

Thanks in advance :).

Cheers!

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 16, 2014, 8:42am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/2 "2014-04-16T08:42:49Z")

</div>

Up?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 17, 2014, 2:21am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/3 "2014-04-17T02:21:33Z")

</div>

Seems like you _might_ mean with\_nested seeing you are looping over what appears to be two different lists.

Or else “groups.storages” is something else. That looks like the odd one out to me.

Everything else looks fine, so when you say “doesn’t seem to work”, more info would be helpful about how it was not working?

Thanks!

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 17, 2014, 8:16am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/4 "2014-04-17T08:16:09Z")

</div>

Hello Michael,

Thanks for your response.

What I’m trying to achieve is exactly what is described in this post: [https://coderwall.com/p/w5o6eq](https://coderwall.com/p/w5o6eq)

As an example, I tried the following:

- name: iterate over hosts  
command: echo {{ hostvars.{{ item }}.ansible\_hostname }}  
with\_items:
- groups.storages  
ignore\_errors: true  
tags: gz

As far as I understand this is supposed to return every hostname, instead I get:

changed: [[ceph001.enocloud.com](http://ceph001.enocloud.com)] =\> (item=groups.storages) =\> {“changed”: true, “cmd”: [“echo”, “{{hostvars.{{item}}.ansible\_hostname}}”], “delta”: “0:00:00.003401”, “end”: “2014-04-17 10:11:03.764429”, “item”: “groups.storages”, “rc”: 0, “start”: “2014-04-17 10:11:03.761028”, “stderr”: “”, “stdout”: “{{hostvars.{{item}}.ansible\_hostname}}”}

Any idea? Furthermore, the final goal is to collect the ip address of the following intertace: ansible\_bond1.2108. Not sure if it’s reachable given this: [https://github.com/ansible/ansible/issues/6879](https://github.com/ansible/ansible/issues/6879)

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 17, 2014, 11:52am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/5 "2014-04-17T11:52:00Z")

</div>

Got it! This is the problem line:

command: echo {{ hostvars.{{ item }}.ansible\_hostname }}

It should look like this:

{{ hostvars[item].ansible\_hostname }}

Basically templates don’t nest, and inside of template expressions, you can just use variables mostly as if they were Python.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 17, 2014, 4:05pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/6 "2014-04-17T16:05:32Z")

</div>

Thanks! This is getting better and better:

Modifying my playbook like this:

- name: iterate over hosts  
command: ping -c 1 {{ hostvars[item][“ansible\_bond1.2108”].ipv4.address }}  
with\_items: groups.storages  
ignore\_errors: true  
tags: gz

Now I get:

\< TASK: swift-proxy | iterate over hosts \>

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 17, 2014, 10:13pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/7 "2014-04-17T22:13:47Z")

</div>

“ansible\_bond1.2108”

This part needs quotes around it.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 18, 2014, 8:12am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/8 "2014-04-18T08:12:31Z")

</div>

I’m confused, quotes are already there.

command: ping -c 1 {{ hostvars[item][“ansible\_bond1.2108”].ipv4.address }}

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 22, 2014, 9:02pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/9 "2014-04-22T21:02:53Z")

</div>

No more ideas?

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [April 22, 2014, 10:13pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/10 "2014-04-22T22:13:26Z")

</div>

Try:

{{ hostvars[item][‘ansible\_bond1.2108’][‘ipv4’][‘address’] }}

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 23, 2014, 8:17am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/11 "2014-04-23T08:17:48Z")

</div>

Same error ☹

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [April 23, 2014, 8:29am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/12 "2014-04-23T08:29:30Z")

</div>

Could you try instead of command module something like:

debug: msg=“{{ hostvars[item][‘ansible\_bond1.2108’][‘ipv4’][‘address’] }}”

And run ansible-playbook with **-vvvv.**

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 23, 2014, 8:42am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/13 "2014-04-23T08:42:40Z")

</div>

Thanks for you help, this is what I got:

[http://pastebin.com/qfLyS9yi](http://pastebin.com/qfLyS9yi)

Cheers.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [April 23, 2014, 9:00am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/14 "2014-04-23T09:00:56Z")

</div>

You didn’t understand me, I would like you to replaced the whole “command: ping -c 1 {{ hostvars[item][“ansible\_bond1.2108”].ipv4.address }}” line with the debug line I wrote in my previous reply.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 23, 2014, 9:21am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/15 "2014-04-23T09:21:20Z")

</div>

Arf sorry, I read too fast.

[http://pastebin.com/H1Vzmb7w](http://pastebin.com/H1Vzmb7w)

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [April 23, 2014, 9:40am UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/16 "2014-04-23T09:40:47Z")

</div>

I don’t understand, it’s like it is not recognizing the **hostvars** keyword?

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 23, 2014, 1:34pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/17 "2014-04-23T13:34:47Z")

</div>

I don’t know 😕

---

<div class="post-metadata">

**Author:** ![Sebastien\_Han](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sebastien\_Han](https://forum.ansible.com/u/Sebastien_Han)\
**Post date:** [April 25, 2014, 2:07pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/18 "2014-04-25T14:07:47Z")

</div>

No one?

---

<div class="post-metadata">

**Author:** ![Stefan\_Mojsilovic](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Stefan\_Mojsilovic](https://forum.ansible.com/u/Stefan_Mojsilovic)\
**Post date:** [October 1, 2014, 3:04pm UTC](https://forum.ansible.com/t/looping-over-a-set-of-host-in-a-playbook/15012/19 "2014-10-01T15:04:20Z")

</div>

Had the same problem. Managed to solve it using this way to access the value:

debug: msg=“{{ hostvars[item][‘ansible\_default\_ipv4’][‘address’] }}”  
with\_items: groups.web\_servers
