# Can I loop over multipe actions?

**URL:** <https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228>\
**Category:** Ansible Project\
**Created:** [January 9, 2015, 8:21am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228 "2015-01-09T08:21:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 9, 2015, 8:21am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/1 "2015-01-09T08:21:14Z")

</div>

Hello,

So include and with\_items is depricated and I am wondering how to implement the following:

I have to create a couple of similar services:

(I know this is not working)  
`

- name: loopy  
shell: download something  
template: src: dest:  
service: name={{ service }} state=started  
with\_items: listing  
`

Is the only way to do this?

`

- name: download everything  
shell: download something  
with\_items: listing
- name: copy templates  
template: src: dest:  
with\_items: listing
- name: download everything  
service: name={{ service }} state=started  
with\_items: listing  
`

What would be a better approach?

thanks

Peter

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [January 9, 2015, 8:31am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/2 "2015-01-09T08:31:57Z")

</div>

The solution you’ve shown is the only one at the moment.

Peter Mooshammer [pmoosha@gmail.com](mailto:pmoosha@gmail.com) napisał:

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 10, 2015, 12:34am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/3 "2015-01-10T00:34:23Z")

</div>

thanks for getting back to me.

Here is what i intended:

I have a list of docker services:

`

services:

- servicename: zapp  
containername: zapp  
port: 5555  
protocol: tcp  
exposed: false  
image: zapp
- servicename: xox  
containername: xox  
port: 4444  
protocol: udp  
exposed: true  
linkto: zapp  
image: xox

`

And I wanted to use a loop to create those services, but now I am doing this:

`

- { role: some-services, service: “{{ services[0] }}” }
- { role: some-services, service: “{{ services[1] }}” }

`

And I wanted to do this as a loop. But maybe I am completely off. Any ideas?

Note one of the reasons I am doing this is because ansible does not support docker restarts at the moment (it will with 1.9)

thanks

Peter

---

<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:** [January 10, 2015, 12:38am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/4 "2015-01-10T00:38:02Z")

</div>

there is not currently a way to loop through roles, you can make the  
role accept a list and have it's tasks loop over it though.

also i would advice to not use a variable named 'service' as it can  
conflict with the module of the same name.

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 10, 2015, 12:58am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/5 "2015-01-10T00:58:31Z")

</div>

thanks - good point!!!

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [January 10, 2015, 11:03am UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/6 "2015-01-10T11:03:02Z")

</div>

I'm pretty sure modules and actions live in a different name space than variables. Or do you mean visually?

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 10, 2015, 7:29pm UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/7 "2015-01-10T19:29:33Z")

</div>

I think Tomasz it correct, it was working. Changed it anyways so people don’t get confused.

One more question. I tried:

And I wanted to use a loop to create those services, but now I am doing this:

`

- { role: some-services, service: “{{ services[0] }}”, when: services[0] is defined }
- { role: some-services, service: “{{ services[1] }}”, when: services[1] is defined }  
`

or

`

- { role: some-services, service: “{{ services[0] }}”, when: “{{ services[0] }}” is defined }
- { role: some-services, service: “{{ services[1] }}”, when: “{{ services[0] }}” is defined }

`

But none of that is working, this however works:  
`

- { role: some-services, service: “{{ services[1] }}”, when: simple\_var is defined }

`

thanks

Peter

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 10, 2015, 7:45pm UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/8 "2015-01-10T19:45:07Z")

</div>

just an update:

`

- name: debug {{ services[0][‘hostname’] }}  
debug: msg={{ services[0][‘hostname’] }}  
when: services[0] is defined
- name: debug {{ services[3][‘hostname’] }}  
debug: msg={{ services[3][‘hostname’] }}  
when: services[3] is defined

`

This, it is just not working as a conditional for roles…

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 10, 2015, 8:17pm UTC](https://forum.ansible.com/t/can-i-loop-over-multipe-actions/17228/9 "2015-01-10T20:17:19Z")

</div>

Got it:

`

- { role: some-services, service: “{{ services[0] }}”, when: services.0 is defined }
- { role: some-services, service: “{{ services[1] }}”, when: services.1 is defined }

`
