DEV Community

loading...
Cover image for Dynamically Updating Docker Container IP in the Inventory using Ansible

Dynamically Updating Docker Container IP in the Inventory using Ansible

piyushbagani15 profile image Piyush Bagani ・3 min read

Hello Readers👋👋,

Here In this Article, I will demonstrate how to Dynamically Update Docker Container IP in the Inventory using Ansible.

So Let’s get started…
Prerequisites:-

What are Ansible and Docker?

Below is the link to one of my other articles from where you can learn about Ansible and Docker.

Link:https://dev.to/piyushbagani15/configuration-of-apache-webserver-on-the-top-of-docker-using-ansible-nkf

Now we know what Ansible and Docker so without any further delay, let’s get started with the demonstration…

→ Create an Inventory file with an extension .txt, Here I have created a file called ip.txt and We don’t need to add the IP address in that as it will be dynamically updated.
alt text
→ Create Ansible Configuration file:

To create an ansible file, create a file with the extension .cfg, here I have a configuration file with named ansible.cfg
alt text
→ Now Create ansible playbook

Create an Ansible playbook with an extension .yml, here I have created a playbook named docker_new.yml
alt text
alt text
alt text
Don’t Worry about the code I will provide the link of GitHub Repository, from where you can find the playbooks.

I have created the Ansible playbook in such a way that it will configure Docker, install docker, pull Docker image, and then fetch the IP of the container and updated it in an inventory of the controller node.

Here While launching docker container, it will pull the image which I have created and uploaded it to Docker Hub. In this Image I have Dockerized SSH- Install SSH and Enable SSH Service in Docker, this is because Docker containers are not pre-configured with SSH service so because of this We won’t be able to update IP dynamically. This is the reason I have created seperate image. Tricky😰 Right, Don’t Worry….

For Your Reference only I am providing Steps to do above tricky task of Dockerizing SSH

Make sure to follow these steps in creating image-

yum install net-tools -y
yum install openssh-server -y
ssh-keygen -A
put /usr/sbin/sshd in /root/.bashrc file
set some password for root account, which is used in inventory file during ssh

Reference Links:

Link: https://docs.docker.com/engine/reference/commandline/commit/
Link: https://docs.docker.com/engine/reference/commandline/push/

→Now Lets run the Playbook

To run the playbook use the command ansible-playbook
alt text
alt text
Yeah, Playbook ran successfully!!!🎇🎇

→ Let’s Check ip.txt file…

Earlier we have seen that ip.txt was empty but it is updated dynamically.
alt text
→ So that’s how we can configure the docker container in such a way that the IP of the container is updated dynamically to the Inventory of Controller Node.

Also I have created playbook for configuring Web-Server and The Target Node will be the container and we got its IP updated in Inventory. So we are good to go…

→ Playbook named webserver-docker.yml
alt text
Now after running this playbook we got :
alt text
Hurrah🎇🎇, The Web-Server is configured properly inside Container.
That Is It For this Article…..
Thank You for Reading….
Keep Learning, Keep Hustling🎯🎯

GitHub Link: https://github.com/PiyushBagani15/Ansible-Docker_Integration/tree/main/Ansible-Docker_container-Dynamic

Discussion (0)

pic
Editor guide