šŸ› Retrieving the container IP and updating the Inventory file using Ansibleā€¦šŸ˜Ž

Ansh@24
4 min readMar 29, 2021
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

Image build success!!!!

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

[defaults]
inventory = /etc/ansible/dock_ip.txt
host_key_checking = false
deprecation_warnings = false
ask_pass = false
[privilege escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

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

docker.yml

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ā€¦

dynamic.yml

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 šŸ¤—ā€¦.

--

--

Ansh@24

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