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.

Prasantmahato
4 min readApr 6, 2021

--

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.

Image_used: prasant1998/sshtesting

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

First Play

  • Here host is localhost.

STEP1

  • Used the vars_prompt module which helps user to provide with the container name .

STEP2

  • 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 .

STEP3

  • Used the command module to directly provide the command to install docker.

STEP4

  • Used the package module to install python3 package .

STEP5

  • Used the command module to install docker-py library which is used by ansible to connect to docker .

STEP6

  • Used the service module to start and enable the docker service.

STEP7

  • 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.

STEP8

  • Used the docker_container module to launch container.

STEP9

  • 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.

STEP10

  • Used the debug module to look onto the information to retrieve the IP.

STEP11

  • Used the debug module to print IP address to the screen ,To make sure.

STEP12

  • Used the blockinfile module to update the inventory with the IP fetched.

Second Play

  • Here host is docker group.

STEP1

  • Used the package module to install httpd.

STEP2

  • Used the command module to start and enable the service of httpd.

STEP3

  • 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 .

Open for any query and suggestions .

THANKYOU

--

--