I have a pretty straightforward script where i copy some configuration files from ansible host to a linux machine and then start a docker container mounting those config files.
My Docker container are immutable, so i don't mind recreating them often. In fact, i would like to recreate them in case config changes or they are found stopped. For this i really need a nice way to check status of a container. So the first question - How can i check the status of a Docker container in Ansible ? (i would like to avoid the solution where we match for container name and status in the output of Docker ps command)
Currently, my simple idempotent script is:
---
- hosts: docker
tasks:
- name: copy config files to docker hosts
copy: src="src" dest=/opt/mounted/
- name: start docker container
docker:
name: container
image: image
state: started
volumes:
- /opt/mounted:/opt/config/:ro
The script is pretty idempotent but for my case, it really is not because for some reasons, i need to recreate the container in case Ansible finds that either config files have changed or container was stopped. Now i can create handlers which would simply first delete and then create containers and i can notify them if config files change, but they really don't do well in case of first run or when container is stopped. I need a task which checks if container is stopped or non-existent and then i will call that handler again. How can i arrange my playbook to do that ?