How To Install Docker Using Ansible

Garis Space
3 min readDec 3, 2023

--

Ansible + Docker

In this post, we will explore how to install Ansible on a macOS and use it to install Docker. Ansible is a powerful automation tool that allows for managing server and application configuration in an organized and repeatable manner.

Step 1: Installing Ansible on macOS
The simplest way to install Ansible on macOS is using Homebrew, the package manager for macOS. Open your terminal and type:

brew install ansible

# or use docker image to run playbook, example:
docker run --rm -it -v $PWD:/app -v ~/.ssh/id_rsa:/root/.ssh/id_rsa alpinelinux/ansible ansible-playbook -i app/inventory.ini app/playbooks/docker.yml

Step 2: Configuring Ansible
Prepare an inventory.ini file, which will contain the IP addresses of the machines where you want to execute Ansible tasks. Here’s a sample content for the file:

[all]
# In this case, node1 is the hostname with 192.168.18.131 IP address and garis is sudo user.
node1 ansible_host=192.168.18.131 ansible_user=garis ansible_ssh_common_args='-o StrictHostKeyChecking=no'

Step 3: Creating the Ansible Playbook for Docker Installation
Next, create a playbook named docker.yml under a directory named playbooks. This playbook will contain all the necessary tasks to install Docker on a remote Ubuntu server. The playbook looks like this:

---
- name: Install Docker on Ubuntu
hosts: all
remote_user: garis # Change remote user to your sudo user!
become: true
vars:
arch_mapping: # Map ansible architecture {{ ansible_architecture }} names to Docker's architecture names
x86_64: amd64
aarch64: arm64

tasks:
- name: Update and upgrade all packages to the latest version
ansible.builtin.apt:
update_cache: true
upgrade: dist
cache_valid_time: 3600

- name: Install required packages
ansible.builtin.apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- software-properties-common

- name: Create directory for Docker's GPG key
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: '0755'

- name: Add Docker's official GPG key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
keyring: /etc/apt/keyrings/docker.gpg
state: present

- name: Print architecture variables
ansible.builtin.debug:
msg: "Architecture: {{ ansible_architecture }}, Codename: {{ ansible_lsb.codename }}"

- name: Add Docker repository
ansible.builtin.apt_repository:
repo: >-
deb [arch={{ arch_mapping[ansible_architecture] | default(ansible_architecture) }}
signed-by=/etc/apt/keyrings/docker.gpg]
https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable
filename: docker
state: present

- name: Install Docker and related packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
update_cache: true
loop:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin

- name: Add Docker group
ansible.builtin.group:
name: docker
state: present

- name: Add user to Docker group
ansible.builtin.user:
name: "{{ ansible_user }}"
groups: docker
append: true

- name: Enable and start Docker services
ansible.builtin.systemd:
name: "{{ item }}"
enabled: true
state: started
loop:
- docker.service
- containerd.service

This playbook includes various tasks such as updating packages, installing required dependencies, adding Docker’s GPG key, setting up the Docker repository, installing Docker and related packages, adding a Docker group, and ensuring the Docker service is enabled and running.

Step 4: Running the Playbook
Finally, you can run the playbook using the following command:

ansible-playbook -i inventory.ini playbooks/docker.yml

# or use docker
docker run --rm -it -v $PWD:/app -v ~/.ssh/id_rsa:/root/.ssh/id_rsa alpinelinux/ansible ansible-playbook -i app/inventory.ini app/playbooks/docker.yml

This command will execute the playbook on the hosts specified in your inventory.ini file, effectively installing Docker on them.

Conclusion
By following these steps, you’ve successfully installed Ansible on your macOS laptop and used it to automate the installation of Docker on a remote server. This showcases the power and simplicity of Ansible for automating tasks across different systems.

--

--