mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-03 07:41:30 -07: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
|
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
|
restarted. Use force_kill to kill a container rather than stopping it. Use keep_volumes to retain volumes associated
|
||||||
with a removed container.'
|
with a removed container.'
|
||||||
- 'I(stopped) - a container matching the specified name will be stopped. Use force_kill to kill a container rather than
|
- 'I(stopped) - Asserts that the container is first I(present), and then if the container is running moves it to a stopped
|
||||||
stopping it.'
|
state. Use force_kill to kill a container rather than stopping it.'
|
||||||
required: false
|
required: false
|
||||||
default: started
|
default: started
|
||||||
choices:
|
choices:
|
||||||
|
@ -492,10 +492,13 @@ EXAMPLES = '''
|
||||||
docker_container:
|
docker_container:
|
||||||
name: mycontainer
|
name: mycontainer
|
||||||
state: present
|
state: present
|
||||||
recreate: yes
|
image: ubuntu:14.04
|
||||||
force_kill: yes
|
command: sleep infinity
|
||||||
image: someplace/image
|
|
||||||
command: echo "I'm here!"
|
- name: Stop a contianer
|
||||||
|
docker_container:
|
||||||
|
name: mycontainer
|
||||||
|
state: stopped
|
||||||
|
|
||||||
- name: Start 4 load-balanced containers
|
- name: Start 4 load-balanced containers
|
||||||
docker_container:
|
docker_container:
|
||||||
|
@ -1089,7 +1092,7 @@ class Container(DockerBaseClass):
|
||||||
self.parameters.client.module.fail_json(msg=msg)
|
self.parameters.client.module.fail_json(msg=msg)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def found(self):
|
def exists(self):
|
||||||
return True if self.container else False
|
return True if self.container else False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1557,7 +1560,7 @@ class ContainerManager(DockerBaseClass):
|
||||||
self.facts = {}
|
self.facts = {}
|
||||||
|
|
||||||
state = self.parameters.state
|
state = self.parameters.state
|
||||||
if state in ('started', 'present'):
|
if state in ('stopped', 'started', 'present'):
|
||||||
self.present(state)
|
self.present(state)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
self.absent()
|
self.absent()
|
||||||
|
@ -1578,51 +1581,46 @@ class ContainerManager(DockerBaseClass):
|
||||||
container = self._get_container(self.parameters.name)
|
container = self._get_container(self.parameters.name)
|
||||||
image = self._get_image()
|
image = self._get_image()
|
||||||
|
|
||||||
if not container.found:
|
if not container.exists:
|
||||||
self.log('No container found')
|
|
||||||
# New container
|
# New container
|
||||||
|
self.log('No container found')
|
||||||
new_container = self.container_create(self.parameters.image, self.parameters.create_parameters)
|
new_container = self.container_create(self.parameters.image, self.parameters.create_parameters)
|
||||||
if new_container:
|
if new_container:
|
||||||
container = new_container
|
container = new_container
|
||||||
container = self.update_limits(container)
|
else:
|
||||||
container = self.update_networks(container)
|
# Existing container
|
||||||
if state == 'started':
|
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
|
||||||
|
|
||||||
|
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)
|
container = self.container_start(container.Id)
|
||||||
self.facts = container.raw
|
elif state == 'started' and self.parameters.restart:
|
||||||
return
|
self.container_stop(container.Id)
|
||||||
|
container = self.container_start(container.Id)
|
||||||
# Existing container
|
elif state == 'stopped' and container.running:
|
||||||
different, differences = container.has_different_configuration(image)
|
self.container_stop(container.Id)
|
||||||
image_different = self._image_is_different(image, container)
|
container = self._get_container(container.Id)
|
||||||
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
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
self.facts = container.raw
|
self.facts = container.raw
|
||||||
|
|
||||||
def absent(self):
|
def absent(self):
|
||||||
container = Container(self.client.get_container(self.parameters.name), self.parameters)
|
container = self._get_container(self.parameters.name)
|
||||||
if container.found:
|
if container.exists:
|
||||||
if container.running:
|
if container.running:
|
||||||
self.container_stop(container.Id)
|
self.container_stop(container.Id)
|
||||||
self.container_remove(container.Id)
|
self.container_remove(container.Id)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue