Integration of Docker with Ansible!!

As we know that it brings huge time when we install packages or configure large numbers of servers manually. We face many issues while installing packages in heterogeneous environment. We don’t even know which command the servers support. This creates complexity in work and make the simple process tedious to adopt. Deploying application on web server also needs some sort of automation.

This task is given by Mr. VImal Daga Sir


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 httpd container and expose it to the public

🔹 Copy the html code in /var/www/html directory and start the web server.

Why Docker🐬?

Why Ansible 🤔?

Automating Ansible with Docker🤠

> Pre-requisites:

  • controller node where we write ansible playbook
  • managed node which is to be managed.

Let’s Start our journey!!

First of all, Start Controller node and install Ansible.

Ansible doesn’t come under Redhat 8 version dvd. Python is the base language for ansible. Therefore, we have to use pip command to install ansible.

pip3 install ansible

.> Check whether the ansible is installed or not using following command.

ansible --version

It will also show location of inventory(database of host ip’s). But at first time it will show none.

Check host ip which we have to manage:

Let’s create an inventory:

Here we provide host ip with ssh user and password for connectivity.

Make a configuration file for ansible:

Here provide inventory location and disable key checking so that system will use ssh user and password instead of key.

Check whether the hosts are connected to controller nodes:

Let’s write ansible-playbook:

I] First of all, we need to docker repo to the managed node. The docker package is downloaded from baseurl. Register keyword will store output of command and var keyword will print output using debug module.

II] We have downloaded docker package in the previous step. Now we need to install.

III] After installation, start the docker services.

IV] As the base language for ansible is python, we need to install docker sdk using pip3.

V] Let us copy our webpages to the manager node using copy module.

VI] This step is important. In this step, we are creating docker container and starting docker services. The port address translation of httpd 80 port is done. The webpages we have copied into the managed nodes are copied to the apache webserver’s directory /var/www/html.

Our Task come to an End.

Let’s run ansible playbook!!

Check whether the docker services in Managed Node started or not.

Check whether our apache webserver is working fine or not.

And It’s Working !!