Retrieving Container IP/Hostname Dynamically Using Ansible and Configuring a WebServer Inside the Container

Welcome guys! In this article I’ve showed you the way of how one can retrieve container’s IP / Hostname Using Ansible and then Configuring Webserver inside the Container. So, guys get deep dive into it…

What is Ansible?

Ansible is an open source IT configuration management (CM) and automation platform, provided by Red Hat. Ansible is well known for its Configuration Management. Ansible is Python Product. Ansible works on Declarative Approach. Ansible loves to do the repetitive tasks which is very tedious in the Agile World. Ansible works on PUSH mechanism. Ansible is a DevOps tool which basically used to automate the things. Ansible works in the concept of Idempotence-means if the desire already exists then it won’t do that task again. It uses playbook to describe automation jobs. Playbook uses a simple language called YAML. One Playbook can contain one or more Plays.

Ansible is good at automation but if it comes to inventory then there’s come one challenge in Ansible which is Static Inventory. In some situation, we need to update our Inventory manually which is really very time consuming as well as in this Agile World it is very tedious kind of Work. So, here comes the solution to it which is none other than Dynamic Inventory.

What is Dynamic Inventory?

Dynamic inventory is an inventory which can add the the Inventory /Host on the fly from dynamic sources, including cloud sources, by using the supplied inventory script.

Why we need Dynamic Inventory?

Static Inventory doesn’t help when we’ve complex infrastructure ,especially when we keep on installing the Operating System. In that case, Dynamic Inventory takes place.

What is Docker?

Docker is a software which has the capability to launch the Entire Operating System / Container within one second of time span.

What is Apache Web Server?

Apache HTTP Server is a web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.


  1. Ansible installed and configured.

So, lets implement the Practical →

There’s a Python pre-created script that’s available to retrieve the Dynamic Inventory…

Step1: Configuring Ansible Inventory

Step 2: Writing Ansible Playbook that will Configure Yum Repo for Docker, Install the Docker , Then launch the Docker Container for the same.

Here is the Playbook for same

Playbook ran successfully!!

Step 3: Download the Python Scripts.

Download the script in your System. You can download from this location

First make one separate directory. In my case, I’ve created /etc/dynamic_host/


After Downloading the script, make the scripts executable

  chmod +x
Files downloaded successfully

Step 4:Doing some Modification in the script downloaded

After Downloading the files , we need to do some modification to it…

Go inside the file called using vim command and in the first line itself make some changes. Instead of #!/usr/bin/env python write #!/usr/bin/python3

Changes made successfully

Now we’re good to go…

Step 5: Retrieving the Tags of the Container using the Dynamic Inventory script of Docker

All the tags has been retrieved successfully!!

Step 6: Finally, Retrieving the hosts

 ansible all --list-hosts
Host retrived successfully!!
Container launched Successfully!!

Step 7: Testing the Webserver

Fetch the IP of your System and the port 9999 and browse it in your browser


Webserver Deployed Successfully inside the Docker Container!!

Alternative Way:

Knowing extra is always a Good thing

Step 1: Creating Docker that enables SSH

Ansible uses SSH Protocol to go inside the System for Configuration Management.


Step 2: Building the Docker Image

Image built successfully and tagged as ssh:v1

Step 3: Running the Playbook that will Launch the Container and Dynamically retrieve the IP of Container and then will launch the WebServer over it.

Here is the playbook for same

And here is the output for same:

Playbook ran successfully

Step 5: Testing the Webserver

Webserver Deployed successfully!!

Voila!! Web Server has been successfully Deployed inside the Docker Container..

Hope you find this Blog easy🤞!!

Happy Reading!!😊

Do like Share and Follow me

Thank you !!

Machine Learning || Deep Learning || Kubernetes|| Docker || AWS || Jenkins || Ansible(RH294) || Python || Linux(RHEL8 )