Installing Docker using Ansible-playbook on Ubuntu 22.04 LTS

Introduction

The application Docker makes it easier to manage application processes in containers. You can run your applications in resource-isolated processes with containers. Containers are similar to virtual machines, but they are more mobile, use fewer resources, and rely more on the host operating system.

Ansible – The purpose of configuration management systems is to make it easier for administrators and operations teams to manage a large number of servers. They let you automate control of many different systems from a single location.

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in Linux commands.
  • Internet connectivity.

We can use the given link to install Ansible on Ubuntu 22.04 LTS Linux machine.

In this post, We will show you how to install Docker using Ansible-playbook on ubuntu 22.04 LTS Linux machine.

Step 1: Create Ansible Playbook

To install Docker on Ubuntu using an Ansible playbook, you can create a YAML file with the necessary tasks. Below is a simple example of an Ansible playbook that installs Docker on an Ubuntu system.

We need to create a install_docker.yml file by using the given command.

nano install_docker.yml

Paste the following tasks.

---
- name: Install Docker on Ubuntu
  hosts: your_target_host
  become: yes  # Run tasks with sudo

  tasks:
    - name: Update apt package cache
      apt:
        update_cache: yes

    - name: Install dependencies
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common

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

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

    - name: Update apt package cache (again)
      apt:
        update_cache: yes

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

    - name: Add user to the docker group
      user:
        name: "{{ ansible_ssh_user }}"
        groups: docker
        append: yes

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

This playbook performs the following tasks:

  1. Updates the APT package cache.
  2. Installs required dependencies.
  3. Adds Docker’s GPG key.
  4. Adds Docker’s APT repository.
  5. Updates the APT package cache again.
  6. Installs Docker.
  7. Adds the specified user to the Docker group.
  8. Starts the Docker service.

Save the above content into a file, e.g., install_docker.yml. Make sure to replace your_target_host with the actual host or group of hosts where you want to install Docker.

Step 2: Executing Ansible-playbook

To execute the playbook, use the following command.

ansible-playbook install_docker.yml

Make sure you have Ansible installed on your machine and SSH access to the target host before running the playbook.

Step 3: Checking Docker Version

We need to execute the following command to check the installed docker version.

sudo docker info

Conclusion

We have successfully installed Docker using Ansible playbook on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.

Author

Installing Docker using Ansible-playbook on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top