🛠Retrieving the container IP and updating the Inventory file using Ansible…😎

Scenario

Scenario : Our aim is to dynamically update the inventory file of hosts ,so that on the fly we can configure docker container as WebServer once it is launched….

Let we get insight over how it could be done :

1️⃣First of all , we would create our own ssh_enabled docker image:

first of all ,create working directory ..

#mkdir dynamic_inven

#cd /dynamic_inven

Now create docker file :

#vim Dockerfile

Dockerfile is the default name recognized by the docker build command….

🔴From the centos image we have created customized image which is ssh_enabled with root user password as ‘docker .’

Now lets build the docker file:

#docker build -t ssh_os:v1 .

we have built , ssh_os named docker image as version 1.

Its recommended to create the Dockerfile in same working directory

Now lets check if the Docker Image exists in local repo or not:

#docker images

Now once we had our image time to configure ansible files:

lets see:

#cd /etc/ansible

create ansible.cfg file”:

#vim ansible.cfg

create inventory file : dock_ip.txt (in our case)

#vim dock_ip.txt

save it empty only , our ansible-playbook will be updating it soon……..

Lets first configure managed node for docker :

#vim docker.yml

In above playbook in managed node…..

→We have configured yum repo as well as docker repo

→Installed net-tools for CLI based OS

→Installed docker

→Started docker daemon

→Installed yum-utils and python3

→Ansible module for docker

Now , its time to retrieve IP of launched container in docker engine…

we have created dynamic.yml playbook for this purpose, lets see what it consist….

In this playbook from above built image ; ssh_os : v1
→We have launched container with some user defined name , ssh_port and webserver_port….

→Also at line #42 we have updated the inventory file , “dock_ip.txt”

Using blockinfile module of ansible -- This module will insert/update/remove a block of multi-line text surrounded by customizable marker lines.

→as well as at line #32 we have register i.e stored the variable named container, which will hold the information about launched docker container.

→The “container‘ variable will be used to update the inventory :

container variable contains key — [‘container’][‘NetworkSettings’][‘IPAddress’] which will be helping in retrieving the container ip , so that we will update this is inventory file…

After running this playbook we can see , that dock_ip.txt is updated…

That is we have successfully retrieved the IP of newly launched conatiner

Now lets copy the html pages to webserver and start the services:

We have created another playbook for this :

#vim docker_setup.yml

→ In this playbook , we have installed httpd package .

→Secondly , copied the static html pages to document root /var/www/html

→Then, started the httpd server , using /usr/sbin/httpd

As container image is not booted as systemd, thus here we can’t use services module to start web-server…

Finally , we have started the web server in launched container , lets see

Now , check if the web-server is running or not :

As port 80 is exposed to some customized port , we can use Base system ip to access the webpages.

Bravo ! Server is up…..

Thus, we have dynamically updated the inventory file using ansible blockinfile module and configured the retrieved container IP as web-server..

There are several scripts too available for performing the above updation..

You will find the above used playbooks below:

🛠🛠happy configuring🤔🤔 ………

Hope you find it helpful 🤗….

Technological Enthusiast , Like to express what is need of time, Relates real world to philosophical insights