Multiple (differing) actions and with_items

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

---

- 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.

---

- 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

Sorry, noticed a typo in my original post

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

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 :slight_smile: ):

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

Like this:

---

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
---

- 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}}"