Ansible playbook to retrieve container IP and then configure it with web server

Deepanshu Yadav
4 min readMay 17, 2021

Prerequisites- Ansible configured in your system.

make a new inventory file with any name and update it in ansible.cfg file as shown below. 👇

✨you just need to write the highlighted line. ✨

Now let me show you the whole playbook and then will explain step by step what we have done and how👇

  1. First of all, we have asked the user for the docker os name that he want to configure using the vars_prompt module of ansible

2. Then we added the docker repo in the system and installed it by using yum_repository and command module and then also permanently started the docker services in the system using service module.

3. Then we have to install some extra libraries of python which are required for the remote execution in the docker containers as shown below

4. Now, we have pulled an image from dockerhub in which ssh server is already configured as it is needed for remote login through ssh client. You can use any image with such capability or you can create your own also. if you want to use mine, link is in the end of this story🎇.

5. Now, we launched the container with the same name that we collected from the user/client. After that, we aslo collected the information about this docker container and stored in a variable register🍤.

🔰6. Now comes the main thing which helps in retrieving the ip and this will also help in updating the inventory for the new containers🔰.

👉 we retrieved the Ip from the register variable (and printed it also using debug module) and used it to update the inventory by using the template module. Through template module, we are uploading a file which will configure the IP database in inventory that we wrote in ansible.cfg with the docker container ip.🍤

Make file with any name and write as shown above. i made it with the name ip_docker.txt 👲.

Now is the time to configure our docker system, since we now know the ip and it is in inventory file, therefore we ansible can go inside that container and configure it🤞.

🎈I have shown to configure web server, but you can configure anything you want🎈.

Now is the time to run this playbook🔥

You can see your inventory file now come up with the docker ip already written👇

Now open your browser and put the ip

so, now your web server is successfully running on docker🔥.

so, in this way you can anything configure inside your container only you need an ssh enabled image. Link to my image👇

Link to code👇

For any queries, DM me on linkedin anytime. Thanks for your time😊.

--

--