Select Page

Ansible playbook for installation of Docker CE on Ubuntu or Debian:

- name: install docker ce on Ubuntu or Debian
  hosts: newinstance
  become: yes
  become_method: sudo
  gather_facts: yes

  tasks:
  - debug: msg="play_hosts={{play_hosts}}"
  - debug: msg="ansible_distribution={{ansible_distribution}}"

  - name: remove possible old versions
    apt:
      name: "{{ item }}"
      state: absent
    with_items:
      - docker 
      - docker-engine 
      - docker.io
    become: true
  
  - name: install dependencies
    apt:
      name: "{{ item }}"
      state: present
    with_items:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg2
      - software-properties-common
      - python-pip
    become: true

  - name: install python modules (for older versions of OS)
    pip:
      name: "{{item}}"
      state: present
    with_items:
    - urllib3
    - pyOpenSSL
    - ndg-httpsclient
    - pyasn1

  - name: Debian add docker key
    apt_key:
      url: https://download.docker.com/linux/debian/gpg
      state: present
    become: true
    when: "ansible_distribution == 'Debian'"
    
  - name: Ubuntu add docker key
    apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present
    become: true
    when: "ansible_distribution == 'Ubuntu'"
    
  - name: create repo line
    command: bash -c "echo \"deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable\" "
    register: docker_repo_line

  - debug: 
      msg: "{{ docker_repo_line.stdout }}"

  - name: add docker repo
    apt_repository:
      repo: "{{ docker_repo_line.stdout }}"
      state: present
    become: true
     
  - name: install docker ce
    apt:
      name: docker-ce
      state: present
      update_cache: yes
    become: true