Install & Manage Docker Using Ansible Playbook

Khemnath chauhan
1 min readApr 12, 2022

I have created below Ansible playbook to configure, install and manage docker using Ansible playbook.

- hosts: awsnode
tasks:
- name: Download the docker repo file from docker site.
get_url:
url: "https://download.docker.com/linux/rhel/docker-ce.repo"
dest: "/etc/yum.repos.d"
# There is issue on above docker-ce.repo file content, the packages not available under rhel, it's available under centos.
# Below steps will first replace the existing repo file's rhel string with centos.
- name: Replace the rhel keyword in repo file with centos.
replace:
path: "/etc/yum.repos.d/docker-ce.repo"
regexp: 'rhel'
replace: 'centos'
- name: Install Docker Software
package:
name: "docker-ce"
state: present
- name: Start docker service
service:
name: "docker"
state: "started"
- name: Install pip command to install python library.
package:
name: "python38-pip"
state: present
- name: Install docker-py python Library.
pip:
name: "docker-py"
state: forcereinstall
- name: Pull docker Image name ubuntu
docker_image:
name: "ubuntu"
# This below line i have added to fix the issue during the docker_image module execution. Not mandatory to use this path otherwise.
vars:
ansible_python_interpreter: /usr/bin/python3
- name: Create the docker container from above ubuntu image.
docker_container:
name: myubuntu
state: started
image: ubuntu
command: sleep infinity
vars:
ansible_python_interpreter: /usr/bin/python3

--

--