Ansible Playbook that retrieves new container IP, update inventory, and configure webserver using ssh

Write an Ansible PlayBook that does the following operations in the managed nodes:

🔹 Configure Docker

🔹 Start and enable Docker services

🔹 Run the docker container and expose it to the public

🔹 Retrieve new Container IP and update the inventory

🔹 Copy the html code in /var/www/html directory and start the web server. Do further Configuration of Webserver inside that Container.

Prerequisites: python interpreter, Ansible, sshpass should be installed

If config file = None ,then do the following:

mkdir /etc/ansible

vim /etc/ansible/ansible.cfg , press i to insert contents as below:

Press Esc, then press :wq to save

Step 1: Configure docker

  1. Configure yum repository for docker
  2. Install docker

Step 2: Start and enable docker services

Step 3: Pull the centos_ssh server image from the Docker Hub

  1. Install docker sdk
  2. Pull the centos_ssh server image

OR use Dockerfile to build image

Step 4: Launch docker container using centos_ssh server image

Step 5:Retrieve container IP

For this first retrieve container name , as docker_container_info module needs name of container to give all information about it. Then retrieve container IP and set fact.

Step 6: Update the inventory

ip_temp.yml file will rkeep on updating inventory file

Step 7: Copy contents to /var/www/html/ folder

Step 8: Start web server

You can find all the code on the link below:

Note: Run Playbook in following order: 1) docker_cont.yml 2) doct_cont2.yml

Important Note: doct_cont2.yml will run successfully only when we are running this playbook on ansible host as it uses ssh to configure and docker container IP are local so they can easily use ssh to configure

After running docker_cont.yml successfully inventory file ip.txt looks like below:

The web server is started and when we use http://localhost:8080 in web browser we can see our web page