Repository: ansible/ansible-modules-core

Issue:
Docker module gives error KeyError: 'Memory' when using  remote API v1.19
**Summary**

When running a playbook with _docker_api_version: "1.19"_, the function _get_differing_containers_ gives the following error:

```
Traceback (most recent call last):
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437502622.78-176169208388238/docker", line 3132, in <modu
le>
    main()
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437502622.78-176169208388238/docker", line 1496, in main
    reloaded(manager, containers, count, name)
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437502622.78-176169208388238/docker", line 1373, in reloa
ded
    for container in manager.get_differing_containers():
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437502622.78-176169208388238/docker", line 908, in get_di
ffering_containers
    actual_mem = container['Config']['Memory']
KeyError: 'Memory'
```

If  _docker_api_version: "1.18"_ is used it does not give an error and deploys the container successfully. It appears it could be related to a change in Remote API v1.19 where,

> The fields Debug, IPv4Forwarding, MemoryLimit, and SwapLimit are now returned as boolean instead of as an int.
> -[Docker Remote API v1.19 Changes](https://docs.docker.com/reference/api/docker_remote_api/#v1-19)

**Ansible Version**

```
 ansible 1.9.2
```

**Ansible Configuration**

```
 host_key_checking = False
 scp_if_ssh = True
```

**Playbook**

```
  - name: Deploy NGINX container
    docker:
      name: nginx
      registry: "private-docker-registry"
      docker_api_version: "1.19"
      pull: always
      tty: yes
      image: private-docker-registry/nginx
      state: reloaded
      net: bridge
      ports:
      - "0.0.0.0:8090:8090"
      volumes:
      - /mnt/nginx/:nginx:ro
```

**Environment**
- Ubuntu 14.04 ansible master
  - ansible v1.9.2
- CentOS 6.6 container host 
  - Docker v1.7.1 
  - docker-py v1.3.0

**Steps to Reproduce**
- Install Ansible v1.9.2 on Ubuntu 14.04
- Install Docker v1.7.1 and docker-py v1.3.0 on CentOS 6.6
- Create a playbook with _docker_api_version: "1.19"_ to use Remote API version 1.19
- Run playbook on Ubuntu ansible host to deploy a container on the CentOS container host



Discussion:
It looks like ansible-modules-core/cloud/docker/docker.py#L1011 is referencing the Config element which does not have a Memory attribute,

```
 actual_mem = container['Config']['Memory']
```

The HostConfig element contains the proper Memory value

```
 actual_mem = container['HostConfig']['Memory']
```


Resolving patch:
diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py
--- a/cloud/docker/docker.py
+++ b/cloud/docker/docker.py
@@ -942,6 +942,9 @@ def get_differing_containers(self):
         running = self.get_running_containers()
         current = self.get_inspect_containers(running)
 
+        #Get API version
+        api_version = self.client.version()['ApiVersion']
+
         image = self.get_inspect_image()
         if image is None:
             # The image isn't present. Assume that we're about to pull a new
@@ -1008,7 +1011,11 @@ def get_differing_containers(self):
             except ValueError as e:
                 self.module.fail_json(msg=str(e))
 
-            actual_mem = container['Config']['Memory']
+            #For v1.19 API and above use HostConfig, otherwise use Config
+            if docker_api_version >= 1.19:
+                actual_mem = container['HostConfig']['Memory']
+            else:
+                actual_mem = container['Config']['Memory']
 
             if expected_mem and actual_mem != expected_mem:
                 self.reload_reasons.append('memory ({0} => {1})'.format(actual_mem, expected_mem))


Test patch: