- name: Set up Docker hosts: "{{ target_host }}" become: yes vars: target_host: "localhost" 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 - name: Verify Docker installation by running the hello world image command: docker run hello-world register: hello_world_output ignore_errors: true - name: Display hello-world output debug: var: hello_world_output.stdout_lines - name: Add user to docker group user: name: "{{ ansible_user }}" groups: docker append: true handlers: - name: Restart Docker service: name: docker state: restarted