Skip to main content

Command Palette

Search for a command to run...

Hands-On Ansible Project: Deploying a Web App on Nginx Servers🔥

Published
•4 min read
N

I am actively seeking a DevOps role and am currently available for new opportunities. With hands-on experience and project involvement in Docker, Kubernetes (K8s), CI/CD, Jenkins, AWS, and DevOps practices, I am equipped with the skills to drive efficient and scalable infrastructure solutions. I am eager to contribute my expertise and continue learning in a dynamic DevOps environment. If you have any openings or know of relevant positions, feel free to connect with me on LinkedIn to explore opportunities and learn more about my skills and experiences.

Ansible playbooks are highly advantageous, as demonstrated in our previous blog on Ansible.
Today, we will embark on a hands-on project, where we will explore the powerful capabilities of Ansible to deploy a web application on an Nginx server. We will set up a Master server (EC2 instance) and three other servers, all using the same key pair for secure access. By leveraging Ansible, we can easily manage and deploy our application across multiple servers, streamlining the process and ensuring consistency.

Table of Contents:

  1. Setting up the Master Server

    • Create an EC2 instance and set up Ansible

    • Verify Ansible installation

    • Create the Ansible inventory

  2. Deploying the Web App

    • Prepare the web application files

    • Create an Ansible playbook to install Nginx, start the service, and transfer files

  3. Executing the Ansible Playbook

    • Deploy the web app to the designated server(s)

    • Verify successful deployment

Let's commence this endeavor and begin the journey toward our goal.

  1. Create a Master server (EC2 instance) and three servers with one key pair for all servers

  2. Connect the master server with SSH and install Ansible on it using the following command:

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y
  1. Now check whether Ansible properly install or not using the following command

     cat /etc/ansible/hosts
    

  2. Once the file is open, you can add the IP addresses or hostnames of the servers you want to manage. The format for adding hosts is as follows:

edit /etc/ansible/hosts file and add the public key of ansible server instances

  1. Let's verify the inventory that we have created.
ansible-inventory --list -y

  1. Copy the private key to the master server where Ansible is set up

The below command is to copy the private key to a remote server from the local machine folder and after that copy the private key on ssh folder of the remote server

sudo scp -i "an-access-key.pem" an-access-key.pem ubuntu@ec2-18-208-153-82.compute-1.amazonaws.com:/home/ubuntu/.ssh

  1. Try ping command using Ansible to the Nodes.

To test that Ansible is able to connect to your nodes, you can use the following command:

ansible servers -m ping

  1. Now we will deploy a sample webpage using the Ansible playbook.

    so first we will create an index.html file

vim index.html

  1. Create a playbook to install, start and enable the Nginx and transfer the index.html file to the Nginx location.
vim deploy_webapp.yml
-
 name: this is a simple html project deploy using ansible
 hosts: servers
 become: true
 tasks:
   - name: install nginx
     apt:
       name: nginx
       state: latest

   - name: start nginx
     service:
       name: nginx
       state: started

   - name: Deploy webpage
     copy:
       src: index.html
       dest: /var/www/html
  1. Execute the Ansible playbook YAML file to perform the task.

    To run the playbook on the prod_inv server, use the following command.

     ansible-playbook -i ansible/inventories/prod_inv deploy_webapp.yml
    

Here, we have deployed a web page one server named "prod_inv". If you want to deploy the web page on all dedicated servers, simply use the following command instead of the "prod_inv" server:

ansible-playbook deploy_webapp.yml
  1. Now Check prod_inv server IP address using this command:

     ansible-inventory -i /home/ubuntu/ansible/inventories/prod_inv --list -y
    

  2. Here, you can find the IP address of the Ansible host: 54.85.98.171. Please wait for a moment of magic!

    Now, please copy the IP address and open your browser. Hit Enter!

Boom!! Enjoy the experience!

In this hands-on project, we successfully deployed a web application on an Nginx server using Ansible. By following the step-by-step guide, we created a Master server and multiple nodes with one key pair for seamless access. Ansible proved to be a valuable tool for automating the installation and configuration of Nginx on the nodes and deploying our web application. This project showcased the flexibility and efficiency of Ansible, making it an essential tool for managing infrastructure and simplifying the deployment process.

By mastering Ansible, you gain the ability to manage large-scale infrastructure and deploy applications with ease, making it a valuable skill for any DevOps engineer or system administrator. Whether you're just starting with Ansible or looking to enhance your expertise, this project provides a practical and insightful learning experience that empowers you to tackle complex infrastructure management tasks effectively. So, roll up your sleeves, dive into the world of Ansible, and take your automation skills to new heights!

Are you ready to embark on your Ansible journey? Follow along with this project, and you'll soon be orchestrating your own infrastructure with ease.

— Happy Learning !!!

Let’s connect !!!

Linkedin

Github

Mail

Medium

More from this blog

Untitled Publication

92 posts