Ansible playbook that will Create and retrieve new Container IP and update the inventory.
This type of architecture can be used in many used cases, to solve with some small steps like further Configuration of Web server could be done inside that Container, And I would be explaining that in this blog.
What is Ansible ?
Ansible is a configuration management tool, that automates the configuration of multiple servers by the use of Ansible playbooks.
So let’s began with the practical ..
First, I created a Docker file with the ssh configuration and then build an image and uploaded in the docker hub ,So that the image can be used future.
Second, I created a playbook that would first do the configuration in the localhost then to the IP present in the docker group which would be updated by the Ansible-Playbook.
INSIDE THE PLAYBOOK
- Here host is localhost.
- Used the vars_prompt module which helps user to provide with the container name .
- Used the copy module though I think there is no use of this step here ,but to give a idea, If needed We can customize the module. Here I am configuring yum for the docker to be installed .
- Used the command module to directly provide the command to install docker.
- Used the package module to install python3 package .
- Used the command module to install docker-py library which is used by ansible to connect to docker .
- Used the service module to start and enable the docker service.
- Now in this step, I used the docker_image module to pull the image from the docker hub but commented the step as I had pulled the image previously.
- Used the docker_container module to launch container.
- Used the docker_container_info module to get information about the container and used register function to store all the output to the variable we provide in the function.
- Used the debug module to look onto the information to retrieve the IP.
- Used the debug module to print IP address to the screen ,To make sure.
- Used the blockinfile module to update the inventory with the IP fetched.
- Here host is docker group.
- Used the package module to install httpd.
- Used the command module to start and enable the service of httpd.
- Used the copy module to Create a webpage with content.
Now, Running the playboook .
We can confirm my docker container is launched.
Yes, the webpage is updated inside the docker container .