From ec9f56d8e0bf7175bc3469cd342adb85f666687e Mon Sep 17 00:00:00 2001 From: chouseknecht Date: Thu, 2 Jun 2016 12:32:44 -0400 Subject: [PATCH] Fix #3822 stop container --- .../modules/cloud/docker/docker_container.py | 84 +++++++++---------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 0262bacbd86..56565d66e01 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -369,8 +369,8 @@ options: re-create a matching container, even if it is running. Use restart to force a matching container to be stopped and restarted. Use force_kill to kill a container rather than stopping it. Use keep_volumes to retain volumes associated with a removed container.' - - 'I(stopped) - a container matching the specified name will be stopped. Use force_kill to kill a container rather than - stopping it.' + - 'I(stopped) - Asserts that the container is first I(present), and then if the container is running moves it to a stopped + state. Use force_kill to kill a container rather than stopping it.' required: false default: started choices: @@ -492,10 +492,13 @@ EXAMPLES = ''' docker_container: name: mycontainer state: present - recreate: yes - force_kill: yes - image: someplace/image - command: echo "I'm here!" + image: ubuntu:14.04 + command: sleep infinity + +- name: Stop a contianer + docker_container: + name: mycontainer + state: stopped - name: Start 4 load-balanced containers docker_container: @@ -1089,7 +1092,7 @@ class Container(DockerBaseClass): self.parameters.client.module.fail_json(msg=msg) @property - def found(self): + def exists(self): return True if self.container else False @property @@ -1557,7 +1560,7 @@ class ContainerManager(DockerBaseClass): self.facts = {} state = self.parameters.state - if state in ('started', 'present'): + if state in ('stopped', 'started', 'present'): self.present(state) elif state == 'absent': self.absent() @@ -1578,51 +1581,46 @@ class ContainerManager(DockerBaseClass): container = self._get_container(self.parameters.name) image = self._get_image() - if not container.found: - self.log('No container found') + 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 - container = self.update_limits(container) - container = self.update_networks(container) - if state == 'started': - container = self.container_start(container.Id) - self.facts = container.raw - return - - # Existing container - different, differences = container.has_different_configuration(image) - image_different = self._image_is_different(image, container) - if image_different or different or self.parameters.recreate: - self.diff['differences'] = differences - self.log("differences") - self.log(differences, pretty_print=True) - 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 image_different: - self.diff['image_different'] = True + else: + # Existing container + different, differences = container.has_different_configuration(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) + 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 - container = self.update_limits(container) - container = self.update_networks(container) + if container and container.exists: + container = self.update_limits(container) + container = self.update_networks(container) - if state == 'started' and not container.running: - container = self.container_start(container.Id) - elif state == 'started' and self.parameters.restart: - self.container_stop(container.Id) - container = self.container_start(container.Id) - elif state == 'present' and container.running: - self.container_stop(container.Id) - container = self._get_container(container.Id) + if state == 'started' and not container.running: + container = self.container_start(container.Id) + elif state == 'started' and self.parameters.restart: + self.container_stop(container.Id) + container = self.container_start(container.Id) + elif state == 'stopped' and container.running: + self.container_stop(container.Id) + container = self._get_container(container.Id) self.facts = container.raw def absent(self): - container = Container(self.client.get_container(self.parameters.name), self.parameters) - if container.found: + container = self._get_container(self.parameters.name) + if container.exists: if container.running: self.container_stop(container.Id) self.container_remove(container.Id)