# looping over set of tasks (roles?)

**URL:** <https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550>\
**Category:** Ansible Project\
**Created:** [June 25, 2014, 8:46pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550 "2014-06-25T20:46:02Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [June 27, 2014, 2:47pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/21 "2014-06-27T14:47:43Z")

</div>

This is true about custom iterators, though not all people can write custom iterators and I have personally witnessed some unjustified negativism for accepting in core small improvements for available iterators for everyone’s benefit. To be more specific, I was thinking of a “goback” feature as a very restricted ‘goto’. It would allow to move only backwards and only in the same play and same file. That way all problems of ‘goto’ are gone. That said, now I am thinking of another possible implementation: By default ‘times’ would be 1. If times == 0 it would check that an ‘until’ condition has been set and repeat tagged tasks until that condition becomes true. Other cases: - Stress testing a group of hosts by repeating a group of tasks X times - Run a time-dependent port knocking scheme X times to pass through a firewall - Run X passes of a series of data-destructive tasks on some disk containing your secrets And generally repeating any couple of “run task & check for output” tasks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 27, 2014, 2:52pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/22 "2014-06-27T14:52:06Z")

</div>

we have a do/until already

---

<div class="post-metadata">

**Author:** ![Daniel\_Siechniewicz](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Daniel\_Siechniewicz](https://forum.ansible.com/u/Daniel_Siechniewicz)\
**Post date:** [April 17, 2015, 2:08pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/23 "2015-04-17T14:08:46Z")

</div>

Hi,

Sorry to reanimate this, but I think I have a “legit” use case for looping over multiple tasks:

This is what I’ve got:

- name: install cq packages  
local\_action: cqpkg name={{ item.pkg }}  
path={{ item.file }}  
state=present  
admin\_user={{ admin\_user }}  
admin\_password={{ admin\_pass }}  
host={{ ansible\_eth1.ipv4.address }}  
port={{ listen\_port }}  
with\_items: cq\_packages

But really I need (pretend syntax of course):

- name: install cq packages

# Install package

- local\_action: cqpkg name={{ item.pkg }}  
path={{ item.file }}  
state=present  
admin\_user={{ admin\_user }}  
admin\_password={{ admin\_pass }}  
host={{ ansible\_eth1.ipv4.address }}  
port={{ listen\_port }}

# Restart service if package requires it to be done immediately

- service: name={{ service\_name }} enabled=yes state=started  
when: {{ item.cqrestart }} == immediate

# Wait for the port to come up (this can happen a while after restart)

- wait\_for: port={{ listen\_port }} delay=1

# Wait for the darned thing to come back, and this can take AGES

- shell: “{{ aem\_check }}”  
until: result.stdout.find(“200 OK”) != -1  
retries: 180  
delay: 10  
register: result  
changed\_when: False

# Rinse, repeat

with\_items: cq\_packages

Because I’m using an ansible module to do actual installation (cqpkg) doing it as a script would require some serious rewriting OR doing all the restarts and waiting and checking within the module. Waiting and checking I’d be reasonably OK with, but not service restart, to be honest. The restart cannot be a handler either, because some packages require immediate restart (application can break beyond recovery if cq is not restarted there and then). As far as I managed to understand flush\_handlers this won’t work in a loop either, not to mention unintended consequences, as it’s all or nothing.

This can, and most likely will be, separated out into a parameterised role, but it’ll force fairly non-intuitive "flow.

This really seems to be a simple example of why loop over multiple tasks makes sense: I want to install a package, restart a service and wait for things to settle down before proceeding with installation of next package.

I’ll need to come up with a solution pretty much today, so this is just for general discussion.

Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_Siechniewicz](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Daniel\_Siechniewicz](https://forum.ansible.com/u/Daniel_Siechniewicz)\
**Post date:** [April 17, 2015, 2:33pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/24 "2015-04-17T14:33:43Z")

</div>

Or is this a role for include + with\_items that will be available in 2.0?

Regards,  
Daniel

---

<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:** [April 17, 2015, 2:36pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/25 "2015-04-17T14:36:54Z")

</div>

yes

---

<div class="post-metadata">

**Author:** ![Nick\_Tkach](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@Nick\_Tkach](https://forum.ansible.com/u/Nick_Tkach)\
**Post date:** [October 19, 2016, 5:24pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/26 "2016-10-19T17:24:48Z")

</div>

Can you expand on what you mean by “include + with\_items”? I have a use case that is almost identical to what Daniel gives above. Need something like this (I’m sure it’s not exactly right):

`  
roles/httpd/tasks/main.yml:

---

<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:** [October 19, 2016, 5:51pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/27 "2016-10-19T17:51:41Z")

</div>

- include: file.yml  
with\_fileglob:
- vars/\*.yml

file.yml

- include\_vars: {{ item }}
- debug: msg=“{{ field1 }} from with list”
- debug: msg=“{{ field2 }} from with list”
- template: src=“templates/site.conf.j2” dest=“site.conf”

---

<div class="post-metadata">

**Author:** ![Nick\_Tkach](https://avatars.discourse-cdn.com/v4/letter/n/b38774/32.png) [@Nick\_Tkach](https://forum.ansible.com/u/Nick_Tkach)\
**Post date:** [October 19, 2016, 8:54pm UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/28 "2016-10-19T20:54:52Z")

</div>

Thank you very much! No wonder I hadn’t figured this out earlier! I’m sure there’s just some aspect I never looked into. Just for the next person who comes along what I got to work was

playbook.yml

`

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [March 24, 2019, 9:37am UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/29 "2019-03-24T09:37:26Z")

</div>

Hi. I think this situation hasn’t changed in ansible v2.7 as well.

There is no way I can find to repeat a particular task or a set of tasks within an existing playbook.

It would have been great if we could use tags to call some “tagged-tasks again” wherever we want in a playbook OR create a new feature like ‘goto or goback’ as discussed in the above threads.

My scenario:  
I have to deal with 3 tasks A, B and C. Here C is dependent on A and B’s performance. i.e If C’s output is not as expected, then again A and B have to be repeated again. And somehow I cannot fit this in a ‘block-rescue-always’ structure as its pointless.

Right now, I manage this using ‘include\_task’ option along with ‘when’ conditional. I re-enter A and B tasks into a separate ‘.yml’ file and then call it into this playbook using ‘include\_task’ wherever I want.

This solved my purpose but is ridiculous especially when we have so many other options to effectively orchestrate things using ansilble.

Any inputs/thoughts on this would be helpful for me to explore more.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [March 24, 2019, 10:02am UTC](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550/30 "2019-03-24T10:02:01Z")

</div>

If there is some condition that can be tested externally, then why not a simple script that calls ansible-playbook until that condition obtains?

[Previous page](https://forum.ansible.com/t/looping-over-set-of-tasks-roles/15550.md?page=1)
