From af2b4779504f9a8d5397c3f470bbc2ba45b95608 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 5 Nov 2018 12:33:56 +0100 Subject: [PATCH] docker_container: fix container removal when auto_remove is used (#48061) * Fix container removal when auto_remove is used. --- .../48061-docker_container-auto_removal.yml | 2 ++ .../modules/cloud/docker/docker_container.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/48061-docker_container-auto_removal.yml diff --git a/changelogs/fragments/48061-docker_container-auto_removal.yml b/changelogs/fragments/48061-docker_container-auto_removal.yml new file mode 100644 index 00000000000..3eaf6640c12 --- /dev/null +++ b/changelogs/fragments/48061-docker_container-auto_removal.yml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container - do not fail when removing a container which has ``auto_remove: yes``." diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index b2eb71fa6e5..8b734455b06 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -803,6 +803,7 @@ try: from docker.types import Ulimit, LogConfig else: from docker.utils.types import Ulimit, LogConfig + from docker.errors import APIError, NotFound except Exception as dummy: # missing docker-py handled in ansible.module_utils.docker pass @@ -2419,7 +2420,7 @@ class ContainerManager(DockerBaseClass): if status != 0: self.fail(output, status=status) if self.parameters.cleanup: - self.container_remove(container_id, force=True, ignore_failure=self.parameters.auto_remove) + self.container_remove(container_id, force=True) insp = self._get_container(container_id) if insp.raw: insp.raw['Output'] = output @@ -2428,7 +2429,7 @@ class ContainerManager(DockerBaseClass): return insp return self._get_container(container_id) - def container_remove(self, container_id, link=False, force=False, ignore_failure=False): + def container_remove(self, container_id, link=False, force=False): volume_state = (not self.parameters.keep_volumes) self.log("remove container container:%s v:%s link:%s force%s" % (container_id, volume_state, link, force)) self.results['actions'].append(dict(removed=container_id, volume_state=volume_state, link=link, force=force)) @@ -2437,9 +2438,16 @@ class ContainerManager(DockerBaseClass): if not self.check_mode: try: response = self.client.remove_container(container_id, v=volume_state, link=link, force=force) - except Exception as exc: - if not ignore_failure: + except NotFound as exc: + pass + except APIError as exc: + if exc.response.status_code == 409 and ('removal of container ' in exc.explanation and + ' is already in progress' in exc.explanation): + pass + else: self.fail("Error removing container %s: %s" % (container_id, str(exc))) + except Exception as exc: + self.fail("Error removing container %s: %s" % (container_id, str(exc))) return response def container_update(self, container_id, update_parameters):