From a11073df9ad65fe1e242afc02c6da3ddc199decc Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 23 Oct 2018 08:52:36 +0200 Subject: [PATCH] docker_image: allow to delete image by ID (#47393) * Allow to delete docker image by ID. * Added changelog. --- .../fragments/47393-docker_image-id.yaml | 2 ++ .../modules/cloud/docker/docker_image.py | 22 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/47393-docker_image-id.yaml diff --git a/changelogs/fragments/47393-docker_image-id.yaml b/changelogs/fragments/47393-docker_image-id.yaml new file mode 100644 index 00000000000..9f2ef1a5023 --- /dev/null +++ b/changelogs/fragments/47393-docker_image-id.yaml @@ -0,0 +1,2 @@ +minor_changes: +- "docker_image - Allow to use image ID instead of image name for deleting images." diff --git a/lib/ansible/modules/cloud/docker/docker_image.py b/lib/ansible/modules/cloud/docker/docker_image.py index be9943d5be8..b6ef08c4c3d 100644 --- a/lib/ansible/modules/cloud/docker/docker_image.py +++ b/lib/ansible/modules/cloud/docker/docker_image.py @@ -250,7 +250,9 @@ image: import os import re -from ansible.module_utils.docker_common import HAS_DOCKER_PY_2, HAS_DOCKER_PY_3, AnsibleDockerClient, DockerBaseClass +from ansible.module_utils.docker_common import ( + HAS_DOCKER_PY_2, HAS_DOCKER_PY_3, AnsibleDockerClient, DockerBaseClass, is_image_name_id, +) from ansible.module_utils._text import to_native try: @@ -293,10 +295,11 @@ class ImageManager(DockerBaseClass): self.buildargs = parameters.get('buildargs') # If name contains a tag, it takes precedence over tag parameter. - repo, repo_tag = parse_repository_tag(self.name) - if repo_tag: - self.name = repo - self.tag = repo_tag + if not is_image_name_id(self.name): + repo, repo_tag = parse_repository_tag(self.name) + if repo_tag: + self.name = repo + self.tag = repo_tag if self.state in ['present', 'build']: self.present() @@ -363,11 +366,14 @@ class ImageManager(DockerBaseClass): :return None ''' - image = self.client.find_image(self.name, self.tag) - if image: - name = self.name + name = self.name + if is_image_name_id(name): + image = self.client.find_image_by_id(name) + else: + image = self.client.find_image(name, self.tag) if self.tag: name = "%s:%s" % (self.name, self.tag) + if image: if not self.check_mode: try: self.client.remove_image(name, force=self.force)