SnapShooter Learning Center

Install and Setup Docker Using Ansible on Debian 11

Install and Setup Docker Using Ansible on Debian 11

Simon Bennett]
Simon Bennett
Last Updated: Oct 27, 2021
Table of Contents

# Introduction

Automating server installation and configuration plays an important role in the system administration of modern application environments. Ansible is one such tool used to automate server installation and configuration. It is a simple, lightweight and powerful tool and it also provides a set of features and modules to write the automation script.

Setting up and managing whole IT environments are too complex and a very time-consuming process for a system administrator. Ansible helps system administrators and developers to simplify complex tasks, savw their time and allow them to focus attention on other tasks.


# Features

  • Free and Open-source.
  • Simple to set up and use.
  • Agentless.
  • Efficient.
  • Flexible.
  • Provides a lot of modules and plugins.

In this post, we will explain how to set up Docker using Ansible on Debian 11.


# Requirements

  • A server running Debian 11 operating system.
  • A root password is set up on your server.

# Add an Ansible Repository

By default, the Ansible package is not included in the Debian 11 default repository. So you will need to add the Ansible repository to APT.

First, install all the required dependencies using the following command:

apt-get install gnupg2 curl -y

Once all the dependencies are installed, edit the APT source list file and add the Ansible repository.

nano /etc/apt/sources.list

Add the following line:

deb http://ppa.launchpad.net/ansible/ansible/ubuntu focal main

Save and close the file then add the GPG key with the following command:

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367

Next, update the repository using the following command:

apt-get update -y

# Install Ansible

Now, you can install the Ansible package by running the following command:

apt-get install ansible -y

Once the Ansible is installed, verify the Ansible version using the following command:

ansible --version

You will get the following output:

ansible [core 2.11.6] 
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
  jinja version = 2.11.3
  libyaml = True

# Create an Ansible Playbook

Ansible playbook is a scripts file written in YAML format. It contains all steps which the user wants to execute on a particular machine. In simple word, the Ansible playbook is a core feature of Ansible and tells Ansible what to execute.

First, create a directory to hold the playbook and other required files.

mkdir Docker
mkdir Docker/vars

Next, create a default.yml file to store all variables. We will use this variable in the Ansible playbook.

nano Docker/vars/default.yml

Add the following lines:

---
create_containers: 4
default_container_name: docker
default_container_image: debian
default_container_command: sleep 1d

Save and close the file when you are finished.

Where:

  • create_containers: - Create a 4 container on the remote machine.
  • default_container_name: Create a container with name docker.
  • default_container_image: Use debian image to create a container.
  • default_container_command: Run sleep 1d on each container.

Next, create a playbook.yml file to define all tasks to be executed on the remote machine to set up a Docker container.

nano Docker/playbook.yml

Add the following lines:

---
- hosts: localhost
  become: true
  vars_files:
    - vars/default.yml

  tasks:

    - name: Install required system packages
      apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']

    - name: Add Docker GPG apt Key
      shell: 'curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg'

    - name: Add Docker Repository
      shell: 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian bullseye stable" | tee /etc/apt/sources.list.d/docker.list'

    - name: Update apt and install docker-ce
      apt: update_cache=yes name=docker-ce state=latest

    - name: Install Docker Module for Python
      pip:
        name: docker

    - name: Pull default Docker image
      docker_image:
        name: "{{ default_container_image }}"
        source: pull

    # Creates the number of containers defined by the variable create_containers, using values from vars file
    - name: Create default containers
      docker_container:
        name: "{{ default_container_name }}{{ item }}"
        image: "{{ default_container_image }}"
        command: "{{ default_container_command }}"
        state: present
      with_sequence: count={{ create_containers }}

Save and close the file when you are finished.


# Run an Ansible Playbook

At this point, your Ansible playbook is ready to set up a docker container on the remote machine.

Now, change the directory to Docker and run the playbook on the local machine:

cd Docker
ansible-playbook playbook.yml -l localhost -u root 

Once the Ansible playbook is executed successfully, you will get the following output:

PLAY [localhost]                      

TASK [Gathering Facts] ************************************************************************************************************************
ok: [localhost]

TASK [Install aptitude using apt] *************************************************************************************************************
ok: [localhost]

TASK [Install required system packages] *******************************************************************************************************
ok: [localhost] => (item=apt-transport-https)
ok: [localhost] => (item=ca-certificates)
ok: [localhost] => (item=curl)
ok: [localhost] => (item=software-properties-common)
ok: [localhost] => (item=python3-pip)
ok: [localhost] => (item=virtualenv)
ok: [localhost] => (item=python3-setuptools)

TASK [Add Docker GPG apt Key] *****************************************************************************************************************
changed: [localhost]

TASK [Add Docker Repository] ******************************************************************************************************************
changed: [localhost]

TASK [Update apt and install docker-ce] *******************************************************************************************************
changed: [localhost]

TASK [Install Docker Module for Python] *******************************************************************************************************
changed: [localhost]

TASK [Pull default Docker image] **************************************************************************************************************
changed: [localhost]

TASK [Create default containers] **************************************************************************************************************
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
changed: [localhost] => (item=3)
changed: [localhost] => (item=4)
[DEPRECATION WARNING]: The container_default_behavior option will change its default value from "compatibility" to "no_defaults" in 
community.docker 2.0.0. To remove this warning, please specify an explicit value for it now. This feature will be removed from 
community.docker in version 2.0.0. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=9    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

You can now verify if the Docker container was created successfully using the following command:

docker ps -a

You should see that there are four containers are created and running.

CONTAINER ID   IMAGE     COMMAND      CREATED              STATUS    PORTS     NAMES
c9b167c8d2a7   ubuntu    "sleep 1d"   About a minute ago   Created             docker4
465bdd8eab13   ubuntu    "sleep 1d"   About a minute ago   Created             docker3
3f9c266f3e83   ubuntu    "sleep 1d"   About a minute ago   Created             docker2
7456c50168df   ubuntu    "sleep 1d"   About a minute ago   Created             docker1

You can also verify the Docker image created by the Ansible playbook using the following command:

docker images

You will get the following output:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    ba6acccedd29   12 days ago   72.8MB

# Conclusion

In the above post, we explained how to install and use Ansible to set up a Docker container on Debian 11. You can now customize your playbook to set up a Docker container on multiple remote machines.


Did you find this article helpful?