Repository: ansible/ansible

Issue:
Could not run attached docker container with syslog logging driver
##### ISSUE TYPE

Bug Report

##### COMPONENT NAME

`modules/cloud/docker/docker_container.py`

##### ANSIBLE VERSION

```
ansible 2.3.1.0
  config file = /Users/bolshakov/Documents/Projects/amediateka/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Apr  4 2017, 08:47:57) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)]
```

##### OS / ENVIRONMENT

N/A

##### SUMMARY

According to the [docker documentation](https://docs.docker.com/engine/api/v1.30/#operation/ContainerLogs) it possible to run `docker logs` command only if you're using either `json-file` or `journald` logging driver.

When you run a container with another deriver (e.g. `none`, or `syslog`) ansible fails with error 

```
docker.errors.APIError: 500 Server Error: Internal Server Error (\"configured logging reader does not support reading\")
```

The error happens because we are trying to read [logs after starting a container](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/docker/docker_container.py#L1952)

```python
    def container_start(self, container_id):
       # ... skipped 
       self.client.start(container=container_id)
       
       if not self.parameters.detach:
         status = self.client.wait(container_id)
         output = self.client.logs(container_id, stdout=True, stderr=True, stream=False, timestamps=False)
       # ... skipped
```

but `self.client.logs` always fails for all logging drivers except `json-file` and `journald`.

##### STEPS TO REPRODUCE

    ansible-playbook -i ansible/inventories/dev ansible/run.yml

```yaml
> cat run.yml

- name: Deploy
  hosts:
    - application
  tasks:
    - name: Pull image
      docker_container:
        name: test-syslog
        image: busybox:latest
        state: started
        detach: false
        pull: true
        command: echo hello
        log_driver: syslog
        log_options:
          syslog-facility: local4
          syslog-address: udp://127.0.0.1:514
```

##### EXPECTED RESULTS

It runs container without an error.

##### ACTUAL RESULTS

```
TASK [Pull image] ***********************************************************************************************************************************************************************************************************************
fatal: [app.local]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to 127.0.0.1 closed.
", "module_stdout": "Traceback (most recent call last):
  File \"/tmp/ansible_TLpSBI/ansible_module_docker_container.py\", line 2140, in <module>
    main()
  File \"/tmp/ansible_TLpSBI/ansible_module_docker_container.py\", line 2133, in main
    cm = ContainerManager(client)
  File \"/tmp/ansible_TLpSBI/ansible_module_docker_container.py\", line 1766, in __init__
    self.present(state)
  File \"/tmp/ansible_TLpSBI/ansible_module_docker_container.py\", line 1813, in present
    container = self.container_start(container.Id)
  File \"/tmp/ansible_TLpSBI/ansible_module_docker_container.py\", line 1969, in container_start
    output = self.client.logs(container_id, stdout=True, stderr=True, stream=False, timestamps=False)
  File \"/usr/lib/python2.7/site-packages/docker/utils/decorators.py\", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File \"/usr/lib/python2.7/site-packages/docker/api/container.py\", line 839, in logs
    return self._get_result(container, stream, res)
  File \"/usr/lib/python2.7/site-packages/docker/api/client.py\", line 403, in _get_result
    return self._get_result_tty(stream, res, cont['Config']['Tty'])
  File \"/usr/lib/python2.7/site-packages/docker/api/client.py\", line 417, in _get_result_tty
    self._raise_for_status(res)
  File \"/usr/lib/python2.7/site-packages/docker/api/client.py\", line 222, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File \"/usr/lib/python2.7/site-packages/docker/errors.py\", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error: Internal Server Error (\"configured logging reader does not support reading\")
", "msg": "MODULE FAILURE", "rc": 0}
```

I formatted output a bit.


Discussion:
cc @ThomasSteinbach @cove @dusdanig @jctanner @joshuaconner @kassiansun @softzilla @zfil
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py
--- a/lib/ansible/modules/cloud/docker/docker_container.py
+++ b/lib/ansible/modules/cloud/docker/docker_container.py
@@ -1961,7 +1961,14 @@ def container_start(self, container_id):
 
             if not self.parameters.detach:
                 status = self.client.wait(container_id)
-                output = self.client.logs(container_id, stdout=True, stderr=True, stream=False, timestamps=False)
+                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)
+                else:
+                    output = "Result logged using `%s` driver" % logging_driver
+
                 if status != 0:
                     self.fail(output, status=status)
                 if self.parameters.cleanup:


Test patch: