# Buggy code
def container_remove(self, container_id, link=False, force=False):
    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.results["actions"].append(
        dict(removed=container_id, volume_state=volume_state, link=link, force=force)
    )
    self.results["changed"] = True
    response = None
    if not self.check_mode:
        try:
            response = self.client.remove_container(
                container_id, v=volume_state, link=link, force=force
            )
        except Exception as exc:
            self.fail("Error removing container %s: %s" % (container_id, str(exc)))
    return response


# 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_remove(self, container_id, link=False, force=False, ignore_failure=False):
    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.results["actions"].append(
        dict(removed=container_id, volume_state=volume_state, link=link, force=force)
    )
    self.results["changed"] = True
    response = None
    if not self.check_mode:
        try:
            response = self.client.remove_container(
                container_id, v=volume_state, link=link, force=force
            )
        except Exception as exc:
            if not ignore_failure:
                self.fail("Error removing container %s: %s" % (container_id, str(exc)))
    return response