mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fix #3822 stop container
This commit is contained in:
parent
c9e4955d38
commit
ec9f56d8e0
1 changed files with 42 additions and 44 deletions
|
@ -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,24 +1581,20 @@ 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
|
||||
|
||||
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)
|
||||
|
@ -1603,9 +1602,8 @@ class ContainerManager(DockerBaseClass):
|
|||
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
|
||||
|
||||
if container and container.exists:
|
||||
container = self.update_limits(container)
|
||||
container = self.update_networks(container)
|
||||
|
||||
|
@ -1614,15 +1612,15 @@ class ContainerManager(DockerBaseClass):
|
|||
elif state == 'started' and self.parameters.restart:
|
||||
self.container_stop(container.Id)
|
||||
container = self.container_start(container.Id)
|
||||
elif state == 'present' and container.running:
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue