Retrieving Docker Container IP and auto-updating the Ansible Inventory using playbook…

Arjun Nigam
3 min readDec 27, 2020

Hey guys, I am back with another interesting practical for you . Everyone knows now how to configure webserver on a docker container using Ansible. If not, here’s the link to my previous article where I have shown how to achieve the same.

https://arjunnigam10.medium.com/configuring-web-server-on-a-docker-container-using-ansible-3ed545ced2cc

So now that everyone is on the same page. Let me ask you, what can we do after it. What I meant was, what if I need to do further Configuration of Webserver inside that Container. Won’t we be needing the IP of the container and update it in the inventory.

But who will do that tedious work of noting the IP of the container and then updating the inventory file.

So why not let Ansible do the work…

Today, I am gonna show you how…

You guys are well aware of ansible facts and its pre-defined variables right…

So just with the help of them. I have completed this task.

So let’s get started…

I used Two IP’s as hosts : First- the one on which I configured the docker container and inside it webserver.

And second : My Local Host ( For updation of the Inventory file)

I used the same playbook , used in the above mentioned article, and added to it the task of retrieval of the IP . With the help of Ansible facts, pre-defined variables and hostvars, I added the task of retrieving the IP and updating the inventory via Playbook. Image to which you can observe below.

I will leave the link to my playbook below.

As you can observe in the image below , My playbook ran successfully.

If you will observe more closely, you can observe in the image below , you would see the IP of the Container.

So now that our playbook ran successfully , let’s check whether our ansible inventory got updated or not.

And as you can observe in the image below , it got updated successfully…

Now that we have retrieved the IP of the Container we can do further Configuration of Webserver inside that Container.

Well that was all from my side. Hope you found that interesting. Keep learning :)

View my Playbook on Github:

https://github.com/Arjun0071/ipretrieve_anisble

Arjun Nigam

https://www.linkedin.com/in/arjun-nigam-4728b11b8

Signing Off!!!

--

--