Repository: ansible/ansible-modules-core

Issue:
NameError: global name 'docker' is not defined
**Issue Type:**
    Bug Report

**Ansible Version:**

```
   ansible 1.9 (devel 46b29c3f4b) last updated 2015/01/21 22:45:12 (GMT +100)
   lib/ansible/modules/core: (detached HEAD 396c399b54) last updated 2015/01/21 22:45:21 (GMT +100)
   lib/ansible/modules/extras: (detached HEAD 2396f36f11) last updated 2015/01/21 22:45:30 (GMT +100)
   v2/ansible/modules/core: (detached HEAD 8ba8f5ef4d) last updated 2015/01/21 22:45:39 (GMT +100)
   v2/ansible/modules/extras: (detached HEAD 2396f36f11) last updated 2015/01/21 22:45:45 (GMT +100)
    configured module search path = /usr/share/ansible
```

**Environment:**

```
 Ubuntu 13.10
 Docker-py (0.7.1) 
 Docker version 1.4.1, build 5bc2ff8
```

**Summary:**

Using the docker module in a playbook and running that playbook returns the error below:

```
    TASK: [Run Docker private registry] ******************************************* 
    failed: [localhost] => {"failed": true, "parsed": false}
    SUDO-SUCCESS-bomqzjizfwqbuvuvurlcknjmzfygsfvo
    Traceback (most recent call last):
    File "/home/.ansible/tmp/ansible-tmp-1421886278.27-   209846179492524/docker", line 2543, in <module>
    main()
    File "/home/.ansible/tmp/ansible-tmp-1421886278.27-209846179492524/docker", line 848, in main
    docker_api_version = dict(default=docker.client.DEFAULT_DOCKER_API_VERSION),
    NameError: global name 'docker' is not defined
    FATAL: all hosts have already failed -- aborting

     PLAY RECAP ******************************************************************** 
       to retry, use: --limit @/home/registry.retry
        localhost                  : ok=2    changed=0    unreachable=0    failed=1 
```

**Steps To Reproduce:**

```
       ---
       - name: Using ansible to update docker 
         hosts: local
         sudo: yes

         tasks: 
          - name: Run Docker private registry
            docker:
               docker_api_version: 1.16
               image: registry
               state: running
               expose: 5000
               ports: "127.0.0.1:5000:5000"
               volumes:  "~/docker/registry-storage:/var/www:ro"
               env: "STORAGE_PATH=~/docker/registry-storage"
```

**Expected Results:**

```
      TASK: [Run Docker private registry] *********************************
       ok: [localhost]
```

**Actual Results:**

The error thrown is already pasted in the summary section.



Discussion:
it seems the docker python module is not installed, this is still a bug as checking for the docker module happens after attempts to use it.

Thanks @bcoca  for looking at it. I installed **docker python module** with sudo pip install docker-py and confirmed it was installed by checking **pip list**.


Resolving patch:
diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py
--- a/cloud/docker/docker.py
+++ b/cloud/docker/docker.py
@@ -524,6 +524,8 @@ def __init__(self, module):
         # connect to docker server
         docker_url = urlparse(module.params.get('docker_url'))
         docker_api_version = module.params.get('docker_api_version')
+        if not docker_api_version:
+            docker_api_version=docker.client.DEFAULT_DOCKER_API_VERSION
         self.client = docker.Client(base_url=docker_url.geturl(), version=docker_api_version)
 
         self.docker_py_versioninfo = get_docker_py_versioninfo()
@@ -845,7 +847,7 @@ def main():
             memory_limit    = dict(default=0),
             memory_swap     = dict(default=0),
             docker_url      = dict(default='unix://var/run/docker.sock'),
-            docker_api_version = dict(default=docker.client.DEFAULT_DOCKER_API_VERSION),
+            docker_api_version = dict(),
             username        = dict(default=None),
             password        = dict(),
             email           = dict(),


Test patch: