Install docker on ubuntu using ansible with python3

Installing Docker on Ubuntu using Ansible with Python3 can be done in different ways. In this article, we will explore three different options to achieve this task.

Option 1: Using the apt module

---
- name: Install Docker on Ubuntu
  hosts: localhost
  become: yes
  tasks:
    - name: Install required packages
      apt:
        name: ['apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common']
        state: present

    - 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 bionic stable
        state: present

    - name: Update apt cache
      apt:
        update_cache: yes

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

This option uses the apt module provided by Ansible to install Docker on Ubuntu. It first installs the required packages for Docker installation, adds the Docker GPG key, adds the Docker repository, updates the apt cache, and finally installs Docker using the apt module.

Option 2: Using the shell module

---
- name: Install Docker on Ubuntu
  hosts: localhost
  become: yes
  tasks:
    - name: Install required packages
      shell: apt-get install -y apt-transport-https ca-certificates curl software-properties-common

    - name: Add Docker GPG key
      shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

    - name: Add Docker repository
      shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

    - name: Update apt cache
      shell: apt-get update

    - name: Install Docker
      shell: apt-get install -y docker-ce

This option uses the shell module provided by Ansible to execute shell commands directly. It performs the same steps as option 1 but uses shell commands instead of Ansible modules.

Option 3: Using the docker_compose module

---
- name: Install Docker on Ubuntu
  hosts: localhost
  become: yes
  tasks:
    - name: Install Docker Compose
      pip:
        name: docker-compose
        executable: pip3

    - name: Install Docker SDK for Python
      pip:
        name: docker
        executable: pip3

    - name: Install Docker
      docker_compose:
        project_src: /path/to/docker-compose.yml
        state: present

This option uses the docker_compose module provided by Ansible to install Docker using a Docker Compose file. It first installs Docker Compose and the Docker SDK for Python using pip, and then uses the docker_compose module to install Docker using the specified Docker Compose file.

Among the three options, Option 1 is the recommended approach as it utilizes Ansible modules specifically designed for package management and repository handling. It provides better readability, maintainability, and portability compared to executing shell commands directly (Option 2) or using Docker Compose (Option 3) for simple Docker installation.

Rate this post

17 Responses

    1. I respect your caution, but sometimes taking risks can lead to great rewards. The Shell module might have its challenges, but it also has potential for innovation. Its all about finding the right balance between safety and pushing boundaries. Keep an open mind!

Leave a Reply

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

Table of Contents