Retrieving Docker container IP and loading variable dynamically through ansible

harsh
3 min readAug 24, 2021

--

Docker the most demanded and used technology in the industry, docker not only makes it easier to create, deploy, and run applications by using containers but it also simplifies the process of building, running, managing, and distributing applications.

Usually, we append IPs manually in the inventory file but in today’s world, we can’t do this as we have thousands of containers launching and deleting at the same time.

Now we are gonna launch the docker container and will append the IP of the docker container for further configuration dynamically in the inventory file, in our case we will configure the Apache HTTP server

In my case, I have made a playbook which I am gonna use as shown below

after writing the playbook just run your playbook with ansible-playbook <file name> command

as you can see our playbook runs without any errors and works well.

Now we will see another scenario on how Ansible Playbook will dynamically load the variable file named the same as os_name and just by using the variable names we can Configure our target node.

In my case, I have made a playbook which I am gonna use as shown below

and in our variable file, we have used two variables with respective the variable package names and service names, the extension of variable file should be yml but ansible supports other extensions also.

after writing the playbook and variable files just run your playbook with the ansible-playbook <file name> command

just make sure your variable file should have a name as same as your operating system distributor name otherwise it won’t work.

as you can see our playbook run’s perfectly and have no errors.

and our web server works perfectly with the message we wanted to deliver.

If you liked the above blog, please share it.

LinkedIn Link :-

--

--