Repository: ansible/ansible-modules-core

Issue:
docker_service - NameError: global name 'compose_version' is not defined
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/module/task -->

docker_service
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/usr/share/ansible/modules/']
```
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

Ubuntu 16.04
Kernel: 4.4.0-21-generic x86_64
Docker-py: 1.8.0
Docker API: 1.23
##### SUMMARY

<!--- Explain the problem briefly -->

Trying to use the `docker_service` module but get error when running a very basic playbook. The error message is **NameError: global name 'compose_version' is not defined**:

```
fatal: []: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "\r\nTraceback (most recent call last):\r\n  File \"/tmp/ansible_PxFd_Y/ansible_module_docker_service.py\", line 807, in <module>\r\n    main()\r\n  File \"/tmp/ansible_PxFd_Y/ansible_module_docker_service.py\", line 802, in main\r\n    result = ContainerManager(client).exec_module()\r\n  File \"/tmp/ansible_PxFd_Y/ansible_module_docker_service.py\", line 487, in __init__\r\n    if LooseVersion(compose_version) < LooseVersion(MINIMUM_COMPOSE_VERSION):\r\nNameError: global name 'compose_version' is not defined\r\n", "msg": "MODULE FAILURE", "parsed": false}
```
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

I only need to include the `docker_service` directive and get the error.

<!--- Paste example playbooks or commands between quotes below -->

```
- docker_service:
    project_src: blabla
    state: absent
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

Status OK:
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

fatal: []: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "\r\nTraceback (most recent call last):\r\n  File \"/tmp/ansible_PxFd_Y/ansible_module_docker_service.py\", line 807, in <module>\r\n    main()\r\n  File \"/tmp/ansible_PxFd_Y/ansible_module_docker_service.py\", line 802, in main\r\n    result = ContainerManager(client).exec_module()\r\n  File \"/tmp/ansible_PxFd_Y/ansible_module_docker_service.py\", line 487, in **init**\r\n    if LooseVersion(compose_version) < LooseVersion(MINIMUM_COMPOSE_VERSION):\r\nNameError: global name 'compose_version' is not defined\r\n", "msg": "MODULE FAILURE", "parsed": false}



Discussion:

Resolving patch:
diff --git a/cloud/docker/docker_service.py b/cloud/docker/docker_service.py
--- a/cloud/docker/docker_service.py
+++ b/cloud/docker/docker_service.py
@@ -484,14 +484,14 @@ def __init__(self, client):
         if self.files:
             self.options[u'--file'] = self.files
 
+        if not HAS_COMPOSE:
+            self.client.fail("Unable to load docker-compose. Try `pip install docker-compose`. Error: %s" % HAS_COMPOSE_EXC)
+
         if LooseVersion(compose_version) < LooseVersion(MINIMUM_COMPOSE_VERSION):
             self.client.fail("Found docker-compose version %s. Minimum required version is %s. "
                              "Upgrade docker-compose to a min version of %s." %
                              (compose_version, MINIMUM_COMPOSE_VERSION, MINIMUM_COMPOSE_VERSION))
 
-        if not HAS_COMPOSE:
-            self.client.fail("Unable to load docker-compose. Try `pip install docker-compose`. Error: %s" % HAS_COMPOSE_EXC)
-
         self.log("options: ")
         self.log(self.options, pretty_print=True)
 


Test patch: