Retrieve Container IP and Update Ansible Inventory Using Ansible
WHAT IS DOCKER?
Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.
WHAT IS ANSIBLE?
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows.
When automating Docker services using Ansible sometimes we need to create more containers and configure some services in them but the problem is after creating a container we need to have its IP and ssh credentials to do ssh. This problem is solved by retrieving the container IP as soon as it launches using an ansible-playbook.
one more problem is that normally container doesn’t have ssh enabled so we also need a custom Docker image that has ssh enabled so we can perform this task so I have already created a custom docker image you can get it from here:
https://hub.docker.com/r/venkatesh14/sshimage
Task Objective
Using the ansible-playbook retrieve container IP and update the Ansible inventory. So that further Configuration of the Webserver could be done inside that Container.
Configuration plan
- Launch container and Retrieve IP of the container.
- Do the configuration inside the new Container using ansible
NOTE: all the source code is in the GitHub link provided in the last
Let’s Get Started
inventory.txt.j2
This file is used to create an inventory for the Docker container.
[{{container_name}}]
{{result['container']['NetworkSettings']['IPAddress']}} ansible_connection=ssh ansible_user=root ansible_ssh_pass=root
Note: Here you can use blockinfile module as well for appending further IPs in the same file.
Following is the ansible-playbook that will launch a container from our custom images and using the above template we will create inventory consisting of Docker IP and ssh credentials.
To execute the playbook using the following keyword:
ansible-playbook "playbook name"
In the following execution we can see that first, we set SELinux to permissive so we will not get any problems, and then we pulled the fresh image from docker and launched it. note that we also passing a name for the container in the very first step you can see in the image.
After the container launched using ansible facts we able to retrieve the IP of the container and after that using the above inventory template it will update the IP in the Container inventory.
Following is the Container Ip inventory:
Step 2. Do the configuration inside the new Container using ansible
Now we have a new Inventory that is created by the last step using this inventory we will do configuration inside the container.
First, we will install a web server then we have some dummy content we will copy it to DocumentRoot of the webserver. then we will start the server.
we have executed this playbook you can see in the following image.
Finally, We can see that we are able to retrieve the container IP, and using this IP we configured the webserver successfully and the result is very much clear.
GitHub Link for source code:
https://github.com/venkateshpensalwar/ARTH/tree/main/Ansible/Retrive_containerIP
Conclusion:
Hope this blog is helpful to you!!!!
No comments