need help getting started using Ansible conditionals with regards to the absence/presence of Docker containers

Hi;

  I think I know what I need to do if I were running a bash script to
manage Docker containers.

  But I'm not sure how to leverage that knowledge into Ansible conditionals.

  My problem is that I need to make certain that there are no Docker
containers (running or not) that match a certain name (actually at
this point, any Docker container), prior to starting it up (docker
run).

  I wish that the Ansible Docker module supported query operations.

  My query (via bash shell) is basically: "sudo docker ps" or "sudo
docker ps -a" where I usually parse the output via one or more of the
grep, awk, wc and tr commands. If I know that the docker container
does not exist then I will create a new one (perhaps with different
options).

My solution right now is to "docker rm -f $(docker ps -a -q)" which
loudly screams if there are no docker containers.

  I'd like to program in such a way as that there are no ugly red responses :slight_smile:

Thanks,
Ken Wolcott

Hi Ken,

I don’t think it’s possible right now to remove all containers in one go. If you do have a name, and you’re using the 1.9 prerelease, you should able to run:

  • name: stop and remove the container “thename”
    docker:
    state: absent
    name: thename
    image: ignored

You can also remove all containers that were launched from a specific image:

  • name: stop and remove all containers launched from training/webapp
    docker:
    state: absent
    image: training/webapp

  • Ash