1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# https://docs.docker.com/engine/install/debian/
- name: Install Docker
  tags: docker
  hosts: "{{ ansible_limit | default([]) }}"
  become: true
  tasks:

    # sudo apt-get update
    # sudo apt-get install ca-certificates curl
    - name: Install required packages
      ansible.builtin.apt:
        name:
          - ca-certificates
          - curl
          - gnupg # for the signing keys
          - python3-debian # for ansible.builtin.deb822_repository
        state: present
        update_cache: true

    # sudo install -m 0755 -d /etc/apt/keyrings
    # sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
    # sudo chmod a+r /etc/apt/keyrings/docker.asc
    - name: Download Docker GPG key
      ansible.builtin.get_url:
        url: https://download.docker.com/linux/debian/gpg
        dest: /etc/apt/keyrings/docker.asc
        mode: "0644"

    - name: Get system architecture
      ansible.builtin.command: dpkg --print-architecture
      register: dpkg_arch
      changed_when: false

    # Left behind by earlier runs: apt reads both formats, and a one-liner
    # with a different signed-by for the same repository breaks apt outright.
    - name: Remove one-line Docker repositories
      ansible.builtin.file:
        path: /etc/apt/sources.list.d/{{ item }}
        state: absent
      loop:
        - download_docker_com_linux_debian.list
        - docker.list

    # echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
    #   https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
    #   | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    - name: Add Docker repository
      ansible.builtin.deb822_repository:
        name: docker
        types: deb
        uris: https://download.docker.com/linux/debian
        suites: '{{ ansible_facts["distribution_release"] }}'
        components: stable
        architectures: "{{ dpkg_arch.stdout }}"
        signed_by: /etc/apt/keyrings/docker.asc
        state: present

    # sudo apt-get update
    # sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    - name: Install Docker
      ansible.builtin.apt:
        name: "{{ item }}"
        state: present
        update_cache: true
      loop:
        - docker-ce
        - docker-ce-cli
        - containerd.io
        - docker-buildx-plugin
        - docker-compose-plugin

# vim: ft=yaml.ansible