---

# Add some prerequisites
- name: Installing Docker Prerequisites on {{ ansible_distribution }} {{ ansible_distribution_major_version }}
  apt:
    update_cache: yes
    state: present
    name:
      - apt-transport-https
      - ca-certificates
      - gnupg2
      #- software-properties-common # not on debian 13

# Add APT GPG key
# NOTE apt_key is deprecated in Debian 13, use get_url and store the key in /etc/apt/trusted.gpg.d/*
- name: Adding Docker APT repository key for {{ ansible_distribution }} {{ ansible_distribution_major_version }}
  ansible.builtin.get_url:
    url: "https://download.docker.com/linux/debian/gpg"
    dest: /etc/apt/trusted.gpg.d/docker.asc
    mode: '0644'
    force: true

# Add apt repository
- name: Adding Docker APT repository for {{ ansible_distribution }} {{ ansible_distribution_major_version }}
  apt_repository:
    filename: docker
    state: present
    repo: 'deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/docker.asc] https://download.docker.com/linux/debian trixie stable'

# Apt update
- name: Updating apt cache
  apt:
    update_cache: yes

# Get exact apt package docker version based on var docker_version
- name: Finding APT Docker Version String for {{ docker_version }}
  shell: "apt-cache madison docker-ce | awk '{ print $3 }' | grep '{{ docker_version }}' | sort | tail -n1"
  register: docker_version_output

# Setting docker version string
- name: Set docker_version_string variable to {{ docker_version_output.stdout }}
  set_fact:
    docker_version_string: "{{ docker_version_output.stdout }}"

# Install docker and docker-compose-plugin
# note that with the version in here it will upgrade docker to this version, it may fail if a newer version is installed
- name: Installing Docker
  apt:
    update_cache: yes
    state: present
    # Tell apt to NOT start docker after install
    policy_rc_d: 101
    name:
      - docker-ce={{ docker_version_string }}
      - docker-ce-cli={{ docker_version_string }}
      - containerd.io
      - docker-buildx-plugin
      - docker-compose-plugin

# Configure docker /etc/docker/daemon.js (notice docker has NOT started yet because of policy_rc_d)
- name: Creating /etc/docker
  file:
    path: /etc/docker
    owner: root
    group: root
    state: directory
- name: Templating /etc/daemon.json
  template:
    src: daemon.json.j2
    dest: /etc/docker/daemon.json
    owner: root
    group: root
    mode: 0644
    backup: yes
  notify: restart docker

# Start docker now that we have copied our custom /etc/docker/daemon.json
- name: Starting docker daemon
  service:
    name: docker
    enabled: yes
    state: started

# Install python-docker (required for ansible docker modules)
- name: Installing python3-docker for {{ ansible_distribution }} {{ ansible_distribution_major_version }}
  apt:
    update_cache: yes
    state: present
    name: python3-docker




# # Add docker apt GPG key and repo (creates to /etc/apt/sources.list.d/download_docker_com_linux_debian.list )
# - name: Adding Docker apt repository key
#   apt_key: url='https://download.docker.com/linux/debian/gpg' state=present
#   when: ansible_distribution == 'Debian'

# - name: Adding Docker apt repository key
#   apt_key: url='https://download.docker.com/linux/ubuntu/gpg' state=present
#   when: ansible_distribution == 'Ubuntu'

# - name: Adding Debian 9 Docker apt repository sources
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/debian stretch stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "9"

# - name: Adding Debian 10 Docker apt repository sources
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/debian buster stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "10"

# - name: Adding Debian 11 Docker apt repository sources
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/debian bullseye stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "11"

# - name: Adding Debian 12 Docker apt repository sources
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/debian bookworm stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "12"

# - name: Adding Docker apt repository sources for Ubuntu 18
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "18"

# - name: Adding Docker apt repository sources for Ubuntu 20
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "20"

# - name: Adding Docker apt repository sources for Ubuntu 22
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/ubuntu jammy stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "22"

# - name: Adding Docker apt repository sources for Ubuntu 24
#   apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/ubuntu noble stable' state=present
#   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "24"



# - name: Update apt cache
#   apt:
#     update_cache: yes

# - name: find docker version string
#   shell: "apt-cache madison docker-ce | awk '{ print $3 }' | grep '{{ docker_version }}' | sort | tail -n1"
#   register: docker_version_output

# - name: set docker_version_string variable
#   set_fact:
#     docker_version_string: "{{ docker_version_output.stdout }}"

# # Install docker and docker-compose-plugin
# # note that with the version in here it will upgrade docker to this version, it may fail if a newer version is installed
# - name: Installing Docker
#   apt:
#     update_cache: yes
#     state: present
#     # Tell apt to NOT start docker after install
#     policy_rc_d: 101
#     name:
#       - docker-ce={{ docker_version_string }}
#       - docker-ce-cli={{ docker_version_string }}
#       - containerd.io
#       - docker-buildx-plugin
#       - docker-compose-plugin

# # Configure docker /etc/docker/daemon.js (notice docker has NOT started yet because of policy_rc_d)
# # - name: Create /etc/docker
# #   file:
# #     path: /etc/docker
# #     owner: root
# #     group: root
# #     state: directory

# # - name: Copying daemon.json
# #   template:
# #     src: daemon.json.j2
# #     dest: /etc/docker/daemon.json
# #     owner: root
# #     group: root
# #     mode: 0644
# #     backup: yes
# #   notify: restart docker


# # Start docker now that we have copied our custom /etc/docker/daemon.json
# - name: Starting docker daemon
#   service:
#     name: docker
#     enabled: yes
#     state: started


# # # Install python-docker (required for ansible docker modules)
# # - name: Installing python-docker for Ubuntu 18
# #   apt:
# #     update_cache: yes
# #     state: present
# #     name: python-docker
# #   when: ansible_os_family == "Debian" and ansible_distribution_major_version == "18"

# # - name: Installing python3-docker for Ubuntu 20+
# #   apt:
# #     update_cache: yes
# #     state: present
# #     name: python3-docker
# #   when: ansible_os_family == "Debian" and ansible_distribution_major_version >= "20"
