How to Use Ansible to Install and Set Up Docker on Ubuntu 20.04?

Overview

Ansible is a free tool for managing configuration that automates the installation and administration of software programs, infrastructure, and systems. It makes it easier to manage complicated infrastructure and automate tedious processes by using straightforward yet effective language to create and describe IT workflows.

Using Ansible to install Docker provides a reliable, scalable, and reproducible way to manage Docker installations on multiple servers, which can help reduce operational overhead and ensure consistency and reliability across your infrastructure.

In this tutorial, we will cover the steps required to use Ansible to install and set up Docker on Ubuntu 20.04.

Prerequisites

There are some prerequisties that need to be met before you begin:

  • Ubuntu 20.04 server with SSH access

  • User with sudo privileges

  • An SSH key pair for authentication with the remote server

  • Stable internet connection

Get Started

Step 1: Update and upgrade the system's packages and software.

sudo apt update && sudo apt upgrade

Step 2: Install Ansible on your local machine.

sudo apt install ansible

Step 3: Create an Ansible playbook file. In this example, we will name it docker.yml.

touch docker.yml

Step 4: Open the docker.yml file in your preferred text editor and paste the following code:

---
- hosts: all
  become: true
  tasks:
    - name: Install dependencies
      apt:
        name: "{{ item }}"
        state: present
      with_items:
        - apt-transport-https
        - ca-certificates
        - curl
        - gnupg
        - lsb-release

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

    - name: Add Docker repository
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
        state: present

    - name: Install Docker
      apt:
        name: docker-ce
        state: present

    - name: Add user to Docker group
      user:
        name: "{{ ansible_user }}"
        groups: docker
        append: yes

    - name: Start Docker service
      service:
        name: docker
        state: started

The code above performs the following tasks:

  • Installs dependencies needed for Docker installation

  • Adds Docker's GPG key

  • Adds Docker repository

  • Installs Docker

  • Adds the current user to the Docker group

  • Starts the Docker service

Step 5: Save and close the docker.yml file.

Step 6: Create an Ansible inventory file. In this example, we will name it inventory.ini.

touch inventory.ini

Step 7: Open the inventory.ini file in your preferred text editor and add the IP address of the target Ubuntu 20.04 machine., username, and key pair file name.

Note: The key pair file has to be in the same location as the inventory.ini and docker.yml files.

Step 8: Save and close the inventory.ini file.

Step 9: Run the Ansible playbook.

ansible-playbook -i inventory.ini docker.yml

Step 10: Initiate the connection to the remote server.

ssh -i <Key_Pair.pem> <username>@<ip address>

ssh -i Home_Key_Pair.pem ubuntu@23.909.XXX.XXX

Step 11: After the playbook finishes executing, verify that Docker is installed and running on the target machine by running the following command:

docker --version

Conclusion

By following the steps outlined in this tutorial, you can quickly and easily install Docker on Ubuntu 20.04 using Ansible and take advantage of the many benefits that Docker provides for application deployment and man

Last updated