Skip to content

Commit

Permalink
[NEW] added uninstall playbook to docker install playbook and made fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Sep 17, 2024
1 parent 980b8b2 commit 2322135
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 44 deletions.
41 changes: 41 additions & 0 deletions Ansible/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 📋 Docker Installation and Uninstallation Playbook

This playbook installs Docker on multiple Linux operating systems and provides a method to uninstall Docker, including the cleanup of associated files and directories.

## 🛠️ Usage

### Install Docker
Run the following command to install Docker on your servers:
```bash
ansible-playbook -i inventory.ini ./install_docker.yml
```
# Clean up and Uninstall Docker
To clean up and uninstall Docker from your servers, use the following command:
```bash
ansible-playbook -i inventory.ini ./uninstall_docker.yml
```
This will:
* Stop all Docker services.
* Remove all Docker containers, images, volumes, and associated files.
* Remove the Docker GPG keys and repository sources.
* Uninstall Docker packages.
* Remove Docker user and group.


# 💻 Supported Linux Operating Systems
* 🐧 **Debian:** 11,12
* 🐧 **Ubuntu:** 20.04,22.04
* 🐧 **RHEL:** 7,8
* 🐧 **Fedora:** 39,40

# ✅ Tested Operating Systems

***Debian:** 11,12
***Ubuntu:** 20.04,22.04
***RHEL:** 7,8

# ⚙️ Supported Ansible Versions
* ✅ ansible [core 2.16.3]
* ❗️ ansible [core 2.17.3] (compatibility issues)

> Note: The playbook assumes you are running Ansible as the root user. For non-root users, ensure you have become privileges configured.
69 changes: 25 additions & 44 deletions Ansible/docker/install_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
docker-engine
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

- name: Install Docker on Ubuntu
- name: Install Docker on Ubuntu/Debian
block:
- name: Install dependencies
ansible.builtin.apt:
Expand All @@ -41,16 +41,17 @@
loop:
- ca-certificates
- curl
- gnupg

- name: Add Docker’s official GPG key
ansible.builtin.shell: |
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
curl -fsSL https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
- name: Add Docker APT repository
ansible.builtin.shell: |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/{{ ansible_distribution | lower }} $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
apt-get update
- name: Install Docker
Expand All @@ -61,52 +62,18 @@
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- docker-compose
state: present
when: ansible_distribution == 'Ubuntu'

- name: Install Docker on Debian
block:
- name: Install dependencies
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- ca-certificates
- curl

- name: Add Docker’s official GPG key
ansible.builtin.shell: |
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
- name: Add Docker APT repository
ansible.builtin.shell: |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
apt-get update
- name: Install Docker
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- docker-compose
state: present
when: ansible_distribution == 'Debian'
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Install Docker on CentOS/RHEL
block:
- name: Install yum-utils
- name: Install dependencies
ansible.builtin.yum:
name: yum-utils
state: present

- name: Add Docker repository
ansible.builtin.shell: yum-config-manager --add-repo https://download.docker.com/linux/{{ ansible_distribution | lower }}/docker-ce.repo
ansible.builtin.shell: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

- name: Install Docker
ansible.builtin.yum:
Expand All @@ -116,9 +83,8 @@
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- docker-compose
state: present
when: ansible_distribution in ['CentOS', 'RedHat']
when: ansible_distribution in ['RedHat', 'CentOS']

- name: Install Docker on Fedora
block:
Expand All @@ -138,10 +104,25 @@
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- docker-compose
state: present
when: ansible_distribution == 'Fedora'

- name: Install Docker on SLES (s390x)
block:
- name: Add OpenSUSE SELinux repository
ansible.builtin.shell: |
zypper addrepo https://download.opensuse.org/repositories/security:/SELinux/openSUSE_Factory/security:SELinux.repo
- name: Add Docker repository and install
ansible.builtin.zypper:
name:
- docker
- docker-compose
- containerd
- runc
state: present
when: ansible_distribution == 'SLES'

- name: Start and enable Docker
ansible.builtin.systemd:
name: docker
Expand All @@ -159,4 +140,4 @@
path: /var/run/docker.sock
owner: "{{ ansible_user_id }}"
group: docker
mode: '0666'
mode: '0666'
129 changes: 129 additions & 0 deletions Ansible/docker/uninstall_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
- name: Uninstall Docker on multiple OS
hosts: all
become: true
tasks:
- name: Gather facts
ansible.builtin.setup:
filter: ansible_distribution*

- name: Check if Docker is installed
ansible.builtin.command:
cmd: "which docker"
register: docker_installed
ignore_errors: true
failed_when: docker_installed.rc != 0 and docker_installed.rc != 1


- name: Stop all running containers (if Docker is found)
ansible.builtin.shell: docker stop $(docker ps -q)
when: docker_installed.rc == 0
ignore_errors: yes

- name: Remove all stopped containers (if Docker is found)
ansible.builtin.shell: docker rm $(docker ps -a -q)
when: docker_installed.rc == 0
ignore_errors: yes

- name: Remove all Docker images (if Docker is found)
ansible.builtin.shell: docker rmi $(docker images -a -q)
when: docker_installed.rc == 0
ignore_errors: yes

- name: Remove all Docker volumes (if Docker is found)
ansible.builtin.shell: docker volume prune -f
when: docker_installed.rc == 0
ignore_errors: yes

- name: Prune all Docker system files (if Docker is found)
ansible.builtin.shell: docker system prune -a -f --volumes
when: docker_installed.rc == 0
ignore_errors: yes

- name: Stop Docker service (if Docker is found)
ansible.builtin.systemd:
name: docker
state: stopped
when: docker_installed.rc == 0
ignore_errors: yes

- name: Remove Docker packages (Ubuntu/Debian)
ansible.builtin.apt:
name: "{{ item }}"
state: absent
purge: yes
loop:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
- docker-buildx-plugin
- docker.io
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Remove Docker packages (CentOS/RHEL/Fedora)
ansible.builtin.yum:
name:
- docker
- docker-client
- docker-client-latest
- docker-common
- docker-latest
- docker-latest-logrotate
- docker-logrotate
- docker-engine
- containerd.io
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

- name: Remove Docker packages (SLES)
community.general.zypper:
name: docker
state: absent
when: ansible_distribution == 'SLES'

- name: Remove leftover Docker files and directories
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- /var/lib/docker
- /etc/docker
- /var/run/docker.sock
- /usr/local/bin/docker-compose
- /root/.docker
- /home/{{ ansible_user_id }}/.docker
- /var/lib/containerd

- name: Remove Docker group
ansible.builtin.group:
name: docker
state: absent

- name: Clean up APT sources (Ubuntu/Debian)
file:
path: /etc/apt/sources.list.d/docker.list
state: absent
when: ansible_distribution in ['Ubuntu', 'Debian']
ignore_errors: yes

- name: Clean up Yum/DNF sources (CentOS/RHEL/Fedora)
file:
path: /etc/yum.repos.d/docker-ce.repo
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']
ignore_errors: yes

- name: Remove Docker GPG key (Ubuntu/Debian)
ansible.builtin.apt_key:
id: "{{ item }}"
state: absent
loop:
- 9DC858229FC7DD38854AE2D88D81803C0EBFCD88 # Docker GPG key for Ubuntu/Debian
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Remove Docker GPG key (CentOS/RHEL/Fedora)
ansible.builtin.rpm_key:
key: https://download.docker.com/linux/centos/gpg
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

0 comments on commit 2322135

Please sign in to comment.