🛠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 ..
Now create docker file :
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:
Now once we had our image time to configure ansible files:
create ansible.cfg file”:
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)
save it empty only , our ansible-playbook will be updating it soon……..
Lets first configure managed node for docker :
In above playbook in managed node…..
→We have configured yum repo as well as docker repo
→Installed net-tools for CLI based OS
→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 :
→ In this playbook , we have installed httpd package .
→Secondly , copied the static html pages to document root
→Then, started the httpd server , using
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..
Dynamic Inventory - Ansible Documentation
Often a user of a configuration management system will want to keep inventory in a different software system. Ansible…
You will find the above used playbooks below:
Ansible PlayBook that does the following operations in the managed nodes: 🔹 Configure Docker 🔹 Start and enable…
🛠🛠happy configuring🤔🤔 ………
Hope you find it helpful 🤗….