Here is a quick playbook to install and manage Docker with Ansible on Ubuntu hosts.
Don’t forget to add the host to the docker-hosts file.
Here is our ready to use docker playbook:
---
- name: Install Docker and Docker Compose
hosts: docker-hosts
become: yes
tasks:
- name: Install aptitude using apt
apt:
name: aptitude
state: latest
update_cache: yes
- name: Install pip3
apt:
name: python3-pip
update_cache: yes
state: present
- name: Install Docker SDK for Python
ansible.builtin.pip:
name: docker
state: present
- name: Install Docker SDK for Python
ansible.builtin.pip:
name: docker-compose
state: present
- name: Install required system packages
apt:
name: "{{ packages }}"
state: latest
update_cache: yes
vars:
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- software-properties-common
- name: Add Docker’s official GPG key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
ansible.builtin.apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb['codename'] }} stable
state: present
- name: Install Docker Engine
apt:
name: docker-ce
state: latest
update_cache: yes
- name: Ensure Docker service is running
ansible.builtin.service:
name: docker
state: started
enabled: yes
Here’s a step-by-step breakdown of what each part of the playbook does:
- Set the target hosts and become privileged: It specifies that the following tasks should be run on the hosts grouped under
docker-hosts
and that you should escalate to privileged (root) access using thebecome: yes
directive. - Install aptitude: The first task uses the
apt
module to installaptitude
, a package manager for Debian and Ubuntu, ensuring it’s the latest version and updates the package cache. - Install pip3: The next task installs
python3-pip
, the Python package installer, ensuring it’s present on the system and again updates the package cache. - Install Docker SDK for Python: This involves two tasks that use the
ansible.builtin.pip
module to install the Docker SDK (docker
) and Docker Compose (docker-compose
) for Python. - Install required system packages: Several key system packages are installed, such as
curl
,gnupg
, andlsb-release
. This task also demonstrates the use of variables within Ansible by using{{ packages }}
to represent a list of package names defined undervars
. - Add Docker’s official GPG key: This uses the
ansible.builtin.apt_key
module to add the official Docker GPG key to the list of keys used by the APT package manager to authenticate packages. - Add Docker repository: The
ansible.builtin.apt_repository
module adds the official Docker repository to the list of APT sources. - Install Docker Engine: The
apt
module is used again to installdocker-ce
(Docker Community Edition), ensuring that the latest version is installed and the package cache is updated. - Ensure Docker service is running: Finally, the
ansible.builtin.service
module ensures that the Docker service is started and enabled to start on boot.
Suggestions and tips? Leave a comment!