Skip to content

Instantly share code, notes, and snippets.

@rbq
Last active October 19, 2023 11:57
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save rbq/886587980894e98b23d0eee2a1d84933 to your computer and use it in GitHub Desktop.
Save rbq/886587980894e98b23d0eee2a1d84933 to your computer and use it in GitHub Desktop.
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
@rulai-jianfang
Copy link

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
Copy link

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
Copy link

Update ansible?

@rulai-jianfang
Copy link

'ansible_distribution' is undefined, better post your yaml here

@rbq
Copy link
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).

@fhuitelec
Copy link

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
Copy link

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?

@arlekinjose
Copy link

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
Copy link

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

@mstevanic
Copy link

- 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

@JonasGroeger
Copy link

@mstevanic If you have gather_facts: True :)

@aioue
Copy link

aioue commented Jul 8, 2022

Docker on Ubuntu 22.04 AWS AMI from Canonical, sticking as close to the official install page as possible, and leaning on work from @yum-dev above:

- name: install prerequisites for Docker repository
  become: yes
  ansible.builtin.apt:
    pkg:
      - ca-certificates
      - curl
      - gnupg2
      - lsb-release

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

- name: add docker apt repo
  become: yes
  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 its dependencies
  become: yes
  apt:
    pkg:
      - docker-ce
      - docker-ce-cli
      - containerd.io
      - docker-buildx-plugin
      - docker-compose-plugin
    state: present

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

- name: start and enable containerd daemon
  become: yes
  service:
    name: containerd
    state: started
    enabled: yes

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