Install and Configure Docker using Ansible in Ubuntu 20.04

Install and Configure Docker using Ansible in Ubuntu 20.04

Ansible with Docker

Notes:

Ansible Server must be Ubuntu 20.04 and Nodes also 20.04

— -
- hosts: ubuntu20
become: true
vars:
container_count: 4
default_container_name: docker
default_container_image: avian19/corona
default_container_command: sleep 1d

tasks:
— name: Install aptitude
apt:
name: aptitude
state: latest
update_cache: true
— name: Install required pacakges
apt:
pkg:
— apt-transport-https
— ca-certificates
— curl
— software-properties-common
— python3-pip
— virtualenv
— python3-setuptools
state: present
update_cache: true

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

- name: Add Docker Repository
apt_repository:
repo: deb download.docker.com/linux/ubuntu focal stable
state: present

- name: Update apt and install docker-ce
package:
name: docker-ce
state: latest
update_cache: true

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

- name: Pull default Docker image
community.docker.docker_image:
name: “{{ default_container_image }}”
source: pull

name: Create default containers
community.docker.docker_container:
name: “{{ default_container_name }}{{ item }}”
image: “{{ default_container_image }}”
command: “{{ default_container_command }}”
state: present
with_sequence: count={{ container_count }}

After using this playbook, you have configured docker successfully. Congratulations.