From 86387df8b3d007970221eb29235087e20eccefab Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Thu, 19 Jul 2018 12:40:23 +0200 Subject: [PATCH] Fix docker_container stop/start image dependency (#41678) This commit adds a conditional inside the constructor so that if a user didn't specified an image parameter all the code related to the image version comparison is omitted. This is extremely useful when a user want to just stop and start containers using just the name, keeping everything as it is, without the need of specifying all the image and command stuff used at creation. Today if you don't specify the image you get an error, and this is a confusing [1] behavior. [1] https://github.com/ansible/ansible/issues/27960 --- .../modules/cloud/docker/docker_container.py | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index e2012695e3a..e328687a20a 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1739,32 +1739,36 @@ class ContainerManager(DockerBaseClass): def present(self, state): container = self._get_container(self.parameters.name) - image = self._get_image() - self.log(image, pretty_print=True) - if not container.exists: - # New container - self.log('No container found') - new_container = self.container_create(self.parameters.image, self.parameters.create_parameters) - if new_container: - container = new_container - else: - # Existing container - different, differences = container.has_different_configuration(image) - image_different = False - if not self.parameters.ignore_image: - image_different = self._image_is_different(image, container) - if image_different or different or self.parameters.recreate: - self.diff['differences'] = differences - if image_different: - self.diff['image_different'] = True - self.log("differences") - self.log(differences, pretty_print=True) - if container.running: - self.container_stop(container.Id) - self.container_remove(container.Id) + + # If the image parameter was passed then we need to deal with the image + # version comparison, otherwise we should not care + if self.parameters.image: + image = self._get_image() + self.log(image, pretty_print=True) + if not container.exists: + # New container + self.log('No container found') new_container = self.container_create(self.parameters.image, self.parameters.create_parameters) if new_container: container = new_container + else: + # Existing container + different, differences = container.has_different_configuration(image) + image_different = False + if not self.parameters.ignore_image: + image_different = self._image_is_different(image, container) + if image_different or different or self.parameters.recreate: + self.diff['differences'] = differences + if image_different: + self.diff['image_different'] = True + self.log("differences") + self.log(differences, pretty_print=True) + 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) + if new_container: + container = new_container if container and container.exists: container = self.update_limits(container)