Apache WebServer Configuration in Docker using Ansible
Tags: ansible, docker, linux
Published: 2020-11-27 15:04:11

In this blog, we will configure **Docker** and configure webserver in docker container using **Ansible**.

Before we start, we should know about Ansible and Docker.

###What is Ansible?
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ye1e4k6ebwr98m7eru3p.jpg)

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency.

###What is Docker?
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eazahje5j3l5lmthnmbi.png)
Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. 

##Terminologies related to Ansible:
> **Control Node**:

Any machine with Ansible installed. You can run Ansible commands and playbooks by invoking the **ansible** or **ansible-playbook** command from any control node. You can use any computer that has a Python installation as a control node - laptops, shared desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a control node. You can have multiple control nodes.

> **Managed Node**:

The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called **hosts**. Ansible is not installed on managed nodes.

> **Inventory**:

A list of managed nodes. An inventory file is also sometimes called a `hostfile`. Your inventory can specify information like the IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling. To learn more about inventory, see the Working with Inventory section.

> **Modules**:

The units of code Ansible executes. Each module has a particular use, from administering users on a specific type of database to managing VLAN interfaces on a specific type of network device. You can invoke a single module with a task, or invoke several different modules in a playbook. Starting in Ansible 2.10, modules are grouped in collections.

> **Playbook**:

Ordered lists of tasks, saved so you can run those tasks in that order repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in **YAML** and are easy to read, write, share and understand. To learn more about playbooks, see Intro to playbooks.

###Let's get started:-

##Prerequisite:

- **Yum** should be configured in the Managed Node.

This blog contains the following tasks:

❄ Configure Docker

❄ Start and enable Docker services

❄ Pull the httpd server image from the Docker Hub

❄ Run the httpd container and expose it to the public

❄ Copy the html code in /var/www/html directory and start the 
   Webserver

- The system that you want to make **Control Node** install **Ansible** in it using command `pip3 install ansible`, then install **openssh** using command `yum install openssh`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x4551n0t10qo42adbze0.png)

 Here **redhat** is my Control Node and **RHEL_arth** is Managed Node.

- After installing Ansible create an **Inventory** anywhere in the system, here I have created an inventory named `ip.txt`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yvw7bsul9tndxmfil2fs.png)

- Now inside inventory put Managed Node **username**, **Password** and the **connection type** .

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zeokrlmdgr59kx1ap41z.png)

- Now check the connectivity with Managed Node using the command `ansible all -m ping`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/812rgsvowdfamx6784ys.png)

If it shows a message in **green color** that means there is connectivity and you will see the message **ping pong**.

- Now  create a directory `mkdir /etc/ansible`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xtqchc2dl5mk8wjjk7wh.png)

- Inside `etc/ansible` create ansible configuration file `ansible.cfg` and inside configuration file add the path of an inventory and set **host key checking false** so that ` it will not verify when we first time connect through ssh` 👇

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mfbv55qmlk7wy62s8td8.png)

- Now inside `/etc/ansible` directory create **yml file**, here i have created file name **docker.yml**. This yml file is **playbook** where we write code in **YAML**.

Now code inside docker.yml 👇

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m26dc2gc5vch75dil47b.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1n80q1kixdilytpud3yj.png)

- Now run the playbook using the command `ansible-playbook docker.yml`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/com9ruboaephgj9nz29v.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pfytj6784o314tyxpfbe.png)

##Now check in Managed Node or Target node is changes made

- Docker installed
 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dhpt91kpmhvu0guswdlb.png)

Earlier in Manage Node docker was not installed but after running playbook docker is installed👇

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p1zq3icqy7avo7vjsvpi.png)

- Docker repo created in `/etc/yum.repos.d`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ihxvkxvifvnnt4gci7l8.png)

- httpd image launched in Docker

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a2dj3x2v03urjcop8d63.png)

- Now check IP of the container

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rn3vf5ohbngm057w535n.png)

- Launched Webserver
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r1ptmqki3mfoom1cdh9y.png)

Thank You!!✨✨