Automate Docker With Ansible for launching WebServer

Automate Docker With Ansible for launching WebServer

Ansible is one of the most powerful tool for configuration management. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments.

This is a small task in which we learn to launch our own Webserver using Docker and Ansible that use the YAML file to automate the process.

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 deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

In a way, Docker is a bit like a virtual machine. Docker allows applications to use the same Linux kernel as the system that they're running on and only requires applications be shipped with things not already running on the host computer.

what is Webserver ?

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

Installing Ansible in Linux:

I'm using RHEL 8 here. You only need to have Python3 software installed. Ansible uses ssh to go inside the host OS so we also need one more software sshpass. Run these two commands to install Ansible...

pip3 install ansible
yum install sshpass

Configuration of Ansible:

Now we need to create one file where we give IP of our host server...

No alt text provided for this image

Ansible uses this file for check hosts OS and after that Go inside the OS using username and password. This also known as Inventory File.

After that, create one config file for Ansible where we give the location of the host file which we created above. Go to /etc and create one folder named ansible. Go inside the folder and create one config file named ansible.cfg

mkdir -p /etc/ansible
cd ansible
gedit ansible.cfg
No alt text provided for this image

For checking connectivity that all host node is properly connected, RUN

ansible --version

For checking connectivity that all host node is properly connected, RUN

ansible all -m ping

Now let's come to the task...

Task Description

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

As the ansible basic connection has been established now between the controlled node and managed node. Now coming to the ansible playbook make a yml file using gedit web.yml.

No alt text provided for this image
No alt text provided for this image

In above PlayBook:

1.Creating yum repository for docker.

2. Installing ansible using package module.

3.Install Docker Requirement on hosts i.e. docker-py.

4.Pull the Httpd Image from Docker Hub.

5.Launch the Container and exposed it to outer world.

now Running the PlayBook:

Command for run playbook...

ansible-playbook webserver.yaml
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Testing the results:

No alt text provided for this image

Task Completed🤝

Thanks for Reading...


Adarsh Kumar

Cloud Technical Solutions Engineer @ Google

3y

Well done Anant Prakash Sharma 👍

Like
Reply

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics