Multiple (differing) actions and with_items

204 views
Skip to first unread message

gr...@grantlittle.me

unread,
Nov 20, 2015, 2:51:55 PM11/20/15
to Ansible Project
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

gr...@grantlittle.me

unread,
Nov 21, 2015, 9:34:51 PM11/21/15
to Ansible Project
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.

Markus Ellers

unread,
Nov 23, 2015, 2:22:11 PM11/23/15
to Ansible Project
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:

---

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

2. 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 }} state=present ports={{ item.ports }}
wait_for: port={{ item.wait_for_port }}
with_items:
    - "{{myservices}}"

Reply all
Reply to author
Forward
0 new messages