2024-09-09 13:34:05 +00:00
|
|
|
- name: Set up Docker
|
2024-09-10 05:21:24 +00:00
|
|
|
hosts: "{{ target_host }}"
|
2024-09-09 13:34:05 +00:00
|
|
|
become: yes
|
2024-09-10 05:21:24 +00:00
|
|
|
|
2024-09-09 13:34:05 +00:00
|
|
|
vars:
|
2024-09-10 05:21:24 +00:00
|
|
|
target_host: "localhost"
|
2024-09-09 13:34:05 +00:00
|
|
|
docker_gpg_key_url: "https://download.docker.com/linux/ubuntu/gpg"
|
|
|
|
docker_gpg_key_path: "/etc/apt/keyrings/docker.asc"
|
|
|
|
|
|
|
|
tasks:
|
|
|
|
- name: Check if Docker is installed
|
|
|
|
command: which docker
|
|
|
|
register: is_docker_present
|
|
|
|
ignore_errors: yes
|
|
|
|
|
|
|
|
- block:
|
|
|
|
- name: Exit if docker is present
|
|
|
|
debug:
|
|
|
|
msg: "Docker already on host, ending play"
|
|
|
|
- meta: end_play
|
|
|
|
when: is_docker_present.rc == 0
|
|
|
|
|
|
|
|
- name: Update apt cache
|
|
|
|
apt:
|
|
|
|
update_cache: yes
|
|
|
|
|
|
|
|
- name: Install prerequisites
|
|
|
|
apt:
|
|
|
|
name:
|
|
|
|
- ca-certificates
|
|
|
|
- curl
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure keyrings directory exists
|
|
|
|
file:
|
|
|
|
path: "/etc/apt/keyrings"
|
|
|
|
state: directory
|
|
|
|
mode: '0755'
|
|
|
|
|
|
|
|
- name: Download Docker GPG key
|
|
|
|
get_url:
|
|
|
|
url: "{{ docker_gpg_key_url }}"
|
|
|
|
dest: "{{ docker_gpg_key_path }}"
|
|
|
|
mode: '0644'
|
|
|
|
|
|
|
|
- name: Get system architecture
|
|
|
|
shell: dpkg --print-architecture
|
|
|
|
register: system_arch
|
|
|
|
|
|
|
|
- name: Add Docker repository
|
|
|
|
apt_repository:
|
|
|
|
repo: "deb [arch={{ system_arch.stdout }} signed-by={{ docker_gpg_key_path }}] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable"
|
|
|
|
state: present
|
|
|
|
filename: docker
|
|
|
|
|
|
|
|
- name: Update apt cache
|
|
|
|
apt:
|
|
|
|
update_cache: yes
|
|
|
|
|
|
|
|
- name: Install Docker packages
|
|
|
|
apt:
|
|
|
|
name:
|
|
|
|
- docker-ce
|
|
|
|
- docker-ce-cli
|
|
|
|
- containerd.io
|
|
|
|
- docker-buildx-plugin
|
|
|
|
- docker-compose-plugin
|
|
|
|
state: latest
|
|
|
|
notify: Restart Docker
|
|
|
|
|
2024-09-10 08:15:29 +00:00
|
|
|
- name: Add user to docker group
|
|
|
|
user:
|
|
|
|
name: "{{ ansible_user }}"
|
|
|
|
groups: docker
|
|
|
|
append: true
|
|
|
|
|
2024-09-09 13:34:05 +00:00
|
|
|
- name: Verify Docker installation by running the hello world image
|
|
|
|
command: docker run hello-world
|
|
|
|
register: hello_world_output
|
|
|
|
ignore_errors: true
|
2024-09-10 08:15:29 +00:00
|
|
|
become: no
|
2024-09-09 13:34:05 +00:00
|
|
|
|
|
|
|
- name: Display hello-world output
|
|
|
|
debug:
|
|
|
|
var: hello_world_output.stdout_lines
|
|
|
|
|
|
|
|
handlers:
|
|
|
|
- name: Restart Docker
|
|
|
|
service:
|
|
|
|
name: docker
|
|
|
|
state: restarted
|