Skip to content

Commit

Permalink
Update podman_image to remove image with image id (#434)
Browse files Browse the repository at this point in the history
* Update to remove image with image id.

Signed-off-by: nishipy <goodisonev4@gmail.com>
  • Loading branch information
nishipy authored Jun 8, 2022
1 parent b33a657 commit acedce8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
39 changes: 39 additions & 0 deletions plugins/modules/podman_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@
name: quay.io/bitnami/wildfly
state: absent
- name: Remove an image with image id
containers.podman.podman_image:
name: 0e901e68141f
state: absent
- name: Pull a specific version of an image
containers.podman.podman_image:
name: redis
Expand Down Expand Up @@ -490,13 +495,21 @@ def present(self):

def absent(self):
image = self.find_image()
image_id = self.find_image_id()

if image:
self.results['actions'].append('Removed image {name}'.format(name=self.name))
self.results['changed'] = True
self.results['image']['state'] = 'Deleted'
if not self.module.check_mode:
self.remove_image()
elif image_id:
self.results['actions'].append(
'Removed image with id {id}'.format(id=self.image_name))
self.results['changed'] = True
self.results['image']['state'] = 'Deleted'
if not self.module.check_mode:
self.remove_image_id()

def find_image(self, image_name=None):
if image_name is None:
Expand All @@ -508,6 +521,19 @@ def find_image(self, image_name=None):
else:
return None

def find_image_id(self, image_id=None):
if image_id is None:
# If image id is set as image_name, remove tag
image_id = re.sub(':.*$', '', self.image_name)
args = ['image', 'ls', '--quiet', '--no-trunc']
rc, candidates, err = self._run(args, ignore_errors=True)
candidates = [re.sub('^sha256:', '', c)
for c in str.splitlines(candidates)]
for c in candidates:
if c.startswith(image_id):
return image_id
return None

def inspect_image(self, image_name=None):
if image_name is None:
image_name = self.image_name
Expand Down Expand Up @@ -694,6 +720,19 @@ def remove_image(self, image_name=None):
self.module.fail_json(msg='Failed to remove image {image_name}. {err}'.format(image_name=image_name, err=err))
return out

def remove_image_id(self, image_id=None):
if image_id is None:
image_id = re.sub(':.*$', '', self.image_name)

args = ['rmi', image_id]
if self.force:
args.append('--force')
rc, out, err = self._run(args, ignore_errors=True)
if rc != 0:
self.module.fail_json(msg='Failed to remove image with id {image_id}. {err}'.format(
image_id=image_id, err=err))
return out


def parse_repository_tag(repo_name):
parts = repo_name.rsplit('@', 1)
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/targets/podman_image/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
name: docker.io/library/alpine
register: pull5

- name: Pull image for testing image deletion with image id
containers.podman.podman_image:
name: docker.io/library/ubuntu
register: pull6

- name: List images
command: podman image ls
register: images
Expand All @@ -38,8 +43,10 @@
- pull3 is changed
- pull4 is changed
- pull5 is not changed
- pull6 is changed
- "'alpine-sh' in images.stdout"
- "'library/alpine' in images.stdout"
- "'library/ubuntu' in images.stdout"

- name: add another tag (repository url)
command:
Expand Down Expand Up @@ -79,6 +86,18 @@
state: absent
register: rmi5

- name: Get image id of the target image
containers.podman.podman_image_info:
name: docker.io/library/ubuntu
register: imageinfo

- name: Remove an image with image id
containers.podman.podman_image:
name: "{{ item.Id }}"
state: absent
loop: "{{ imageinfo.images }}"
register: rmi6

- name: List images
command: podman image ls
register: images
Expand All @@ -91,7 +110,9 @@
- rmi3 is changed
- rmi4 is not changed
- rmi5 is changed
- rmi6 is changed
- "'alpine-sh' not in images.stdout"
- "'library/ubuntu' not in images.stdout"

- name: Pull a specific version of an image
containers.podman.podman_image:
Expand Down

0 comments on commit acedce8

Please sign in to comment.