Repository: ansible-collections/community.general

Issue:
docker_compose restarts stopped containers with stopped parameter
see original issues: https://github.com/ansible/ansible/issues/69728 

##### COMPONENT NAME
docker_compose

Error:

```
Unable to load docker-compose. Try `pip install docker-compose`. Error: No module named compose
```

playbook:

```yaml
  roles:
  # Install most docker_service dependencies
  - { role: "nickjj.docker", tags: ["docker"] }

  tasks:
  - name: test docker-compose
    docker_service:
      project_src: test
```

Stack trace:

```
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_docker_service_payload_U2NKy6/__main__.py", line 456, in <module>
    from compose import __version__ as compose_version

fatal: [138.xx.xx.183]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "api_version": "auto",
            "build": false,
            "cacert_path": null,
            "cert_path": null,
            "debug": false,
            "definition": null,
            "dependencies": true,
            "docker_host": "unix://var/run/docker.sock",
            "files": null,
            "hostname_check": false,
            "key_path": null,
            "nocache": false,
            "project_name": null,
            "project_src": "../<...>",
            "pull": false,
            "recreate": "smart",
            "remove_images": null,
            "remove_orphans": false,
            "remove_volumes": false,
            "restarted": false,
            "scale": null,
            "services": null,
            "ssl_version": null,
            "state": "present",
            "stopped": false,
            "timeout": 10,
            "tls": false,
            "tls_hostname": "localhost",
            "tls_verify": false
        }
    },
    "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: No module named compose"
}
```

## Workaround:
Running `pip install docker-compose` on the server managed by ansible fixed the issue.

## Version infos
galaxy info:

```
Role: nickjj.docker
	description: Install Docker and optionally Docker Compose.
	active: True
	commit: d498cc15f393312ae9e104e13cc3ac7d07365c8d
	commit_message: Update README to link to the latest release
	commit_url: https://api.github.com/repos/nickjj/ansible-docker/git/commits/d498cc15f393312ae9e104e13cc3ac7d07365c8d
	company: 
	created: 2016-10-08T23:13:50.040014Z
	dependencies: []
	download_count: 2838
	forks_count: 69
	galaxy_info:
		author: Nick Janetakis
		galaxy_tags: ['containers', 'compose', 'docker', 'packaging', 'system']
		license: MIT
		min_ansible_version: 2.5
		platforms: [{'name': 'Ubuntu', 'versions': ['xenial', 'bionic']}, {'name': 'Debian', 'versions': ['jessie', 'stretch']}]
		role_name: docker
	github_branch: master
	github_repo: ansible-docker
	github_user: nickjj
	id: 12606
	imported: 2018-11-08T21:18:19.792556-05:00
	install_date: Thu Nov 15 16:08:42 2018
	intalled_version: v1.5.0
	is_valid: True
	issue_tracker_url: https://github.com/nickjj/ansible-docker/issues
	license: MIT
	min_ansible_version: 2.5
	modified: 2018-11-09T02:18:19.792718Z
	open_issues_count: 0
	path: ['/home/xxx/.ansible/roles', '/usr/share/ansible/roles', '/etc/ansible/roles']
	role_type: ANS
	stargazers_count: 195
	travis_status_url: 
```

ansible version:

```
ansible --version
ansible 2.7.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/olivier/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.1 (default, Oct 22 2018, 10:41:28) [GCC 8.2.1 20180831]
```



Discussion:
Files identified in the description:
None

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
@tonal: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:
- component name

Please set the description of this issue with this template:
https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE/bug_report.md

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: issue_missing_data --->
COMPONENT NAME
docker_compose
!component =/plugins/modules/cloud/docker/docker_compose
Files identified in the description:
* [`/plugins/modules/cloud/docker/docker_compose`](https://github.com/ansible-collections/community.general/blob/master//plugins/modules/cloud/docker/docker_compose)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
!component =plugins/modules/cloud/docker/docker_compose.py
Files identified in the description:
* [`plugins/modules/cloud/docker/docker_compose.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/cloud/docker/docker_compose.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @DBendit @WojciechowskiPiotr @akshay196 @chouseknecht @danihodovic @dariko @jwitko @kassiansun @sluther @tbouvet
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I'm a bit confused. IMO this issue and it's description is completely unrelated to the initial issue. Don't know if it would be better to open a new issue, but at the end I run into the same issue as described by the original author of ansible/ansible#69728. For whatever reason the `docker_compose` module restarts the server even if `stopped: yes` is set. This also breaks the idempotency of the module and report `changed` on every run.

Resolving patch:
diff --git a/plugins/modules/cloud/docker/docker_compose.py b/plugins/modules/cloud/docker/docker_compose.py
--- a/plugins/modules/cloud/docker/docker_compose.py
+++ b/plugins/modules/cloud/docker/docker_compose.py
@@ -639,6 +639,9 @@ def __init__(self, client):
                              "Upgrade docker-compose to a min version of %s." %
                              (compose_version, MINIMUM_COMPOSE_VERSION, MINIMUM_COMPOSE_VERSION))
 
+        if self.restarted and self.stopped:
+            self.client.fail("Cannot use restarted and stopped at the same time.")
+
         self.log("options: ")
         self.log(self.options, pretty_print=True)
 
@@ -767,7 +770,7 @@ def cmd_up(self):
                         ))
                     result['actions'].append(result_action)
 
-        if not self.check_mode and result['changed']:
+        if not self.check_mode and result['changed'] and not self.stopped:
             out_redir_name, err_redir_name = make_redirection_tempfiles()
             try:
                 with stdout_redirector(out_redir_name):


Test patch: