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

**URL:** <https://forum.ansible.com/t/need-help-getting-started-using-ansible-conditionals-with-regards-to-the-absence-presence-of-docker-containers/17743>\
**Category:** Ansible Project\
**Created:** [March 13, 2015, 7:47pm UTC](https://forum.ansible.com/t/need-help-getting-started-using-ansible-conditionals-with-regards-to-the-absence-presence-of-docker-containers/17743 "2015-03-13T19:47:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_Wolcott](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@Kenneth\_Wolcott](https://forum.ansible.com/u/Kenneth_Wolcott)\
**Post date:** [March 13, 2015, 7:47pm UTC](https://forum.ansible.com/t/need-help-getting-started-using-ansible-conditionals-with-regards-to-the-absence-presence-of-docker-containers/17743/1 "2015-03-13T19:47:36Z")

</div>

Hi;

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

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

&nbsp;&nbsp;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).

&nbsp;&nbsp;I wish that the Ansible Docker module supported query operations.

&nbsp;&nbsp;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.

&nbsp;&nbsp;I'd like to program in such a way as that there are no ugly red responses 🙂

Thanks,  
Ken Wolcott

---

<div class="post-metadata">

**Author:** ![Ash\_Wilson](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ash\_Wilson](https://forum.ansible.com/u/Ash_Wilson)\
**Post date:** [March 14, 2015, 5:22pm UTC](https://forum.ansible.com/t/need-help-getting-started-using-ansible-conditionals-with-regards-to-the-absence-presence-of-docker-containers/17743/2 "2015-03-14T17:22:01Z")

</div>

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
