Retrieving new container IP and updating inventory
Yash Indane
Tech Enthusiast | Integrating Technologies | 1x AWS Certified | 6x Microsoft Certified | Cloud Computing | DevOps
?? Task 14.2 ARTH - Create an Ansible playbook that will retrieve new Container IP and update the inventory. So that further Configuration of Webserver could be done inside that Container.
Github link->
It is always necessary to get the new container IP, when a new container is launched in docker, so that further configuration can be done.
We get the container IP by ansible facts.
here we use loops, as we have to configure the repos, in the same way.
What are hostvars in Ansible?
hostvars is a magical variable in Ansible. Variables related to Ansible are called magic variables.
The difficulty that we face here in this case is that we launch the container and register the IP variable. Now in order to save the IP variable we have to save it in inventory, which is in controller node. So if we write other play and try to access the registered variable from the first play, we get error as that variable is only for the first play.
In order to solve this we use hostvars.
lineinfile module -
- This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression.
- This is primarily useful when you want to change a single line in a file only.
now run the playbook by
ansible-playbook playbook2.yml
Our webpage is deployed on the container, and we are able to access the page due to exposing the port.