Overview

This article will showcase a process how to install Docker using Ansible. We’ll be covering two different approaches how to install Docker with Ansible, one for production environment and other for development/testing purposes.

This process can be applied on any type of Linux machine - bare metal, VM or a cloud VM instance. For this example we’ll be running the playbook on Ubuntu 24.04 LTS.

Side note - we also covered a process how to deploy Docker containers using Ansible - The process can be looked at this post, and how to deploy Nextcloud and Wordpress on Docker using Ansible.

Prerequisites

In order to successfully install Docker using Ansible, the following prerequisites are required:

  • Enabled SSH access on the host group(linux server machines/vms)
  • A user with sudo privileges
  • Installed curl tool on Linux servers

What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Install the Docker for production usage

Create the host file with inventory and variables

First step is to define the inventory file with out target machines on which we plan to run the playbook and to set login variables for Ansible. You can do this with any text editor of your choosing(nano, vs code, vim/neovim, etc).

In the text file, add the host parameters to look something like this:

DockerVm:
  hosts:
    192.168.124.235
  vars:
    ansible_become: yes
    ansible_become_method: sudo
    ansible_user: markon
    ansible_password: markon1234
    ansible_become_pass: markon1234
    ansible_connection: ssh
    ansible_ssh_common_args: '-o StrictHostKeyChecking=no'

install docker using ansible

NOTE - the credentials defined here are just for this example and I do not recommend defining the same for your use case, especially if it’s for production and the password part is recommended to utilize the Ansible Vault tool to encrypt the passwords. Save the file and exit.

Hosts file breakdown:

DockerVM - hosts group name 192.168.124.235 - Remote host(vm/server) IP address under the host group named DockerVM

vars: ansible_user - username of the user that has sudo privileges(also you can set the root user)

ansible_password - password used for the ssh connection

ansible_become_pass - sudo user password (for sudo privileges when running commands that require sudo access to be ran)

ansible_connection - we’re telling the ansible here to run the playbook over the ssh to the remote host

ansible_ssh_common_args: ‘-o StrictHostKeyChecking=no’ - here i added the option to skip key host checking since i didn’t setup the playbook to use the ssh key and without this option, the playbook will fail and return the error. You can define the location of the private ssh key.

Create the install playbook

Next step for us to create the .yaml playbook in which we need to add the tasks that are going to be executed in order to automate the process of installing the Docker with Ansible. You can create playbook file anywhere you wish:

Once the text editor opens, copy and paste in the configuration from the bellow(mind the indentation) and save the file.

---
- name: Installing Docker Engine
  hosts: DockerVm

  tasks:
    - name: Install required system packages
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
          - virtualenv
        state: latest
        update_cache: true

    - name: Add Docker GPG apt Key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker Repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu focal stable
        state: present

    - name: Update apt and install Docker packages
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-compose
        state: latest
        update_cache: true

    - name: Add user to the Docker group
      user:
        name: markon
        groups: docker
        append: yes

install docker using ansible

Playbook breakdown:

In the playbook we have several steps and we’re going to comb them one by one.

hosts: DockerVm - name of the hosts group we defined in the inventory file.

- name: Install required system packages - this step will call out to the apt package manager and install the necessary packages we defined in the playbook(Ansible has in-built package manager modules for many major Linux distro’s; Debian, Ubuntu, Red Hat, Fedora etc).

- name: Add Docker GPG apt Key - step that uses apt_key module to add the GPG key in the Ubuntu’s repository.

- name: Add Docker Repository - for this step we also utilize another module, apt_repository to update the Ubuntu’s repository in order to add another source.

- name: Update apt and install Docker packages - again we apt module but now to install Docker packages.

- name: Add user to the Docker group - this step is to add the user to the docker group in order to run the docker commands without root.

Run the playbook

Now to in order to execute the playbook, navigate to the location of the inventory and playbook file and run the next command:

ansible-playbook -i inventory.yaml install_docker_prod.yaml

You need to get the Ansible output like this:

install docker with ansible

When Ansible finishes running the playbook and you get a successful report like from the above picture, we can double check if the Docker is installed successfully. Docker should be running right away and we can check it with the commands:

systemctl status docker
docker ps -a

install docker with ansible

The reverse playbook or uninstall playbook

Since Ansible isn’t capable of memorizing the made changes, unfortunately, or in another words, ansible is stateless, meaning it doesn’t keep the states in a file or database, we need to have a playbook that will undo or reverse all the changes made by the install playbook. As I like to call them - the “anti-playbook”.

It is of course optional but it’s a good practice to have them when creating more complex playbooks and that can become a hurdle to deal manually.

For our production use case playbook, the reverse playbook should look like this:

---
- name: Uninstalling Docker Engine
 hosts: DockerVm

 tasks:
   - name: Remove user from the Docker group
     user:
       name: markon
       groups: docker
       append: yes
       state: absent

   - name: Remove Docker packages
     apt:
       name:
         - docker-ce
         - docker-ce-cli
         - containerd.io
         - docker-compose
       state: absent

   - name: Remove Docker Repository
     apt_repository:
       repo: deb https://download.docker.com/linux/ubuntu focal stable
       state: absent

   - name: Remove Docker GPG apt Key
     apt_key:
       url: https://download.docker.com/linux/ubuntu/gpg
       state: absent

   # To remove the system packages installed originally is optional step
   - name: Remove system packages
     apt:
       name:
         - apt-transport-https
         - ca-certificates
         - curl
         - software-properties-common
         - virtualenv
       state: absent

Install the Docker for dev/testing purposes using the official Docker install script

On the Docker site itself, they are hosting the install script which pretty much does all the same as our playbook buy they have stated themselves that the script is not intended for production purposes, just testing and development.

It’s esenntialy a bash script. Yes, we can also automate the process which will significally shorthen our playbook but again you can just download the script and install with a one liner command if you wish.

But we’ll proceed to create playbook just for fun of it. You can use the same inventory file.

---
- hosts: DockerVm
  tasks:
  - name: Install Docker
    ansible.builtin.shell: cd ~ && curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh

Playbook breakdown:

hosts: DockerVm - Here we specified that this book will run and be applied only on our docker host group.

tasks: - Specified the task to run

name: name of the task that we’re running

ansible.builtin.sheel: - Ansible shell module that allows us to run bash shell commands on our remote Linux servers

The shell command we ran in our Ansible playbook:

“cd ~ && curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh”

The mentioned shell command will do following: It will navigate first to the user’s home directory, download the Docker install script on home directory and run the same install script.

Run the playbook

When we have our resources, machines and playbook file ready, we can run the playbook to initialize the Docker install process. Run the playbook with the same command as before:

ansible-playbook -i inventory.yaml docker-install-testing.yaml

And you should get the sucessful output again.

Summary

To summarize the article - we performed the steps how to install Docker using Ansible. We created the inventory file and variations of the playbook in which we added our remote node with login variables for the SSH connection, wrote and ran a .yaml playbook which downloads the necessary packages to run Docker and in the end installed Docker with ansible.

Since the Docker installation is possible with Ansible, it’s also possible to deploy Docker containers as well. If interested, you can check out the process at this post.

Thank you for your time…