Fetching IP of a Docker Container || SSH Enabled Docker || Configuration Inside Docker Container Using Ansible

In an Ansible, managed hosts or servers which are controlled by the Ansible control node are defined in a host inventory file as explained in. A host inventory file is a text file that consists of hostnames or IP addresses of managed hosts or remote servers.

Managed hosts can either be listed as individual entries or categorized under a group name as we shall later see. In Ansible, there are two types of inventory files: Static and Dynamic. A static inventory file is a plain text file that contains a list of managed hosts declared under a host group using either hostnames or IP addresses.

In a configuration — especially a containerization setup such as Docker where the inventory file keeps constantly changing as you add or decommission servers, keeping tabs on the hosts defined in the inventory file becomes a real challenge. It becomes inconvenient going back to the host file and updating the list of hosts with their IP addresses.

In this blog, we’ll create and build a SSH enabled Docker image using Dockerfile and then launch a container from this image and retrieve IP address and update it in the inventory dynamically using ansible playbook and further configure webserver inside the launched docker container.

For this problem statement I have used my own ssh enabled docker image with the help of Dockerfile and pushed in DockerHub. We need a ssh enabled image because by default ansible use ssh protocol to login inside any container or os and do configuration there. Here is my docker image which you can directly download and use:

Docker Hub

CentOS-SSH

hub.docker.com

Now, the next part is to create an Ansible playbook that will launch the container, retrieve its IP, and update the IP in the inventory dynamically.

This playbook will ask for the container name, the exposed ports for SSH and WebServer and then it will launch the container from the given image. After launching, it will register the output in a variable named ‘x’. Then it will retrieve the IP of the container for the variable and update it in the inventory file called ‘ip.txt’.

Output of the playbook:

Inventory ip.txt:

Next we just have to write a playbook for webserver and ansible will configure it inside the container

Output of the playbook

Here I’ve used IP of the base container with port number that I exposed i.e., port 500. Other way is you can write the IP of the container to view your webpage created.

And hence, we have successfully launched a container and retrieved and updated IP address using ansible playbook and also configured the webserver inside it.

This task was completed in collaboration with Divya Kurothe, I would like to have this opportunity to thank her for her hard work and support, thank you(❁´◡`❁).

Thank you all for reading, I hope you enjoyed. You are super duper awesome\^o^/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store