- name: China mirror
  set_fact:
    china_mirror_docker: true
  when: china_mirror
- name: China mirror for Docker
  set_fact:
    mirror_docker: https://mirrors.tuna.tsinghua.edu.cn/docker-ce
  when: china_mirror_docker
- name: clean apt Docker
  become: true
  apt:
    name: docker,docker-engine,docker.io,containerd,runc,bridge-utils
    state: absent
    autoremove: yes
    purge: yes
  when: ansible_os_family == 'Debian' and ansible_distribution_release != 'focal'
- name: clean apt Docker for focal
  become: true
  apt:
    name: docker-ce,docker-ee
    state: absent
    autoremove: yes
    purge: yes
  when: ansible_os_family == 'Debian' and ansible_distribution_release == 'focal'
- name: Docker apt 源公钥
  become: true
  apt_key:
    url: '{{mirror_docker}}/linux/{{ ansible_distribution|lower }}/gpg'
  when: ansible_os_family == 'Debian' and ansible_distribution_release != 'focal'
- name: target distribution for non-sid
  set_fact:
    docker_distribution: '{{ ansible_distribution_release|lower }}'
  when: ansible_distribution_release != 'sid'
- name: target distribution for sid
  set_fact:
    docker_distribution: buster
  when: ansible_distribution_release == 'sid'
- name: Docker apt 源
  become: true
  apt_repository:
    repo: 'deb {{mirror_docker}}/linux/{{ ansible_distribution|lower }} {{ docker_distribution }} stable'
    filename: docker
  when: ansible_os_family == 'Debian' and ansible_distribution_release != 'focal'
- name: Docker apt 源 清理
  become: true
  apt_repository:
    repo: 'deb {{mirror_docker}}/linux/{{ ansible_distribution|lower }} {{ docker_distribution }} stable'
    filename: docker
    state: absent
  when: ansible_os_family == 'Debian' and ansible_distribution_release == 'focal'
- name: Docker apt 源 清理
  become: true
  apt_repository:
    repo: 'deb [arch=amd64] {{mirror_docker}}/linux/{{ ansible_distribution|lower }} {{ docker_distribution }} stable'
    filename: docker
    state: absent
  when: ansible_os_family == 'Debian' and ansible_distribution_release == 'focal'
- name: 安装 Docker (apt)
  become: true
  apt:
    name: docker-ce
    update_cache: yes
  when: ansible_os_family == 'Debian' and ansible_distribution_release != 'focal'
  notify: restart_docker
- name: Docker extra packages
  become: true
  apt:
    name: python-backports-shutil-get-terminal-size,python-backports.ssl-match-hostname
  when: ansible_os_family == 'Debian' and ansible_distribution_release != 'focal' and ansible_distribution_release != 'sid'
- name: 安装 Docker (focal)
  become: true
  apt:
    name: docker.io
    update_cache: yes
  when: ansible_os_family == 'Debian' and ansible_distribution_release == 'focal'
  notify: restart_docker
- name: clean yum Docker
  become: true
  yum:
    name: docker,docker-client,docker-client-latest,docker-common,docker-latest,docker-latest-logrotate,docker-logrotate,docker-engine
    state: absent
    autoremove: yes
  when: ansible_os_family == "RedHat"
- name: docker yum 源
  become: true
  yum_repository:
    name: docker-ce
    description: docker-ce
    baseurl: '{{mirror_docker}}/linux/{{ ansible_distribution|lower }}/{{ansible_distribution_major_version}}/$basearch/stable'
    ip_resolve: 'IPv4'
    gpgkey: '{{mirror_docker}}/linux/{{ ansible_distribution|lower }}/gpg'
  when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int <= 8
- name: 安装 Docker (yum)
  become: true
  yum:
    name: docker-ce,docker-ce-cli,containerd.io
    update_cache: yes
  when: ansible_os_family == 'RedHat' and ansible_distribution_major_version|int <= 8
  notify: restart_docker
- name: 安装 Docker (Fedora)
  become: true
  dnf:
    name: moby-engine
    update_cache: yes
  when: ansible_distribution == 'Fedora'
  notify: restart_docker
- name: docker user group
  become: true
  user:
    name: '{{ansible_user_id}}'
    append: true
    groups:
      - docker
  when: ansible_user_id != 'root'
- name: docker-compose requests workaround
  become: true
  pip:
    name: requests
    extra_args: --ignore-installed
  when: ansible_python.version.major == 2
- name: docker-compose
  become: true
  pip:
    name: docker-compose
- name: docker 配置 创建目录
  become: true
  file:
    name: /etc/docker
    state: directory
- name: docker 配置
  become: true
  template:
    src: 'daemon.json.j2'
    dest: /etc/docker/daemon.json
  notify: reload_docker
- name: docker 启动
  become: true
  systemd:
    name: docker
    enabled: yes
    state: started
  when: ansible_virtualization_type != 'docker'
