AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Install Docker in Debian-like systems - Ansible module apt_key, apt_repository and apt

By Luca Berton · Published 2024-01-01 · Category: installation

How to automate the installation of the docker-ce engine in Ubuntu 20.04 LTS x86_64 (or amd64) using Ansible Playbook.

Install Docker in Debian-like systems - Ansible module apt_key, apt_repository and apt

How to Install Docker in Debian-like systems with Ansible?

I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.

See also: Install Google Chrome in Debian like systems - Ansible module apt_key, apt_repository and apt

Ansible install Docker in Debian-like systems

• Add Docker key => ansible.builtin.apt_key • Add Docker repository => ansible.builtin.apt_repository • Update apt cache and install Docker => ansible.builtin.apt

In order to install Docker on a Debian-like system we need to perform three different steps. The first step is to download the GPG signature key for the repository. You are going to use the ansible.builtin.apt_key Ansible module. This encrypted key verifies the genuinity of the packages and the repository and guarantees that the software is the same as Docker releases. The second step is to add the add Docker repository to the distribution. It's an extra website were apt, your distribution package manager looks like for software. You are going to use the ansible.builtin.apt_repository Ansible module. The third step is to update the apt cache for the available packages and install Docker (docker-ce) using the ansible.builtin.apt Ansible module.

Parameters

apt-key url string - URL • apt-key state string - present/absent • apt_repository repo string - repository • apt_repository state string - present/absent • apt name string - name or package specific • apt state string - latest/present/absent • apt update_cache boolean - no/yes

For the ansible.builtin.apt_key Ansible module I'm going to use two parameters: "url" and "state". The "url" parameter specifies the URL of the repository GPG signature key and the "state" verify that is present in our system after the execution. For the ansible.builtin.apt_repository Ansible module I'm going to use two parameters: "repo" and "state". The "repo" parameter specifies the repository parameters and the "state" verify that is present in our system after the execution. For the ansible.builtin.apt Ansible module I'm going to use three parameters: "name", "state", and "update_cache". The "name" parameter specifies the package name (Docker in our use-case) and the "state" verify that is present in our system after the execution. Before installing the package the "update_cache" performs an update of the apt-cache to ensure that the latest version of the package is going to be downloaded.

See also: Install Microsoft Edge on Debian with Ansible

Links

Install Docker Engine

## Playbook

Install Docker in Debian-like systems with Ansible Playbook

updated

Package docker-ce changed name to docker.

---
- name: install Docker
  hosts: all
  become: true
  tasks:
    - name: Install apt-transport-https
      ansible.builtin.apt:
        name:
          - apt-transport-https
          - ca-certificates
          - lsb-release
          - gnupg
        state: latest
        update_cache: true

- name: Add signing key ansible.builtin.apt_key: url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg" state: present

- name: Add repository into sources list ansible.builtin.apt_repository: repo: "deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable" state: present filename: docker

- name: Install Docker ansible.builtin.apt: name: - docker - docker.io - docker-compose - docker-registry state: latest update_cache: true

original code

---
- name: install Docker
  hosts: all
  become: true
  tasks:
    - name: Install apt-transport-https
      ansible.builtin.apt:
        name:
          - apt-transport-https
          - ca-certificates
          - lsb-release
          - gnupg
        state: latest
        update_cache: true

- name: Add signing key ansible.builtin.apt_key: url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg" state: present

- name: Add repository into sources list ansible.builtin.apt_repository: repo: "deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable" state: present filename: docker

- name: Install Docker ansible.builtin.apt: name: - docker-ce - docker-ce-cli - containerd.io state: latest update_cache: true

execution

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory install\ Docker/debian.yml
PLAY [install Docker] *****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [ubuntu.example.com]
TASK [Install apt-transport-https] ****************************************************************
changed: [ubuntu.example.com]
TASK [Add signing key] ****************************************************************************
changed: [ubuntu.example.com]
TASK [Add repository into sources list] ***********************************************************
changed: [ubuntu.example.com]
TASK [Install docker-ce] **************************************************************************
changed: [ubuntu.example.com]
PLAY RECAP ****************************************************************************************
ubuntu.example.com         : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory install\ Docker/debian.yml
PLAY [install Docker] *****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [ubuntu.example.com]
TASK [Install apt-transport-https] ****************************************************************
ok: [ubuntu.example.com]
TASK [Add signing key] ****************************************************************************
ok: [ubuntu.example.com]
TASK [Add repository into sources list] ***********************************************************
ok: [ubuntu.example.com]
TASK [Install docker-ce] **************************************************************************
ok: [ubuntu.example.com]
PLAY RECAP ****************************************************************************************
ubuntu.example.com         : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

$ ssh devops@ubuntu.example.com
Last login: Mon Nov 22 12:06:47 2021 from 192.168.0.102
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
$ docker --version
-sh: 2: docker: not found
$ apt list installed docker-ce -a
Listing... Done
$ 

after execution

$ ssh devops@ubuntu.example.com
Last login: Mon Jan 24 08:53:26 2022 from 192.168.0.102
$ sudo su       
root@ubuntu:/home/devops# cat /etc/apt/sources.list.d/docker.list 
deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
root@ubuntu:/home/devops# docker --version
Docker version 20.10.12, build e91ed57
root@ubuntu:/home/devops# apt list installed docker-ce -a
Listing... Done
docker-ce/focal,now 5:20.10.12~3-0~ubuntu-focal amd64 [installed]
docker-ce/focal 5:20.10.11~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.10~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.9~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.8~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.7~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.6~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.5~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.4~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.3~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.2~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.1~3-0~ubuntu-focal amd64
docker-ce/focal 5:20.10.0~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.15~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.14~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.13~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.12~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.11~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.10~3-0~ubuntu-focal amd64
docker-ce/focal 5:19.03.9~3-0~ubuntu-focal amd64
root@ubuntu:/home/devops# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:975f4b14f326b05db86e16de00144f9c12257553bba9484fed41f9b6f2257800
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
root@ubuntu:/home/devops#

code with ❤️ in GitHub

Conclusion

Now you know how to install Docker in Debian-like systems with Ansible.

See also: Ansible Playbook for Installing Docker on Linux Systems

Related Articles

container lifecycle with Ansiblestatic and dynamic Ansible inventorysudo and become in Ansible playbooks

Category: installation

Watch the video: Install Docker in Debian-like systems - Ansible module apt_key, apt_repository and apt — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home