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

Sumayya Khatoon
5 min readMar 26, 2021

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.

Pre-requisites:

  1. Ansible installed and configured.
  2. Docker to be installed.
  3. Some basics of Ansible is required.

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 docker.py

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

  wget  https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/docker.py

After Downloading the script, make the scripts executable

  chmod +x docker.py
Files docker.py 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 docker.py 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

  ./docker.py
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

<IP_of_System:9999/route_page.html>

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.

Dockerfile

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 !!

--

--

Sumayya Khatoon

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