Configure Docker using Ansible Playbook

Naveen Pareek
3 min readMar 22, 2021
Configure Apache Webserver on Docker using Ansible.

Before discussing about the actual task. Let us some of the basics of Ansible and Docker.

What is Ansible?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

Because Ansible is all about automation, it requires instructions to accomplish each job. With everything written down in simple script form, it’s easy to do version control. It enables Infrastructure As A Code (IAAC).

Ansible can be used in 2 ways for configuration management:
1. Ad-hoc
2. PlayBook

Some basic terminologies need to know before using Ansible.

Controller Node: The system in which Ansible is installed is known as controller node.

Managed Node/ Target Node: The system on which configuration is done is known as Target node or Managed node.

Inventory: Inventory is a file in controller node which Ansible use to connect to target nodes. It consists of-

o IP Addresses. o Protocol. o Username. o Password.

Now let’s start discussing about the task.

Task Description:

🔰Write an Ansible Playbook that does the following operations in the managed nodes:
🔹 Configure Docker.
🔹 Start and enable Docker services.
🔹 Pull the httpd server image from the Docker Hub.
🔹 Run the docker container and expose it to the public.
🔹 Copy the html code in the /var/www/html directory and start the webserver.

Before performing the task, let's see how to install ansible over Redhat 8.0 Operating System.

As Ansible is one of the python library so we can install Ansible using the pip command. With this cmd, we also need to install some dependent software.

pip3 install ansible

yum install sshpass

Now, I'll show you some practicals of the given task in pictorial form step by step.

Initially, define the IP of Controller Node and Managed/Target Node

IP of Target Node
IP of Controller Node

Here we consider that ansible is already installed in the system and its inventory table is also configured with respective target node IP and credentials.

Step1: Check Connectivity between the hosts

Step2: Check whether docker is already installed or not in target node.

Step3: Write an ansible-playbook for configuring webserver on docker container in target node. (e.g., dock_web.yml)

Ansible-playbook file

Step4: Execute the ansible-playbook

Step5: Now check the outcome of process in the target node

IP Address of Target Node

Thank You

Keep Learning & Keep Growing

--

--