Day 59: A Step-By-Step Guide to Deploying a Simple Web App with Ansible🚀
Day#59 Of 90 Days Of DevOps Challenge
🌈Introduction
Ansible, an influential automation tool, has gained significant acclaim among system administrators and DevOps engineers for its capacity to streamline and simplify the deployment and administration of IT infrastructure.
In this blog entry, we will guide you through the steps required to deploy a basic web application with Ansible.
By the conclusion of this guide, you'll have a web server operational on multiple instances, serving a fundamental webpage.
📜Task of the Day
📌Step 01: Setting up the Environment
Note: For this step you can refer to previous blogs as well
Create 3 EC2 Instances
First, launch three EC2 instances on your cloud platform of choice. Ensure that you have the necessary credentials and access rights to create these instances.
Install Ansible in a Host Server
Log in to a host server that will act as the Ansible master.
Add the Ansible PPA repository:
sudo apt-add-repository ppa:ansible/ansible
Update the package list and install Ansible:
sudo apt update sudo apt install ansible
Verify the installation:
ansible --version
Copy the Private Key
To ensure Ansible can SSH into the created instances, we'll need to copy the private key from your local machine to the host server.
You can place the private key in the
/home/ubuntu/.ssh
directory OR you can create a folder as per your choice & place a private key in that folder.scp /path/to/your-key.pem ubuntu@your-host-server:/home/ubuntu/.ssh/
After this, ensure you can access Node-1 and Node-2 from the master node using SSH by connecting to their private IP addresses.
ssh username@Node1_Private_IP ssh username@Node2_Private_IP
If you can access both nodes without a password prompt, your SSH key setup is successful.
Create an Inventory File
Now, create an Ansible inventory file on the master server. This file should contain the private IP addresses of both Node-1 and Node-2, allowing Ansible to manage these servers.
Verify Connection with Ping
Confirm that Ansible can connect to the nodes using the following command:
ansible -i inventory all -m ping
A successful ping response indicates that the servers are active and reachable.
📌Step 02: Create an Ansible Playbook to Install Nginx
Now, it's time to create an Ansible playbook to install Nginx, a popular web server.
Create a new file named install_nginx.yml
and add the following content:
- name: Install Nginx
hosts: all
become: yes
tasks:
- name: Update apt package cache
apt:
update_cache: yes
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started
Execute the playbook using the following command, replacing (Path-of-inventory)
with the actual path to your inventory file:
ansible-playbook install_nginx.yml -i (Path-of-inventory)
This playbook will install Nginx on all the nodes listed in your inventory file.
📌Task 03: Deploy a Sample Webpage
To complete your web application deployment, create a simple HTML webpage. Create an index.html
file and add the following content:
<!DOCTYPE html>
<html>
<head>
<title>Day 59 Of 90 Days Of Devops </title>
</head>
<body>
<h1>By Vishal</h1>
<h2>Task of the Day</h2>
<p>Pages (HTML)</p>
<p>Style Sheets (CSS)</p>
<p>Computer Code (JavaScript)</p>
<p>Live Data (Files and Databases)</p>
</body>
</html>
Now, update the install_nginx.yml
playbook to include the deployment of this webpage. Modify the playbook to look like this:
- name: Install Nginx and Deploy Website
hosts: all
become: yes
tasks:
- name: Update apt package cache
apt:
update_cache: yes
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
- name: Deploy website
copy:
src: index.html
dest: /var/www/html
Execute the updated playbook in the same way as before.
This playbook will not only install Nginx but also copy your index.html
file to the appropriate directory, making your website accessible on all the servers.
Now, Check and Verify the website deployed on all the servers.
👋Conclusion
By following these steps, you have successfully deployed a simple web application using Ansible.
This process showcases the power of automation, as Ansible simplifies server provisioning, configuration, and deployment, enabling you to efficiently manage your infrastructure.
You can expand on this foundation to automate more complex tasks and configurations in your environment.
Happy learning & may your cloud journey be filled with exciting discoveries!🎊
Thank you for joining us on this exciting Day 59 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥
So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :
Hashnode: vishaltoyou.hashnode.dev