Make sure that 'container' is returned in more circumstances, and improve documentation when it is actually returned (#178)

* Make sure that 'container' is returned in more circumstances, and improve documentation when it is actually returned.

* Fix typos.
This commit is contained in:
Felix Fontein 2021-07-24 21:01:14 +02:00 committed by GitHub
parent 9b661a6ac2
commit e2785de840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 8 deletions

View File

@ -0,0 +1,2 @@
bugfixes:
- "docker_container - make sure to also return ``container`` on ``detached=false`` when status code is non-zero (https://github.com/ansible-collections/community.docker/pull/178)."

View File

@ -1139,11 +1139,9 @@ RETURN = '''
container: container:
description: description:
- Facts representing the current state of the container. Matches the docker inspection output. - Facts representing the current state of the container. Matches the docker inspection output.
- Before 2.3 this was C(ansible_docker_container) but was renamed in 2.3 to C(docker_container) due to - Empty if I(state) is C(absent).
conflicts with the connection plugin. - If I(detach=false), will include C(Output) attribute containing any output from container run.
- Empty if I(state) is C(absent) returned: success; or when I(state=started) and I(detach=false), and when waiting for the container result did not fail
- If I(detached) is C(false), will include C(Output) attribute containing any output from container run.
returned: always
type: dict type: dict
sample: '{ sample: '{
"AppArmorProfile": "", "AppArmorProfile": "",
@ -1181,7 +1179,7 @@ status:
description: description:
- In case a container is started without detaching, this contains the exit code of the process in the container. - In case a container is started without detaching, this contains the exit code of the process in the container.
- Before community.docker 1.1.0, this was only returned when non-zero. - Before community.docker 1.1.0, this was only returned when non-zero.
returned: when I(state) is C(started) and I(detached) is C(false), and when waiting for the container result did not fail returned: when I(state=started) and I(detach=false), and when waiting for the container result did not fail
type: int type: int
sample: 0 sample: 0
''' '''
@ -3104,8 +3102,6 @@ class ContainerManager(DockerBaseClass):
else: else:
output = "Result logged using `%s` driver" % logging_driver output = "Result logged using `%s` driver" % logging_driver
if status != 0:
self.fail(output)
if self.parameters.cleanup: if self.parameters.cleanup:
self.container_remove(container_id, force=True) self.container_remove(container_id, force=True)
insp = self._get_container(container_id) insp = self._get_container(container_id)
@ -3113,6 +3109,10 @@ class ContainerManager(DockerBaseClass):
insp.raw['Output'] = output insp.raw['Output'] = output
else: else:
insp.raw = dict(Output=output) insp.raw = dict(Output=output)
if status != 0:
# Set `failed` to True and return output as msg
self.results['failed'] = True
self.results['msg'] = output
return insp return insp
return self._get_container(container_id) return self._get_container(container_id)

View File

@ -615,6 +615,16 @@
register: detach_auto_remove_cleanup register: detach_auto_remove_cleanup
diff: no diff: no
- name: detach with cleanup and non-zero status
docker_container:
name: "{{ cname }}"
image: "{{ docker_test_image_alpine }}"
command: '/bin/sh -c "exit 42"'
detach: no
cleanup: yes
register: detach_cleanup_nonzero
ignore_errors: true
- assert: - assert:
that: that:
# NOTE that 'Output' sometimes fails to contain the correct output # NOTE that 'Output' sometimes fails to contain the correct output
@ -629,6 +639,10 @@
- detach_cleanup.status == 0 - detach_cleanup.status == 0
# - "'Hello from Docker!' in detach_cleanup.container.Output" # - "'Hello from Docker!' in detach_cleanup.container.Output"
- detach_cleanup_cleanup is not changed - detach_cleanup_cleanup is not changed
- detach_cleanup_nonzero is failed
- detach_cleanup_nonzero.status == 42
- "'Output' in detach_cleanup_nonzero.container"
- "detach_cleanup_nonzero.container.Output == ''"
- assert: - assert:
that: that:
- "'Cannot retrieve result as auto_remove is enabled' == detach_auto_remove.container.Output" - "'Cannot retrieve result as auto_remove is enabled' == detach_auto_remove.container.Output"