Hands-On Ansible Project: Deploying a Web App on Nginx Servers🔥
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:
Setting up the Master Server
Create an EC2 instance and set up Ansible
Verify Ansible installation
Create the Ansible inventory
Deploying the Web App
Prepare the web application files
Create an Ansible playbook to install Nginx, start the service, and transfer files
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.
Create a Master server (EC2 instance) and three servers with one key pair for all servers
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
Now check whether Ansible properly install or not using the following command
cat /etc/ansible/hosts
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

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

- 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

- 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

Now we will deploy a sample webpage using the Ansible playbook.
so first we will create an index.html file
vim index.html

- 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
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
Now Check prod_inv server IP address using this command:
ansible-inventory -i /home/ubuntu/ansible/inventories/prod_inv --list -y
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 !!!