From caf7885ef40fb51eeb0020349d48a73b568313f0 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 6 Oct 2018 15:50:12 +0200 Subject: [PATCH] docker_container: fix behavior when image is not specified (#46322) * Don't simply ignore container in present() if image is not specified. * Use image from existing container for recreation if not specified. * Added changelog. * Improve comment. (cherry picked from commit 895019c59b381e405bc31d7cec568c4458f8dfe7) --- .../46322-docker_container-image-not-given.yaml | 2 ++ .../modules/cloud/docker/docker_container.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/46322-docker_container-image-not-given.yaml diff --git a/changelogs/fragments/46322-docker_container-image-not-given.yaml b/changelogs/fragments/46322-docker_container-image-not-given.yaml new file mode 100644 index 00000000000..844a76f1ac6 --- /dev/null +++ b/changelogs/fragments/46322-docker_container-image-not-given.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container - the behavior is improved in case ``image`` is not specified, but needed for (re-)creating the container." diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index d266fdc69c5..722ec6453f2 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1818,11 +1818,19 @@ class ContainerManager(DockerBaseClass): def present(self, state): container = self._get_container(self.parameters.name) + + # If the image parameter was passed then we need to deal with the image + # version comparison. Otherwise we handle this depending on whether + # the container already runs or not; in the former case, in case the + # container needs to be restarted, we use the existing container's + # image ID. image = self._get_image() self.log(image, pretty_print=True) if not container.exists: # New container self.log('No container found') + if not self.parameters.image: + self.fail('Cannot create container when image is not specified!') new_container = self.container_create(self.parameters.image, self.parameters.create_parameters) if new_container: container = new_container @@ -1838,10 +1846,15 @@ class ContainerManager(DockerBaseClass): self.diff['image_different'] = True self.log("differences") self.log(differences, pretty_print=True) + image_to_use = self.parameters.image + if not image_to_use and container and container.Image: + image_to_use = container.Image + if not image_to_use: + self.fail('Cannot recreate container when image is not specified or cannot be extracted from current container!') if container.running: self.container_stop(container.Id) self.container_remove(container.Id) - new_container = self.container_create(self.parameters.image, self.parameters.create_parameters) + new_container = self.container_create(image_to_use, self.parameters.create_parameters) if new_container: container = new_container