# Multiple (differing) actions and with\_items

**URL:** <https://forum.ansible.com/t/multiple-differing-actions-and-with-items/19805>\
**Category:** Ansible Project\
**Created:** [November 19, 2015, 10:50am UTC](https://forum.ansible.com/t/multiple-differing-actions-and-with-items/19805 "2015-11-19T10:50:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant](https://avatars.discourse-cdn.com/v4/letter/g/a5b964/32.png) [@grant](https://forum.ansible.com/u/grant)\
**Post date:** [November 19, 2015, 10:50am UTC](https://forum.ansible.com/t/multiple-differing-actions-and-with-items/19805/1 "2015-11-19T10:50:25Z")

</div>

Hi all,

I’m fairly new to Ansible and have an issue I haven’t been able to answer. I might be attacking it the wrong way, so I’d appreciate any pointers.

I have a number of docker images that need to be started, but I need to make sure the web server inside those docker containers is running before I go on to the next container.

The following is what I’m trying to achieve inside my role

```auto
---

- name: Start and wait for containers
  docker: name=mydata image={{ item.name }} state=present ports={{ item.ports }}
  wait_for: port={{ item.wait_for_port }}
  with_items:
    - { name: "service1", ports: "8080:8080", wait_for_port: 8080 }
    - { name: "service2", ports: "8081:8080", wait_for_port: 8081 }
    - { name: "service3", ports: "8082:8080", wait_for_port: 8082 }

```

It seems I can’t have 2 actions in the same task.

In the final solution the son for the items will probably be passed as an array parameter to the role.

What I need is for the each docker container to start and I then need to wait for the port to become available before moving on to the next

If I split this up into 2 tasks then all the containers will be started without the internal services necessarily becoming available.

```auto
---

- name: Start and wait for containers
  docker: name=mydata image={{ item.name }} state=present ports={{ item.ports }}
  with_items:
    - { name: "service1", ports: "8080:8080" }
    - { name: "service2", ports: "8081:8080" }
    - { name: "service3", ports: "8082:8080" }

- name: Wait for ready state
  wait_for: port
  with_items:
    - 8080
    - 8081
    - 8082

```

Any suggestions are appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![grant](https://avatars.discourse-cdn.com/v4/letter/g/a5b964/32.png) [@grant](https://forum.ansible.com/u/grant)\
**Post date:** [November 22, 2015, 2:34am UTC](https://forum.ansible.com/t/multiple-differing-actions-and-with-items/19805/2 "2015-11-22T02:34:50Z")

</div>

Sorry, noticed a typo in my original post

In the final solution the **j** son for the items will probably be passed as an array parameter to the role.

---

<div class="post-metadata">

**Author:** ![Markus\_Ellers](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Markus\_Ellers](https://forum.ansible.com/u/Markus_Ellers)\
**Post date:** [November 23, 2015, 7:22pm UTC](https://forum.ansible.com/t/multiple-differing-actions-and-with-items/19805/3 "2015-11-23T19:22:11Z")

</div>

I would suggest approching it like this, though I haven’t tested this and I’m not very experienced with docker. (If anyone has experience with this or knows a better solution please correct me 🙂 ):

1. Put your items in a separate file and define them as a dictionary.

Like this:

```auto
---

myservices:
    - { name: "service1", ports: "8080:8080" }
    - { name: "service2", ports: "8081:8080" }
    - { name: "service3", ports: "8082:8080" }

```

1. Now in your playbook be sure to set serial: 1, include the yml file with your service dict and define your tasks like this

```auto
---

- name: Start and wait for containers
  docker: name=mydata image={{ [item.name](http://item.name/) }} state=present ports={{ item.ports }}
  wait_for: port={{ item.wait_for_port }}
  with_items:
    - "{{myservices}}"

```
