This content originally appeared on Level Up Coding - Medium and was authored by Divya Kurothe
Retrieving Docker Container’s IP Dynamically Using Ansible and Configuring a WebServer Inside the Container.
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:
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 Prithviraj Singh, and would like to thank him for his constructive efforts and support.
That’s all folks. Thanks for reading :)
Retrieving Docker Container’s IP Dynamically Using Ansible and Configuring a WebServer Inside the… was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Divya Kurothe
Divya Kurothe | Sciencx (2021-03-31T12:01:51+00:00) Retrieving Docker Container’s IP Dynamically Using Ansible and Configuring a WebServer Inside the…. Retrieved from https://www.scien.cx/2021/03/31/retrieving-docker-containers-ip-dynamically-using-ansible-and-configuring-a-webserver-inside-the/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.