From e4b747d78884e35be8706629af844470f1222e68 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 2 Jan 2021 18:53:25 +0100 Subject: [PATCH] docker_container: return status also when 0 for non-detached containers (#58) * Return status also when 0. * Fix PR number. --- .../58-docker_container-non-detached-status.yml | 2 ++ plugins/modules/docker_container.py | 12 +++++++++++- .../targets/docker_container/tasks/tests/options.yml | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/58-docker_container-non-detached-status.yml diff --git a/changelogs/fragments/58-docker_container-non-detached-status.yml b/changelogs/fragments/58-docker_container-non-detached-status.yml new file mode 100644 index 000000000..1907f7828 --- /dev/null +++ b/changelogs/fragments/58-docker_container-non-detached-status.yml @@ -0,0 +1,2 @@ +minor_changes: +- "docker_container - when a container is started with ``detached=false``, ``status`` is now also returned when it is 0 (https://github.com/ansible-collections/community.docker/issues/26, https://github.com/ansible-collections/community.docker/pull/58)." diff --git a/plugins/modules/docker_container.py b/plugins/modules/docker_container.py index c1dc68a2d..3c41f2111 100644 --- a/plugins/modules/docker_container.py +++ b/plugins/modules/docker_container.py @@ -1136,6 +1136,13 @@ }, ... }' +status: + description: + - 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. + returned: when I(state) is C(started) and I(detached) is C(false), and when waiting for the container result did not fail + type: int + sample: 0 ''' import os @@ -3007,6 +3014,9 @@ def container_start(self, container_id): status = self.client.wait(container_id)['StatusCode'] else: status = self.client.wait(container_id) + self.client.fail_results['status'] = status + self.results['status'] = status + if self.parameters.auto_remove: output = "Cannot retrieve result as auto_remove is enabled" if self.parameters.output_logs: @@ -3023,7 +3033,7 @@ def container_start(self, container_id): output = "Result logged using `%s` driver" % logging_driver if status != 0: - self.fail(output, status=status) + self.fail(output) if self.parameters.cleanup: self.container_remove(container_id, force=True) insp = self._get_container(container_id) diff --git a/tests/integration/targets/docker_container/tasks/tests/options.yml b/tests/integration/targets/docker_container/tasks/tests/options.yml index 79c329143..007952afb 100644 --- a/tests/integration/targets/docker_container/tasks/tests/options.yml +++ b/tests/integration/targets/docker_container/tasks/tests/options.yml @@ -588,9 +588,11 @@ # often enough to be annoying. That's why we disable this for now, # and simply test that 'Output' is contained in the result. - "'Output' in detach_no_cleanup.container" + - detach_no_cleanup.status == 0 # - "'Hello from Docker!' in detach_no_cleanup.container.Output" - detach_no_cleanup_cleanup is changed - "'Output' in detach_cleanup.container" + - detach_cleanup.status == 0 # - "'Hello from Docker!' in detach_cleanup.container.Output" - detach_cleanup_cleanup is not changed - assert: