2022-03-01 02:59:46 +08:00
|
|
|
---
|
2022-04-06 12:36:55 +08:00
|
|
|
|
|
|
|
- name: Docker | Get package facts
|
|
|
|
package_facts:
|
|
|
|
manager: auto
|
|
|
|
|
|
|
|
- name: Docker | Find docker packages
|
|
|
|
set_fact:
|
2024-04-15 10:55:18 +08:00
|
|
|
docker_packages_list: "{{ ansible_facts.packages.keys() | select('search', '^docker+') }}"
|
|
|
|
containerd_package: "{{ ansible_facts.packages.keys() | select('search', '^containerd+') }}"
|
2022-04-06 12:36:55 +08:00
|
|
|
|
|
|
|
- name: Docker | Stop all running container
|
|
|
|
shell: "set -o pipefail && {{ docker_bin_dir }}/docker ps -q | xargs -r {{ docker_bin_dir }}/docker kill"
|
|
|
|
args:
|
|
|
|
executable: /bin/bash
|
|
|
|
register: stop_all_containers
|
|
|
|
retries: 5
|
|
|
|
until: stop_all_containers.rc == 0
|
|
|
|
changed_when: true
|
|
|
|
delay: 5
|
|
|
|
ignore_errors: true # noqa ignore-errors
|
2023-07-05 11:36:54 +08:00
|
|
|
when: docker_packages_list | length>0
|
2022-04-06 12:36:55 +08:00
|
|
|
|
2023-07-26 22:36:22 +08:00
|
|
|
- name: Reset | remove all containers
|
2022-04-06 12:36:55 +08:00
|
|
|
shell: "set -o pipefail && {{ docker_bin_dir }}/docker ps -aq | xargs -r docker rm -fv"
|
|
|
|
args:
|
|
|
|
executable: /bin/bash
|
|
|
|
register: remove_all_containers
|
|
|
|
retries: 4
|
|
|
|
until: remove_all_containers.rc == 0
|
|
|
|
delay: 5
|
2023-07-05 11:36:54 +08:00
|
|
|
when: docker_packages_list | length>0
|
2022-04-06 12:36:55 +08:00
|
|
|
|
|
|
|
- name: Docker | Stop docker service
|
|
|
|
service:
|
|
|
|
name: "{{ item }}"
|
|
|
|
enabled: false
|
|
|
|
state: stopped
|
|
|
|
loop:
|
|
|
|
- docker
|
|
|
|
- docker.socket
|
|
|
|
- containerd
|
2023-07-05 11:36:54 +08:00
|
|
|
when: docker_packages_list | length>0
|
2022-04-06 12:36:55 +08:00
|
|
|
|
|
|
|
- name: Docker | Remove dpkg hold
|
|
|
|
dpkg_selections:
|
|
|
|
name: "{{ item }}"
|
|
|
|
selection: install
|
|
|
|
when: ansible_pkg_mgr == 'apt'
|
|
|
|
changed_when: false
|
|
|
|
with_items:
|
|
|
|
- "{{ docker_packages_list }}"
|
|
|
|
- "{{ containerd_package }}"
|
|
|
|
|
|
|
|
- name: Docker | Remove docker package
|
|
|
|
package:
|
|
|
|
name: "{{ item }}"
|
|
|
|
state: absent
|
|
|
|
changed_when: false
|
|
|
|
with_items:
|
|
|
|
- "{{ docker_packages_list }}"
|
|
|
|
- "{{ containerd_package }}"
|
|
|
|
when:
|
|
|
|
- not ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]
|
|
|
|
- not is_ostree
|
2023-07-05 11:36:54 +08:00
|
|
|
- docker_packages_list | length > 0
|
2022-04-06 12:36:55 +08:00
|
|
|
|
2022-03-01 02:59:46 +08:00
|
|
|
- name: Docker | ensure docker-ce repository is removed
|
|
|
|
apt_repository:
|
|
|
|
repo: "{{ item }}"
|
|
|
|
state: absent
|
|
|
|
with_items: "{{ docker_repo_info.repos }}"
|
|
|
|
when: ansible_pkg_mgr == 'apt'
|
|
|
|
|
|
|
|
- name: Docker | Remove docker repository on Fedora
|
|
|
|
file:
|
|
|
|
name: "{{ yum_repo_dir }}/docker.repo"
|
|
|
|
state: absent
|
|
|
|
when: ansible_distribution == "Fedora" and not is_ostree
|
|
|
|
|
|
|
|
- name: Docker | Remove docker repository on RedHat/CentOS/Oracle/AlmaLinux Linux
|
2022-05-24 20:44:06 +08:00
|
|
|
file:
|
2022-03-01 02:59:46 +08:00
|
|
|
name: "{{ yum_repo_dir }}/docker-ce.repo"
|
|
|
|
state: absent
|
|
|
|
when:
|
|
|
|
- ansible_os_family == "RedHat"
|
|
|
|
- ansible_distribution != "Fedora"
|
|
|
|
- not is_ostree
|
|
|
|
|
|
|
|
- name: Docker | Remove docker configuration files
|
|
|
|
file:
|
|
|
|
name: "{{ item }}"
|
|
|
|
state: absent
|
|
|
|
loop:
|
|
|
|
- /etc/systemd/system/docker.service.d/
|
2022-04-06 12:36:55 +08:00
|
|
|
- /etc/systemd/system/docker.socket
|
|
|
|
- /etc/systemd/system/docker.service
|
|
|
|
- /etc/systemd/system/containerd.service
|
|
|
|
- /etc/systemd/system/containerd.service.d
|
2022-03-01 02:59:46 +08:00
|
|
|
- /var/lib/docker
|
|
|
|
- /etc/docker
|
2022-06-14 17:15:34 +08:00
|
|
|
ignore_errors: true # noqa ignore-errors
|
2022-03-01 02:59:46 +08:00
|
|
|
|
2023-06-30 17:51:57 +08:00
|
|
|
- name: Docker | systemctl daemon-reload # noqa no-handler
|
2022-04-06 12:36:55 +08:00
|
|
|
systemd:
|
|
|
|
daemon_reload: true
|