- name: determine distribution
  setup:
    gather_subset:
      - distribution


- block:
    - name: ensure apt can access https repos
      apt:
        name:
          - ca-certificates
          - curl
          - gnupg
          - lsb-release
        force_apt_get: yes
        state: latest


    - name: add keyrings dir
      file:
        state: directory
        path: "{{ gpg_keyrings_dir }}"


    - name: download docker gpg key
      get_url:
        url: "{{ docker_repo_url ~ '/gpg' }}"
        dest: "{{ gpg_keyrings_dir }}/docker.asc"
        mode: a+r


    - name: add apt repo
      apt_repository:
        repo: "deb [arch=amd64 signed-by={{ (gpg_keyrings_dir ~ '/docker.asc') | quote }}] \
               {{ docker_repo_url }} {{ ansible_distribution_release }} stable"


    - name: update repository index
      apt:
        force_apt_get: yes
        update_cache: yes
      changed_when: no

  when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'