š Retrieving the container IP and updating the Inventory file using Ansibleā¦š
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
[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
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 š¤ā¦.