Repository: ansible/ansible

Issue:
docker_container with empty links list always restarts
_From @jcassee on December 2, 2016 16:14_

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
docker_container

##### ANSIBLE VERSION
```
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
*none*

##### OS / ENVIRONMENT
Ubuntu 16.04

##### SUMMARY
When using `links: []`, the container is always restarted.

##### STEPS TO REPRODUCE
Run this playbook twice:
```
- hosts: localhost
  tasks:
    - docker_container:
        name: test
        image: alpine
        links: []
        command: sleep 10000
```

##### EXPECTED RESULTS
The container is started once. It is not restarted when running the second time.

##### ACTUAL RESULTS
The container is restarted on the second run. When running with `-vvv --diff`:

```
TASK [docker_container] ********************************************************
[...]
changed: [localhost] => {
    "ansible_facts": {}, 
    "changed": true, 
    "diff": {
        "differences": [
            {
                "expected_links": {
                    "container": null, 
                    "parameter": []
                }
            }
        ]
    }, 
    "invocation": {
        [...]
```

The problem seems to be that the modules compares an empty list with `None`. From `docker.log` (when enabled):

```
check differences expected_links [] vs None
primitive compare: expected_links
[...]
differences
[
    {
        "expected_links": {
            "container": null,
            "parameter": []
        }
    }
]
```

_Copied from original issue: ansible/ansible-modules-core#5802_


Discussion:
_From @ansibot on December 2, 2016 16:49_

<!-- @jcassee: Thanks for filing this bug report! -->
@ansible ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: issue_bug_new --->
_From @ansibot on December 9, 2016 19:46_

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the <a href="http://docs.ansible.com/ansible/dev_guide/repomerge.html#move-issues-and-prs-to-new-repo">repomerge</a> page in the dev guide. The guide contains links to tools which automatically move your issue or pull request to the ansible/ansible repo.

<!-- boilerplate: repomerge -->
The problem is here: https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/docker/_docker.py#L1439-L1447

When you define a empty list (`[]`) in the argument `links` the output in this section mention above

```
>>> actual_links
set([])
>>> expected_links
set()
```

Resolving patch:
diff --git a/lib/ansible/modules/cloud/docker/_docker.py b/lib/ansible/modules/cloud/docker/_docker.py
--- a/lib/ansible/modules/cloud/docker/_docker.py
+++ b/lib/ansible/modules/cloud/docker/_docker.py
@@ -1440,7 +1440,10 @@ def get_differing_containers(self):
             for link, alias in (self.links or {}).items():
                 expected_links.add("/{0}:{1}/{2}".format(link, container["Name"], alias))
 
-            actual_links = set(container['HostConfig']['Links'] or [])
+            actual_links = set()
+            for link in (container['HostConfig']['Links'] or []):
+                actual_links.add(link)
+
             if actual_links != expected_links:
                 self.reload_reasons.append('links ({0} => {1})'.format(actual_links, expected_links))
                 differing.append(container)


Test patch: