Ansible playbook to install Docker-CE & docker-compose on Ubuntu

---
- name: Install Docker and Docker Compose on Ubuntu
  hosts: all
  become: true
  vars:
    docker_compose_version: "v2.32.4"
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install prerequisites
      apt:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common

    - name: Add Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present
        keyring: /usr/share/keyrings/docker-keyring.gpg


    - name: Set DPKG architecture
      set_fact:
        dpkg_arch: "{{ 'amd64' if ansible_architecture == 'x86_64' else ansible_architecture }}"


    - name: Add Docker repository
      apt_repository:
        repo: "deb [arch={{ dpkg_arch }} signed-by=/usr/share/keyrings/docker-keyring.gpg] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present
        update_cache: yes
  
    - name: Install Docker CE
      apt:
        update_cache: yes
        name: docker-ce
        state: present

    - name: Download Docker Compose
      get_url:
        url: "https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-{{ ansible_system }}-{{ ansible_architecture }}"
        dest: /usr/local/bin/docker-compose
        mode: '0755'

    - name: Enable and start Docker service
      systemd:
        name: docker
        state: started
        enabled: yes