# Buggy code
def container_start(self, container_id):
    self.log("start container %s" % (container_id))
    self.results["actions"].append(dict(started=container_id))
    self.results["changed"] = True
    if not self.check_mode:
        try:
            self.client.start(container=container_id)
        except Exception as exc:
            self.fail("Error starting container %s: %s" % (container_id, str(exc)))

        if not self.parameters.detach:
            if HAS_DOCKER_PY_3:
                status = self.client.wait(container_id)["StatusCode"]
            else:
                status = self.client.wait(container_id)
            config = self.client.inspect_container(container_id)
            logging_driver = config["HostConfig"]["LogConfig"]["Type"]

            if logging_driver == "json-file" or logging_driver == "journald":
                output = self.client.logs(
                    container_id,
                    stdout=True,
                    stderr=True,
                    stream=False,
                    timestamps=False,
                )
                if self.parameters.output_logs:
                    self._output_logs(msg=output)
            else:
                output = "Result logged using `%s` driver" % logging_driver

            if status != 0:
                self.fail(output, status=status)
            if self.parameters.cleanup:
                self.container_remove(container_id, force=True)
            insp = self._get_container(container_id)
            if insp.raw:
                insp.raw["Output"] = output
            else:
                insp.raw = dict(Output=output)
            return insp
    return self._get_container(container_id)


# Traceback (docker.errors.APIError)
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: docker.errors.APIError: 409 Client Error: Conflict ("can not get logs from container which is dead or marked for removal")
fatal: [kube-gandalf.knf.local]: FAILED! => {
"changed": false,
"rc": 1
}

MSG:

MODULE FAILURE


MODULE_STDERR:

Traceback (most recent call last):
File "/tmp/ansible_l28gQr/ansible_module_docker_container.py", line 2081, in <module>
main()
File "/tmp/ansible_l28gQr/ansible_module_docker_container.py", line 2076, in main
cm = ContainerManager(client)
File "/tmp/ansible_l28gQr/ansible_module_docker_container.py", line 1703, in __init__
self.present(state)
File "/tmp/ansible_l28gQr/ansible_module_docker_container.py", line 1750, in present
container = self.container_start(container.Id)
File "/tmp/ansible_l28gQr/ansible_module_docker_container.py", line 1909, in container_start
output = self.client.logs(container_id, stdout=True, stderr=True, stream=False, timestamps=False)
File "/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/docker/api/container.py", line 824, in logs
output = self._get_result(container, stream, res)
File "/usr/local/lib/python2.7/dist-packages/docker/api/client.py", line 409, in _get_result
return self._get_result_tty(stream, res, self._check_is_tty(container))
File "/usr/local/lib/python2.7/dist-packages/docker/api/client.py", line 418, in _get_result_tty
self._raise_for_status(res)
File "/usr/local/lib/python2.7/dist-packages/docker/api/client.py", line 227, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/usr/local/lib/python2.7/dist-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 409 Client Error: Conflict ("can not get logs from container which is dead or marked for removal")

# Fixed code
def container_start(self, container_id):
    self.log("start container %s" % (container_id))
    self.results["actions"].append(dict(started=container_id))
    self.results["changed"] = True
    if not self.check_mode:
        try:
            self.client.start(container=container_id)
        except Exception as exc:
            self.fail("Error starting container %s: %s" % (container_id, str(exc)))

        if not self.parameters.detach:
            if HAS_DOCKER_PY_3:
                status = self.client.wait(container_id)["StatusCode"]
            else:
                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)
                logging_driver = config["HostConfig"]["LogConfig"]["Type"]

                if logging_driver == "json-file" or logging_driver == "journald":
                    output = self.client.logs(
                        container_id,
                        stdout=True,
                        stderr=True,
                        stream=False,
                        timestamps=False,
                    )
                    if self.parameters.output_logs:
                        self._output_logs(msg=output)
                else:
                    output = "Result logged using `%s` driver" % logging_driver

            if status != 0:
                self.fail(output, status=status)
            if self.parameters.cleanup:
                self.container_remove(
                    container_id, force=True, ignore_failure=self.parameters.auto_remove
                )
            insp = self._get_container(container_id)
            if insp.raw:
                insp.raw["Output"] = output
            else:
                insp.raw = dict(Output=output)
            return insp
    return self._get_container(container_id)