mirror of
https://github.com/containers/ansible-podman-collections.git
synced 2026-03-22 02:29:08 +00:00
Update podman_image to remove image with image id (#434)
* Update to remove image with image id. Signed-off-by: nishipy <goodisonev4@gmail.com>
This commit is contained in:
parent
b33a657f00
commit
acedce8b4b
2 changed files with 60 additions and 0 deletions
|
|
@ -173,6 +173,11 @@ EXAMPLES = r"""
|
|||
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
|
||||
|
|
@ -490,6 +495,7 @@ class PodmanImageManager(object):
|
|||
|
||||
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))
|
||||
|
|
@ -497,6 +503,13 @@ class PodmanImageManager(object):
|
|||
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:
|
||||
|
|
@ -508,6 +521,19 @@ class PodmanImageManager(object):
|
|||
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
|
||||
|
|
@ -694,6 +720,19 @@ class PodmanImageManager(object):
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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:
|
||||
|
|
@ -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
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue