Launching the Web server and deploying the code on top of the Docker by using Ansible Playbook
Published: 2021-03-29 11:36:40

In this article,  We are going to configuring the **Apache Web server** and deploying the code on it on the docker container in the managed node by using **ansible-playbook**.

Before jumping to the practical, let's get familiar with Ansible.
## Ansible
It is an open-source tool mainly used for **configuration management**, **application deployment** and in some cases also for **software provisioning**. Ansible is written in **Python** language and it uses **ssh protocol**.

It uses **YAML** file for configuration management of the system. 



![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1616993554083/8dVhQvhJDq.png)

You can configure multiple systems by sitting in one system. The systems that need the configuration are called as Managed nodes and the system that configured the multiple managed nodes can be called as Controller Node. Typically,there will be only **one** controller node that will configure the **"N"** number of managed nodes.
  
Ansible has an **agentless architecture**, means the software that helps the configuration need to be installed only on a single Controller node only. This is the core competency and unique feature for Ansible that possess, this helps Ansible to have a market share of **74.67%** in the **configuration-management** market.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1616993840825/6En92Jlkx.png)

The above figure reflects the interest of using Ansible for configuration management of their managed nodes.

Let's get into the practical.

**Note:** I used **Amazon 2 Linux EC2** instances for this practical. I used container image as **httpd**

## Controller node setup
First step is to configure the Ansible software or package in the Controller node. Since Ansible is written in Python, so we can install ansible by using pip3 command.

 `yum install python3 -y`

 ` pip3 install ansible` 

Then you create an inventory file which contains all the meta data of the managed nodes which you want to configure. You can find the inventory file which I configured in below figure.


![1 vim ip.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1616994812027/ipnh6kbF0.jpeg)

After creating the inventory file, you have to **update** it in **configuration file**. The ansible's configuration file is located in `/etc/ansible/ansible.cfg` . If not there means, you can create ansible folder using `mkdir` command and ansible.cfg using `touch` command.


![config file.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1616995040233/vHVBRtFDE.jpeg)

The above figure shows the configuration file and how to update the inventory in it.

After completion, you can use the below command to verify there is a connection between managed node and controller node.
`ansible all -m ping`

![2 ping.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1616995254520/xoGPFKV8E.jpeg)

The above figure shows that the controller node has a reliable connection with the managed nodes.

## Practical

### Steps involved
 
- Configuring Docker repo
- Creating directory for mount
- Copy the file to that directory
-  Python3 installation
-  Installation of Docker module
-  Installation of IP tables
-   Docker Installation
-  Starting/Executing Docker service
-  Launch of Docker Container
 

## 1.Configuring Docker repo
For installation of Docker, first step is to creating one **yum repo** called Docker.repo. Using this repo, we can install docker in our OS. 
 ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617000088862/MIop1FxDW.png)

## 2. Creating directory for mount
Usually code is stored in file only and files are stored in directory. The deployment of code in docker container requires **storage**. We are providing storage by **mounting** docker storage to this directory. So that, in future if you want to deploy any webapp in container, you can store that program file in this baseOS directory.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001007796/XnlgDlmTP.png)

## 3. Copy
After the creation of directory, next step is to copy the file you want to deploy in webserver in the above mentioned directory.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001061732/DyBRlxlJx.png)

## 4. Python3 installation
Next step is to install the Python3 using yum module, since ansible is written in Python and so that we can install the python-docker module in future. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001108153/49WMWeXzp.png)

## 5. Installation of Docker module
Then we have to install the docker module in python by using pip 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001144069/sifz7D8GP.png)

## 6. Installing IP tables
In some cases, this step is important, since IP tables are required to start the docker service in some systems. Because in some versions of docker,  docker has no pre-installed IP tables. To avoid those conflicts, we are installing IPtables using yum module.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8mnqbz810lhfetdqz5w.png)
 

## 7. Docker Installation
Next step is to install the docker by using **yum** module.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001243687/x0qLR3gbN.png)

## 8. Start and make Docker service permanent
Then we have to start the **service** of Docker and enable the service for avoiding the service stop due to system **reboot**.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001300907/i5hMGGE5i.png)

## 9. Pull image from DockerHub
We are pulling httpd image from DockerHub by using **docker pull** command
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001336605/t1dGXU_Zg.png)

## 10. Launching Container using docker image
Finally we are going to launch one container named **httpd140** with exposed port of **8040** and also we attached docker container httpd's document root to the baseOS or host's directory. Since, the file in **document root** can only deployed it to the webserver.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001372022/EMHKetvgT.png)

##Output


![7 yml output 1.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001521382/ol0U-CBTL.jpeg)


![8 yml output 2.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001545153/rdSs9Fwje.jpeg)




![expose checking.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1617001565196/H6gTnvkUm.jpeg)


You can check this scripts from my github
GitHub URL: https://github.com/vishnuswmech/Launching-the-Web-server-and-deploying-the-code-on-top-of-the-Docker-by-using-Ansible-Playbook.git

We are done with it!!. Thank you all for your reads. Stay tuned for upcoming articles!! Suggestions are always welcome!!