118 lines
3.8 KiB
YAML

---
- name: Debug distribution information
debug:
msg:
- "Distribution: {{ ansible_facts['distribution'] }}"
- "Distribution Release: {{ ansible_facts['distribution_release'] }}"
- "Distribution Version: {{ ansible_facts['distribution_version'] }}"
- "OS Family: {{ ansible_facts['os_family'] }}"
- name: Check if Docker is already installed
command: docker --version
register: docker_check
ignore_errors: true
changed_when: false
- name: Check if Docker packages are installed via apt
apt:
list: docker-ce
register: docker_apt_check
changed_when: false
- name: Install Docker requirements
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
state: present
update_cache: yes
- name: Remove old Docker repository files
file:
path: "{{ item }}"
state: absent
loop:
- /etc/apt/sources.list.d/docker.list
- /etc/apt/sources.list.d/docker-ce.list
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Create keyrings directory
file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Download Docker's official GPG key
get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.gpg
mode: '0644'
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Add Docker repository for Ubuntu
apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
when:
- ansible_facts['distribution'] == "Ubuntu"
- docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Add Docker repository for Debian
apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"
state: present
when:
- ansible_facts['distribution'] == "Debian"
- docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Add Docker repository for Linux Mint
apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
when:
- ansible_facts['distribution'] == "Linux Mint"
- docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Update apt cache after Docker repo add
apt:
update_cache: yes
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Install Docker CE
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
update_cache: yes
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Start and enable Docker service
systemd:
name: docker
state: started
enabled: yes
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Add user to docker group
user:
name: "{{ ansible_user }}"
groups: docker
append: yes
when: docker_check.rc != 0 or docker_apt_check.results[0].installed is not defined
- name: Display Docker status
debug:
msg:
- "Docker already installed: {{ docker_check.stdout if docker_check.rc == 0 else 'Not found' }}"
- "Docker CE package installed: {{ 'Yes' if docker_apt_check.results[0].installed is defined else 'No' }}"
- "Actions taken: {{ 'None - Docker already present' if docker_check.rc == 0 and docker_apt_check.results[0].installed is defined else 'Docker installation/configuration performed' }}"