Skip to content

Instantly share code, notes, and snippets.

@fetwar
Last active August 7, 2023 08:16
Show Gist options
  • Save fetwar/2920988532b03ec7bf9893351166d792 to your computer and use it in GitHub Desktop.
Save fetwar/2920988532b03ec7bf9893351166d792 to your computer and use it in GitHub Desktop.
Install Docker on Ubuntu - Ansible Playbook
---
- name: Install Docker on Ubuntu
hosts: workstations
become: true
tasks:
- name: Update the apt package index
apt:
update_cache: true
- name: Install required packages for HTTPS
apt:
name:
- ca-certificates
- gnupg
state: present
- name: Add Docker GPG key
block:
- file:
path: /etc/apt/keyrings
state: directory
mode: '755'
- ansible.builtin.get_url:
url: "https://download.docker.com/linux/ubuntu/gpg"
dest: /tmp/docker.asc
mode: '600'
force: true
- shell: gpg --yes -o /etc/apt/keyrings/docker.gpg --dearmor /tmp/docker.asc
args:
creates: /etc/apt/keyrings/docker.gpg
- file:
path: /tmp/docker.asc
state: absent
- name: Get architecture
command: dpkg --print-architecture
register: dpkg_output
changed_when: false
- name: Get Ubuntu version codename
shell: . /etc/os-release && echo "$VERSION_CODENAME"
register: ubuntu_codename
changed_when: false
- name: Set up Docker repository
copy:
dest: /etc/apt/sources.list.d/docker.list
content: "deb [arch={{ dpkg_output.stdout }} signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu {{ ubuntu_codename.stdout }} stable"
owner: root
group: root
mode: '644'
- name: Install Docker Engine, containerd, and Docker Compose
apt:
update_cache: true
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
- name: Verify Docker Engine installation
command: docker run hello-world
register: result
failed_when: "'Hello from Docker' not in result.stdout"
- name: Verify Docker Compose installation
command: docker compose version
register: result
failed_when: "'Docker Compose version' not in result.stdout"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment