Configure Web Server in Docker and start services using Ansible Playbook
Tags: Httpd, Web Server, Ansible, Docker

WHAT IS ANSIBLE ?

Ansible is a configuration management system written in Python using a declarative markup language to describe configurations. It is used to automate software configuration and deployment.

What is Docker ?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. The Docker daemon is a service that runs on your host operating system. Docker container is an open source software development platform. Its main benefit is to package applications in “containers,” allowing them to be portable among any system running the Linux operating system (OS). Docker Hub is a cloud-based registry service which allows us to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so we can deploy images to our hosts.

what is Webserver ?

A web server is a computer that runs websites. It’s a computer program that distributes webpages as they are requisitioned. The basic objective of the web server is to store, process and deliver web pages to the users. The communication between client and server takes place using the Hypertext Transfer Protocol (HTTP). A web server processes incoming network requests over HTTP and several other related protocols.

Ansible Architecture :

Ansible Playbooks :

Ordered lists of tasks, saved so we 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.

Inventory :

A list of managed nodes. An inventory file is also sometimes called a “hostfile”. Our inventory can specify information like IP address for each managed node. An inventory can also organize managed nodes.

Control Node:

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

Managed Node:

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

❗Task 1 — Ansible❗

STEPS TO DO THIS TASK :

🔹 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 web server.

First we have to create VM for Controller Node and Managed Node. As we can see in the below Image we are using rhel_ControlNode as Controller Node where as rhel_ManagedNode is for Managed Node.

VM’s

we can install ansible via pip command as it came from python

pip3 install ansible

after that we can check the version of Ansible.

ansible --version

We can create a group of IP in /root/MyHosts.txt, in that we have to write that Managed Node IP as a host in Controller Node. Give our Username and password of that Managed Node. By creating groups of IP known as Inventory can helps us to do automation that Node which are in group. As we can see there are 1 group below in the figure. If we want to do automation / configuration on multiple Managed Node then we can give their IP in one of the Group. we can give any name to group.here we didnt give any name so it consider as default.

MyHosts.txt

192.168.0.102 ansible_user=root ansible_ssh_pass=redhat ansible_connection=ssh

We need to give the path of MyHost.txt (IP data) in this file /etc/ansible/ansible.cfg inside inventory. To run this playbook we need to give ssh permission. This can be done via host_key_checking=FASLE.

Before running your playbook we need to check that all the Managed Node is connected or pingable with the Controller Node or not. This can be checked by following command. Those Managed Node IP is connected with Controller Node that only.

ansible all --list-hosts

first we have to download software epel-release and install sshpass to give ssh key checking .

After that we install sshpass using yum (Note : In controller Node it is required to configure yum. )

To check Connectivity with all Managed Nodes we can use

ansible all -m ping

Now, we are good to go for creating our own Playbook to run the code. Generally, Ansible Support YAML language in playbook. So, i have created two files one is a playbook web.yml and the other is index.html which we will be copying inside the container.

Code inside web.yml .

Code inside index.html

To run this playbook we need to write -

ansible-playbook web.yml.

For Detailed Explaination

ansible-playbook -vvv web.yml

After Successfully running the playbook, we can check the container whether it is launched successfully or not in Managed Node.

docker --version

docker ps

As we can see that we have launched our container Mywebserver successfully. We can also check the IP of that container.

docker inspect Mywebserver | grep IP

Now we can connect this IP and see the web page i.e index.html.

curl <ip>/filename.extension

We can also check it on browser

Hope it is informative …

Thank You !!