Home > Blockchain >  Remove all images using Ansible module
Remove all images using Ansible module

Time:08-24

I know how remove all images using shell:

- shell: |
      docker stop $(docker ps -a -q)
      docker rm $(docker ps -a -q)
      docker rmi $(docker images -q)
      exit 0

Also I can remove image with docker_image by name:

- name: Remove image
  docker_image:
    state: absent
    name: {{image_name}}

But, is there a way to remove all images using docker_image module or any other Ansible module for working with docker?

CodePudding user response:

You will have to do it in multiple steps, like your sub-shells are doing it:

  1. list docker objects, including images and containers, with the module docker_host_info
  2. remove all those containers, with the module docker_container
  3. remove all those images, with the module docker_image module

So, in Ansible:

- docker_host_info:
    images: yes
    containers: yes
  register: docker_objects

- docker_container:
    name: "{{ item.Id }}"
    state: absent
  loop: "{{ docker_objects.containers }}"
  loop_control:
    label: "{{ item.Id }}"

- docker_image:
    name: item.RepoTags | first
    state: absent
    force_absent: yes
  loop: "{{ docker_objects.images }}"
  loop_control:
    label: "{{ item.RepoTags | first }}"
  • Related