DEV Community

Cover image for Docker Container IP Dynamically Update in an Inventory Using Ansible
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Docker Container IP Dynamically Update in an Inventory Using Ansible

Hello everyone,

This blog is a demonstration of the configuration of the Docker container in such a way that the container IP is dynamically updated in an Inventory of a Controller Node.

So Let's get started...

Prerequisites:-

What is Ansible and Docker?

Now we know what Ansible and Docker are so, let's get started with the demonstration...

First create an Inventory file

Create an Inventory file with an extension .txt, here I have created an Inventory file named ip.txt. We don't need to add the IP address of the Target node in that as it will be dynamically updated.

Alt Text

Create Ansible Configuration file

To create an ansible file, create a file with extension .cfg, here I have a configuration file with named ansible.cfg

Alt Text

Now create an Ansible Playbook

Create an Ansible playbook with an extension .yml, here I have created a playbook named docker.yml

Alt Text

Alt Text

This playbook 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.

Now run the Playbook

To run the playbook use command ansible-playbook <name-of-playbook>

Alt Text

Playbook ran successfully!!!✨

Now check that is IP updated in an Inventory of a Controller Node

Earlier we have seen that Ip.txt[ Inventory file] was empty👇

Alt Text

After running the playbook👇

Alt Text

Now we can see that IP is updated!!✨

So that's how we can configure the docker container in such a way that the IP of the container is updated dynamically to an Inventory of Controller Node.

Thank you!!⭐

Top comments (0)