mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 23:21:22 -07:00
docker_container: fix interaction of detach:no with auto_remove:yes (#47396)
* Behave better if auto_remove and output_logs are combined. Warn if output cannot be retrieved because of auto_remove. * Add tests. * Added changelog.
This commit is contained in:
parent
29b4b36501
commit
3afdb28209
3 changed files with 68 additions and 11 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- "docker_container - fix behavior of ``detach: yes`` if ``auto_remove: yes`` is specified."
|
|
@ -2211,6 +2211,11 @@ class ContainerManager(DockerBaseClass):
|
||||||
status = self.client.wait(container_id)['StatusCode']
|
status = self.client.wait(container_id)['StatusCode']
|
||||||
else:
|
else:
|
||||||
status = self.client.wait(container_id)
|
status = self.client.wait(container_id)
|
||||||
|
if self.parameters.auto_remove:
|
||||||
|
output = "Cannot retrieve result as auto_remove is enabled"
|
||||||
|
if self.parameters.output_logs:
|
||||||
|
self.client.module.warn('Cannot output_logs if auto_remove is enabled!')
|
||||||
|
else:
|
||||||
config = self.client.inspect_container(container_id)
|
config = self.client.inspect_container(container_id)
|
||||||
logging_driver = config['HostConfig']['LogConfig']['Type']
|
logging_driver = config['HostConfig']['LogConfig']['Type']
|
||||||
|
|
||||||
|
@ -2224,7 +2229,7 @@ class ContainerManager(DockerBaseClass):
|
||||||
if status != 0:
|
if status != 0:
|
||||||
self.fail(output, status=status)
|
self.fail(output, status=status)
|
||||||
if self.parameters.cleanup:
|
if self.parameters.cleanup:
|
||||||
self.container_remove(container_id, force=True)
|
self.container_remove(container_id, force=True, ignore_failure=self.parameters.auto_remove)
|
||||||
insp = self._get_container(container_id)
|
insp = self._get_container(container_id)
|
||||||
if insp.raw:
|
if insp.raw:
|
||||||
insp.raw['Output'] = output
|
insp.raw['Output'] = output
|
||||||
|
@ -2233,7 +2238,7 @@ class ContainerManager(DockerBaseClass):
|
||||||
return insp
|
return insp
|
||||||
return self._get_container(container_id)
|
return self._get_container(container_id)
|
||||||
|
|
||||||
def container_remove(self, container_id, link=False, force=False):
|
def container_remove(self, container_id, link=False, force=False, ignore_failure=False):
|
||||||
volume_state = (not self.parameters.keep_volumes)
|
volume_state = (not self.parameters.keep_volumes)
|
||||||
self.log("remove container container:%s v:%s link:%s force%s" % (container_id, volume_state, link, force))
|
self.log("remove container container:%s v:%s link:%s force%s" % (container_id, volume_state, link, force))
|
||||||
self.results['actions'].append(dict(removed=container_id, volume_state=volume_state, link=link, force=force))
|
self.results['actions'].append(dict(removed=container_id, volume_state=volume_state, link=link, force=force))
|
||||||
|
@ -2243,6 +2248,7 @@ class ContainerManager(DockerBaseClass):
|
||||||
try:
|
try:
|
||||||
response = self.client.remove_container(container_id, v=volume_state, link=link, force=force)
|
response = self.client.remove_container(container_id, v=volume_state, link=link, force=force)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
if not ignore_failure:
|
||||||
self.fail("Error removing container %s: %s" % (container_id, str(exc)))
|
self.fail("Error removing container %s: %s" % (container_id, str(exc)))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
|
@ -480,7 +480,56 @@
|
||||||
## detach ##########################################################
|
## detach ##########################################################
|
||||||
####################################################################
|
####################################################################
|
||||||
|
|
||||||
# TODO: - detach
|
- name: detach without cleanup
|
||||||
|
docker_container:
|
||||||
|
name: "{{ cname }}"
|
||||||
|
image: hello-world
|
||||||
|
detach: no
|
||||||
|
register: detach_no_cleanup
|
||||||
|
|
||||||
|
- name: cleanup
|
||||||
|
docker_container:
|
||||||
|
name: "{{ cname }}"
|
||||||
|
state: absent
|
||||||
|
register: detach_no_cleanup_cleanup
|
||||||
|
|
||||||
|
- name: detach with cleanup
|
||||||
|
docker_container:
|
||||||
|
name: "{{ cname }}"
|
||||||
|
image: hello-world
|
||||||
|
detach: no
|
||||||
|
cleanup: yes
|
||||||
|
register: detach_cleanup
|
||||||
|
|
||||||
|
- name: cleanup (unnecessary)
|
||||||
|
docker_container:
|
||||||
|
name: "{{ cname }}"
|
||||||
|
state: absent
|
||||||
|
register: detach_cleanup_cleanup
|
||||||
|
|
||||||
|
- name: detach with auto_remove and cleanup
|
||||||
|
docker_container:
|
||||||
|
name: "{{ cname }}"
|
||||||
|
image: hello-world
|
||||||
|
detach: no
|
||||||
|
auto_remove: yes
|
||||||
|
cleanup: yes
|
||||||
|
register: detach_auto_remove
|
||||||
|
|
||||||
|
- name: cleanup (unnecessary)
|
||||||
|
docker_container:
|
||||||
|
name: "{{ cname }}"
|
||||||
|
state: absent
|
||||||
|
register: detach_auto_remove_cleanup
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "'Hello from Docker!' in detach_no_cleanup.ansible_facts.docker_container.Output"
|
||||||
|
- detach_no_cleanup_cleanup is changed
|
||||||
|
- "'Hello from Docker!' in detach_cleanup.ansible_facts.docker_container.Output"
|
||||||
|
- detach_cleanup_cleanup is not changed
|
||||||
|
- "'Cannot retrieve result as auto_remove is enabled' == detach_auto_remove.ansible_facts.docker_container.Output"
|
||||||
|
- detach_auto_remove_cleanup is not changed
|
||||||
|
|
||||||
####################################################################
|
####################################################################
|
||||||
## devices #########################################################
|
## devices #########################################################
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue