Ansible playbook and Python Script that will retrieve new Container IPs and Update the Inventory file for future use
In this blog created a python script that will setup ansible and runs the Ansible playbook which will Provision and Configure a Docker Container, whenever an IP changes Dynamically Ansible will be updated IP in Inventory.
Ansible: It is used for Provisioning and Configuring this will allows us to write a code once make use forever best thing in Ansible is IDEMPOTENT in nature.
Docker: Docker is a lightweight containerization that allows us to set up an infrastructure within one second with the help of images stored in Docker Registry even we can create our own Image too you can go through this article to create your own image I think you would like it.
Before diving into our project make sure requirements satisfied
Prerequisite
Python and Ansible installed
Software Versions
- OS RHEL8
2. Ansible 2.10.7
3. Python 3.6.8
4. Docker version 20.10.5, build 55c4c88
5. Apache/2.4.37 (Red Hat Enterprise Linux)
Let’s start our practical before starting make sure you have configured ansible configure file i.e,. ansible.cfg file add below code to setup
[defaults]
inventory= ./Inventory1
host_key_checking=false[privilege-escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=false
Create an Inventory and add
>> touch Inventorys
Create an Ansible playbook to configure, provision, retrieve new Container IPs, and Update the Inventory file for future use
Create a vars.yml file to upload variables for ansible-playbook
>> vim vars.yml
Below are the variables used in this ansible-playbook
Create a script that will accept an input from a user and updates to a ansible-playbook and at last runs a playbook
Create an index.py file for the python script
>> vim index.py
This is a code that will take input from a user then converts takes YAML file that will read a file then input taken by the user is updated to a YAML file then dumped to a YAML file i.e,. index.py, at last, will run an ansible-playbook
Before that, there are few packages that need to be installed
>> pip3 install ansible
>> pip3 install ansible_playbook_runner
>> pip3 install yaml
Below are the code needs to be added to index.py file
Files created so far in our workspace
Our code is ready now we are ready to go
We can run in two ways using python3 or another method
Go to index.py file in the first-line add below path you can find that path by running below command
>> which python3
/usr/bin/python3
Copy and paste path we got from the above command inside the index.py
#/usr/bin/python3
Then make your python script executable form as below
>> chmod +x index.py
After changing the file mode
Now we can run just running this command
>> ./index.py
Another method is using python3
>> python3 index.py
Give input after running a python script
Write Container Name, Image Name, Image Version, State of the container, the port number to expose to know more about the input you can see below image
The below GIF shows you how exactly output works
Image used in above GIF
The above GIF which will take an image as vinod2681997/myweb it’s my own image you can see below how I created an image using a script
You may get a doubt what if you choose an image that is already configured everything that to do with that even for that I made small changes by taking HTTPd image
This GIF shows you how exactly output works on HTTPd image
The below video whatever done above I created an Ansible playbook that will retrieve the new container IP and update the inventory. So that further Configuration of the Web server could be done inside that Container.
GitHub Link(Code)
If you found this article helpful, please don’t forget to hit the Follow and Clap buttons to help me write more articles like this.
Thank You 🖤