# Cycle hosts in tasks

**URL:** <https://forum.ansible.com/t/cycle-hosts-in-tasks/11871>\
**Category:** Ansible Project\
**Created:** [September 7, 2012, 4:00pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871 "2012-09-07T16:00:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Vito\_Moscarito](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Marco\_Vito\_Moscarito](https://forum.ansible.com/u/Marco_Vito_Moscarito)\
**Post date:** [September 7, 2012, 4:00pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/1 "2012-09-07T16:00:03Z")

</div>

Hello, I'm try to generate some task using data from other host (eg.:  
open port for internal application only to host in the same group):

hosts file (PS: only test, i use racksace inventory, so this is not static):  
---8\<-------8\<-------8\<-------8\<-------8\<----  
[mongodb]  
192.168.10.1  
192.168.10.2  
192.168.10.3  
192.168.10.4  
---8\<-------8\<-------8\<-------8\<-------8\<----

Into playbook I try to cycle using hosts but i don't find how to do this:

---8\<-------8\<-------8\<-------8\<-------8\<----  
- hosts: mongodb  
- name: open firewall (MongoDB)  
&nbsp;&nbsp;action: command /usr/sbin/ufw allow from $item app MongoDB  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- @HELP\_ME\_PLEASE\_:)\_@  
&nbsp;&nbsp;&nbsp;&nbsp;# - ${groups.mongodb}  
&nbsp;&nbsp;&nbsp;&nbsp;# - ${ansible\_eth1.ipv4.address}  
---8\<-------8\<-------8\<-------8\<-------8\<----

I know that's possible use groups in template files, but is not  
possible have this information in tasks? Is required to create a bash  
script using template and execute it?

Thank you!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marco

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [September 7, 2012, 4:18pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/2 "2012-09-07T16:18:41Z")

</div>

Just get rid of the whole with\_items clause. Each host is cycled over each task that passes any filters by default.

\<tim/\>

---

<div class="post-metadata">

**Author:** ![Marco\_Vito\_Moscarito](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Marco\_Vito\_Moscarito](https://forum.ansible.com/u/Marco_Vito_Moscarito)\
**Post date:** [September 7, 2012, 4:36pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/3 "2012-09-07T16:36:57Z")

</div>

Hello Timothy  
&nbsp;&nbsp;thanks for quick reply.

I need to execute this task N-time in each hosts, so (using previous  
example) will "converted" to:

- hosts: mongodb  
- name: open firewall (MongoDB)  
&nbsp;&nbsp;action: command /usr/sbin/ufw allow from $item app MongoDB  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- 192.168.10.1  
&nbsp;&nbsp;&nbsp;&nbsp;- 192.168.10.2  
&nbsp;&nbsp;&nbsp;&nbsp;- 192.168.10.3  
&nbsp;&nbsp;&nbsp;&nbsp;- 192.168.10.4

this commands are executed:

(in 192.168.10.1):  
/usr/sbin/ufw allow from 192.168.10.1 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.2 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.3 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.4 app MongoDB

(in 192.168.10.2):  
/usr/sbin/ufw allow from 192.168.10.1 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.2 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.3 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.4 app MongoDB

(in 192.168.10.3):  
/usr/sbin/ufw allow from 192.168.10.1 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.2 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.3 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.4 app MongoDB

(in 192.168.10.4):  
/usr/sbin/ufw allow from 192.168.10.1 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.2 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.3 app MongoDB  
/usr/sbin/ufw allow from 192.168.10.4 app MongoDB

Bye  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marco

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [September 7, 2012, 4:59pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/4 "2012-09-07T16:59:32Z")

</div>

Sorry I misunderstood what you were trying to do.

In Advanced Playbooks there is a section about Accessing Info About Other Hosts that mentions a groups list, but I think that's only for Jinja templates and is not a variable accessible by playbooks. Maybe I misreading the docs. That would be you ideal solution. with\_items: groups['mongodb']

Assuming that isn't an optionI'd create a perform a local task before the one you have that returns a list of the hosts as an ansible\_fact and then reference that with the with\_items directive. with\_items: ${hostvars.localhosts.hosts\_list}

\<tim/\>

---

<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:** [September 7, 2012, 5:18pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/5 "2012-09-07T17:18:24Z")

</div>

> In Advanced Playbooks there is a section about Accessing Info About Other Hosts that mentions a groups list, but I think that's only for Jinja templates and is not a variable accessible by playbooks.

${host\_vars.name\_of\_host.name\_of\_variable} works fine in playbooks  
provided your host names don't have any dots in them. If they do,  
that's a known issue and we need some way of escaping that in the  
non-Jinja2 variable specification. (There's already a ticket on this)

---

<div class="post-metadata">

**Author:** ![Marco\_Vito\_Moscarito](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Marco\_Vito\_Moscarito](https://forum.ansible.com/u/Marco_Vito_Moscarito)\
**Post date:** [September 7, 2012, 5:19pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/6 "2012-09-07T17:19:44Z")

</div>

> Sorry I misunderstood what you were trying to do.

No problem 🙂

> In Advanced Playbooks there is a section about Accessing Info About Other Hosts that mentions a groups list, but I think that's only for Jinja templates and is not a variable accessible by playbooks. Maybe I misreading the docs. That would be you ideal solution. with\_items: groups['mongodb']

After some investigation I see that Task are invoked in:

\* [https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/\_\_init\_\_.py#L240](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/ __init__.py#L240)

using host as param (current host), but it also can have access to  
inventory using self.inventory.

Bye  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marco

---

<div class="post-metadata">

**Author:** ![Marco\_Vito\_Moscarito](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Marco\_Vito\_Moscarito](https://forum.ansible.com/u/Marco_Vito_Moscarito)\
**Post date:** [September 7, 2012, 5:28pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/7 "2012-09-07T17:28:53Z")

</div>

I think you are referring to:

\* [https://github.com/ansible/ansible/issues/907](https://github.com/ansible/ansible/issues/907)

I think proble is not only on dot, but Task don't have access to  
variable (i think), all variables with $ are replaced and added into  
variable without $ eg: ${mydata} are converted to {mydata}:

\* [https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/\_\_init\_\_.py#L258](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/ __init__.py#L258)

I'm investigating how to add this feature, if you can point me on the  
right path.... 🙂

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marco

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [September 7, 2012, 5:35pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/8 "2012-09-07T17:35:23Z")

</div>

Understood.

I was not sure that ${groups['somegroupname']} works in playbooks or that you could grab a list of $hostvars keys (aka host names) to use in an items loop.

\<tim/\>

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [September 7, 2012, 5:52pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/9 "2012-09-07T17:52:37Z")

</div>

Marco Vito Moscaritolo wrote:

---

<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:** [September 7, 2012, 6:02pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/10 "2012-09-07T18:02:16Z")

</div>

I don't know either. If it doesn't work, patches to make it work  
would be welcome. You can definitely assign a regular variable like:

foo: $iAmAList

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [September 7, 2012, 6:16pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/11 "2012-09-07T18:16:59Z")

</div>

Nope. The groups var does not seem accessible in playbooks. Quick threw together a test and go these errors in my attempts.

> unbound variable in with\_items: {groups[‘nodes’]}
> 
> unbound variable in with\_items: {groups.nodes}

Which would be the correct form? I assume the latter.

---

<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:** [September 7, 2012, 6:37pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/12 "2012-09-07T18:37:14Z")

</div>

The latter.

The part that lets variables work in with\_items is pretty naive, I  
think it just throws away the "$" and sees if the top level variable  
is available in the dict.

We'd want something like varLookup that just returned the actual  
value, versus templating it out, but still used the same regex logic.

---

<div class="post-metadata">

**Author:** ![Lee\_S](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@Lee\_S](https://forum.ansible.com/u/Lee_S)\
**Post date:** [January 5, 2013, 9:54pm UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/13 "2013-01-05T21:54:33Z")

</div>

Any solution to this? I’m trying to do the same thing.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 6, 2013, 2:29am UTC](https://forum.ansible.com/t/cycle-hosts-in-tasks/11871/14 "2013-01-06T02:29:07Z")

</div>

Lee S. wrote:

> Any solution to this? I'm trying to do the same thing.

What thing is that?

Daniel
