---
- name: Deploy Docker
  hosts: main_server
  become: yes
  tasks:
    - name: Install dependencies
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg
          - lsb-release
        state: present
        update_cache: yes

    - name: Add Docker GPG key if not exists
      command: bash -c "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker.gpg"
      args:
        creates: /usr/share/keyrings/docker.gpg

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

    - name: Install Docker packages
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
        state: present

    - name: Check docker is active
      service:
        name: docker
        state: started
        enabled: yes

    - name: Ensure group "docker" exists
      ansible.builtin.group:
        name: docker
        state: present

    - name: Install docker-compose
      apt:
        name: docker-compose-plugin
        state: present

