Skip to content

Instantly share code, notes, and snippets.

@rbq

rbq/docker.yaml

Last active May 19, 2021
Embed
What would you like to do?
Install Docker CE on Ubuntu using Ansible
---
- hosts: all
tasks:
- name: Install prerequisites for Docker repository
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/{{ ansible_system | lower }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable
- name: Install Docker CE
apt:
name: ['docker-ce', 'docker-ce-cli', 'containerd.io']
update_cache: yes
- name: Install prerequisites for docker-compose
apt:
name: ['python3-pip', 'python3-setuptools', 'virtualenv']
- name: Install docker-compose
pip:
name: docker-compose
@webhive

This comment has been minimized.

Copy link

@webhive 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 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 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 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 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 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 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 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 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 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 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 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 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 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 davesave commented Oct 15, 2019

Update ansible?

@rulai-jianfang

This comment has been minimized.

Copy link

@rulai-jianfang 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 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).

@fhuitelec

This comment has been minimized.

Copy link

@fhuitelec fhuitelec commented Apr 4, 2020

Just stumble upon this gist via a simple ansible apt_repository docker google search.

Thank you all for the snippets, especially for the ansible_distribution and ansible_distribution_release which I did not know.

I'd suggest a safety net for adding Docker’s GPG key :

- name: Add Docker official GPG key
  become: yes
  apt_key:
    state: present
    keyserver: 'https://download.docker.com/linux/{{ansible_distribution|lower}}/gpg'
    id: 7EA0A9C3F273FCD8

- name: check Docker apt key has not been altered
  become: yes
  command: apt-key fingerprint 0EBFCD88
  register: fingerprint_check
  changed_when: False
  failed_when: "'9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88' not in fingerprint_check.stdout"

Notes:

  • the apt_key.id: 7EA0A9C3F273FCD8 is retrieved from the subkey1
  • the fingerprint 9DC8 [...] CD88 in the check task comes from the official documentation

1 Retrieved via gpg through this one-liner:

See more…

curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
    | gpg --keyid-format long 2>/dev/null \
    | grep sub \
    | cut -d'/' -f2 \
    | cut -d' ' -f1

@kurarrr

This comment has been minimized.

Copy link

@kurarrr kurarrr commented May 23, 2020

https://github.com/kurarrr/docker-ansible/blob/master/playbook.yml
This worked for me with ansible 2.9.9.
But I got the

 {"msg": "The field 'remote_user' has an invalid value, which includes an undefined variable. The error was: 'deploy_user_name' is undefined"}

if I added remote_user: "{{ deploy_user_name }}".
How can I use this variable?

@ispanos

This comment has been minimized.

Copy link

@ispanos ispanos commented Jun 8, 2020

Why everyone is replacing gnupg-agent with gnupg2? The docs suggest gnupg-agent.

@arlekinjose

This comment has been minimized.

Copy link

@arlekinjose arlekinjose commented Jun 21, 2020

I got error "Unable to find any of pip2, pip to use. pip needs to be installed." on ubuntu bionic using command ansible-playbook --connection=local --inventory 127.0.0.1, --limit 127.0.0.1 docker.yaml. It only has python 3. It worked after I setup explicitly the pip version with the parameter "executable".

@yum-dev

This comment has been minimized.

Copy link

@yum-dev yum-dev commented Nov 17, 2020

This works for me

$ ansible --version
ansible 2.10.2
---
- name: apt update
  apt:
    update_cache: yes

- name: Install prerequisites for Docker repository
  apt:
    pkg:
    - apt-transport-https
    - ca-certificates
    - curl
    - gnupg2
    - software-properties-common

- name: add docker apt key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: add docker apt repo
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} stable"
    state: present
    update_cache: yes
  
- name: install docker and it's dependencies
  apt:
    pkg:
      - docker-ce
      - docker-ce-cli
      - containerd.io
    state: present

- name: start and enable docker daemon
  service:
    name: docker
    state: started
    enabled: yes
@ispanos

This comment has been minimized.

Copy link

@ispanos ispanos commented Nov 19, 2020

@yum-dev

repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} stable"

Why {{ ansible_distribution|lower }} and not just Ubuntu? If you use:
... https://download.docker.com/linux/ubuntu ... it works for Pop!_Os too. Otherwise it fails.

@mstevanic

This comment has been minimized.

Copy link

@mstevanic mstevanic commented Jan 27, 2021

- name: fetch docker-compose checksum
  uri:
    url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64.sha256
    return_content: yes
  register: docker_compose_checksum

- name: install docker-compose
  get_url:
    url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64
    checksum: "sha256:{{ docker_compose_checksum.content.split(' ') | first }}"
    dest: /usr/local/bin/docker-compose
    mode: '0755'

you can use {{ ansible_machine }} and {{ ansible_system }} instead of above hardcoded x86_64 and Linux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment