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
@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