Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Docker CE on Ubuntu using Ansible
---
- hosts: all
tasks:
- name: Install prerequisites
apt:
name: ['python3-pip', 'python3-setuptools', 'virtualenv']
update_cache: yes
- name: Install docker-compose
pip:
name: docker-compose
---
- hosts: all
tasks:
- name: Install prerequisites
apt:
name: ['apt-transport-https', 'ca-certificates', 'curl', 'gnupg2' ,'software-properties-common']
update_cache: yes
- name: Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
- name: Install Docker
apt:
name: docker-ce
update_cache: yes
@webhive

This comment has been minimized.

Copy link

webhive commented Apr 27, 2018

Does not work with ubuntu bionic yet. No docker package yet ready.

@ThinkZ

This comment has been minimized.

Copy link

ThinkZ commented May 11, 2018

change stable channel to edge channel if you use ubuntu bionic
15: repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} edge

@erolg

This comment has been minimized.

Copy link

erolg commented Jun 25, 2018

@webhive update cache attribute need at the last task.

Better version:

- hosts: all
  tasks:
  - name: Add Docker GPG key
    apt_key: url=https://download.docker.com/linux/ubuntu/gpg

  - name: Add Docker APT repository
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable

  - name: Install list of packages
    apt:
      name: "{{ item }}"
      state: installed
      update_cache: yes
    with_items:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - docker-ce
@kymtwyf

This comment has been minimized.

Copy link

kymtwyf commented Sep 26, 2018

@erolg Since [DEPRECATION WARNING]: State 'installed' is deprecated. Using state 'present' instead.. This feature will be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

So it's better to use:

- hosts: all
  tasks:
  - name: Add Docker GPG key
    apt_key: url=https://download.docker.com/linux/ubuntu/gpg

  - name: Add Docker APT repository
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable

  - name: Install list of packages
    apt:
      name: "{{ item }}"
      state: present
      update_cache: yes
    with_items:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - docker-ce
@Nouri-Alnahawi

This comment has been minimized.

Copy link

Nouri-Alnahawi commented Nov 16, 2018

Just another heads up in case anyone uses this again, cus I did :D

[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: {{ item }}`, please use `name: [u'apt-transport-https', u'ca-certificates', u'curl', u'software-properties-common', u'docker-ce']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

Better:

- hosts: all
  tasks:
  - name: Add Docker GPG key
    apt_key: url=https://download.docker.com/linux/ubuntu/gpg

  - name: Add Docker APT repository
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable

  - name: Install list of packages
    apt:
      name: ['apt-transport-https','ca-certificates','curl','software-properties-common','docker-ce']
      state: present
      update_cache: yes
@Yasinmohammed007

This comment has been minimized.

Copy link

Yasinmohammed007 commented Apr 23, 2019

Trying to install on my ubuntu machine and came across the below one. Officially the command has to be
+++++++++++++++++++++
sudo add-apt-repository
"deb [arch=amd64] https://download.docker.com/linux/ubuntu
$(lsb_release -cs)
stable"
+++++++++++++++++++++
According to above script it is as below.
" repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable "
But why this "{{ansible_distribution_release}}" , What is it exactly doing here? compared to the official one and yours.

@hhund

This comment has been minimized.

Copy link

hhund commented May 5, 2019

@Yasinmohammed007 I just asked myself the same question:
{{ansible_distribution_release}} inserts the value of the variable ansible_distribution_release which should be equivalent to the output of $(lsb_release -cs)

To see what variables are available use

- debug: var=ansible_facts

in a playbook.

References:
https://superuser.com/questions/1010836/ansible-determine-operating-system
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variables-discovered-from-systems-facts

@sabueso

This comment has been minimized.

Copy link

sabueso commented May 28, 2019

Some notes about this gist:

  1. apt-transport-https should be installed before add the Docker's repository: it's like a race condition, you cannot do the update & install if you have an https in sources files.
  2. The ansible_distribution_release should be enough if you run this in a Ubuntu installation, but adapt it to Debian is cost-less
  3. Install docker-compose maybe will be necessary in the future, so...
#!/usr/bin/env ansible-playbook
- hosts: all
  tasks:
  - name: Add Docker GPG key
    apt_key: url=https://download.docker.com/linux/ubuntu/gpg

  - name: Install basic list of packages
    apt:
      name: ['apt-transport-https','ca-certificates','curl','gnupg2','software-properties-common']
      state: present
      update_cache: yes

  - name: Add Docker APT repository
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable

  - name: Install Docker packages
    apt:
      name: ['docker-ce','docker-ce-cli','containerd.io']
      state: present

  - name: Install Docker-compose 
    shell: curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
@davesave

This comment has been minimized.

Copy link

davesave commented Jun 20, 2019

- name: Install Docker-compose
  become: yes
  #  shell: curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  get_url:
    url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ansible_system}}-{{ansible_architecture}}"
    dest: /usr/local/bin/docker-compose
    mode: +x
@vineethvijay-MyT

This comment has been minimized.

Copy link

vineethvijay-MyT commented Sep 5, 2019

With docker-compose as service.

---
- name: Add Docker GPG key
  apt_key: url=https://download.docker.com/linux/ubuntu/gpg
  become: true

- name: Install basic list of packages
  apt:
    name: ['apt-transport-https','ca-certificates','curl','gnupg2','software-properties-common']
    state: present
    update_cache: yes
  become: true

- name: Add Docker APT repository
  apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
  become: true

- name: Install Docker packages
  apt:
    name: ['docker-ce','docker-ce-cli','containerd.io']
    state: present
  become: true

- name: Install Docker-compose
  shell: curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  become: true
@rulai-jianfang

This comment has been minimized.

Copy link

rulai-jianfang commented Sep 5, 2019

The following script is working for me in uBuntu 18.04


- name: Configure instance(s)
  hosts: myinstance
  remote_user: "{{ deploy_user_name }}" 
  become: true
  tasks:

      - name: Install aptitude using apt
        apt: name=aptitude state=latest update_cache=yes force_apt_get=yes

      - name: Add Docker GPG key
        apt_key:
          url: https://download.docker.com/linux/ubuntu/gpg
          state: present

      - name: Install required system packages
        apt: name={{ item }} state=latest update_cache=yes
        loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']

      - name: Add Docker APT repository
        apt_repository:
          repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
          state: present

      - name: Update apt and install docker-ce
        apt: update_cache=yes name=docker-ce state=latest

      - name: Update apt and install docker-ce-cli
        apt: update_cache=yes name=docker-ce-cli state=latest

      - name: Update apt and install containerd.io
        apt: update_cache=yes name=containerd.io state=latest


      - name: Install Docker Module for Python
        pip:
          name: docker

      - name: Install Docker-compose
        shell: curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
        ignore_errors: yes

@amundra2016

This comment has been minimized.

Copy link

amundra2016 commented Sep 10, 2019

@rulai-jianfang seems like your gist is not rightly indented.

@rulai-jianfang

This comment has been minimized.

Copy link

rulai-jianfang commented Sep 10, 2019

I guess you can just use the part under "tasks" instead of full code snippet. The whole playbook is tested working.

Also check the format with yamllint online, it passed the format/indent test

@manasa-h

This comment has been minimized.

Copy link

manasa-h commented Oct 15, 2019

fatal: [172.31.90.128]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible_distribution' is undefined\n\nThe error appears to be in '/home/devops/ansible/installdocker.yml': line 35, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Add Docker APT repository\n ^ here\n"}

i am getting above error when i run this playbook. what i need to do?

@davesave

This comment has been minimized.

Copy link

davesave commented Oct 15, 2019

Update ansible?

@rulai-jianfang

This comment has been minimized.

Copy link

rulai-jianfang commented Oct 15, 2019

'ansible_distribution' is undefined, better post your yaml here

@rbq

This comment has been minimized.

Copy link
Owner Author

rbq commented Oct 16, 2019

@manasa-h

i am getting ['ansible_distribution' is undefined] when i run this playbook. what i need to do?

Check if you accidentially disabled the gathering of host facts (e.g. gather_facts: no in your playbook).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.