Repository: ansible/ansible-modules-core

Issue:
[Docker] Old containers aren't cleaned up when using count
**Issue Type:**

Bug Report 

**Ansible Version:**

ansible 1.9 (devel 85cfe1bd52)

**Environment:**

Ubuntu 14.04

**Summary:**

When using `count`, old containers are not removed

**Steps To Reproduce:**

Create containers with `count: 2`, `pull: always`, and `state: reloaded`. Run task to pull an updated image.

```
- name: run node client container
  docker:
    image: "company/tv-client:dev-latest"
    env: "SERVICE_NAME=client,SERVICE_TAGS=dev"
    ports: 80
    restart_policy: always
    pull: always
    state: reloaded
    count: 2
```

**Expected Results:**

The 2 original containers are gone and 2 new ones with the latest image are running in their place

**Actual Results:**

The 2 original containers are still running alongside the new containers, but without image tags

```
CONTAINER ID        IMAGE                                  COMMAND                CREATED             STATUS              PORTS                            NAMES
eda5d790bbc1        company/tv-client:dev-latest           "nginx -g 'daemon of   5 seconds ago       Up 5 seconds        443/tcp, 0.0.0.0:49204->80/tcp   serene_blackwell
fce2c32876eb        company/tv-client:dev-latest           "nginx -g 'daemon of   6 seconds ago       Up 5 seconds        443/tcp, 0.0.0.0:49203->80/tcp   agitated_lumiere
05ef9af927ee        147b87b45e35                           "nginx -g 'daemon of   20 minutes ago      Up 20 minutes       443/tcp, 0.0.0.0:49202->80/tcp   clever_pike
0bdeb3ef8f37        147b87b45e35                           "nginx -g 'daemon of   20 minutes ago      Up 20 minutes       443/tcp, 0.0.0.0:49201->80/tcp   lonely_nobel
```



Discussion:
CCing @smashwilson

Thanks. Yeah, `get_deployed_containers` still needs some work.


Resolving patch:
diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py
--- a/cloud/docker/docker.py
+++ b/cloud/docker/docker.py
@@ -420,6 +420,13 @@ def get_split_image_tag(image):
 
     return resource, tag
 
+def normalize_image(image):
+    """
+    Normalize a Docker image name to include the implied :latest tag.
+    """
+
+    return ":".join(get_split_image_tag(image))
+
 
 def is_running(container):
     '''Return True if an inspected container is in a state we consider "running."'''
@@ -1109,16 +1116,20 @@ def get_deployed_containers(self):
         if inspected:
             repo_tags = self.get_image_repo_tags()
         else:
-            image, tag = get_split_image_tag(self.module.params.get('image'))
-            repo_tags = [':'.join([image, tag])]
+            repo_tags = [normalize_image(self.module.params.get('image'))]
 
         for i in self.client.containers(all=True):
-            running_image = i['Image']
-            running_command = i['Command'].strip()
+            details = None
 
             if name:
                 matches = name in i.get('Names', [])
             else:
+                details = self.client.inspect_container(i['Id'])
+                details = _docker_id_quirk(details)
+
+                running_image = normalize_image(details['Config']['Image'])
+                running_command = i['Command'].strip()
+
                 image_matches = running_image in repo_tags
 
                 # if a container has an entrypoint, `command` will actually equal
@@ -1128,8 +1139,9 @@ def get_deployed_containers(self):
                 matches = image_matches and command_matches
 
             if matches:
-                details = self.client.inspect_container(i['Id'])
-                details = _docker_id_quirk(details)
+                if not details:
+                    details = self.client.inspect_container(i['Id'])
+                    details = _docker_id_quirk(details)
 
                 deployed.append(details)
 


Test patch: