Deploying NGINX Server & Host your Webpage Using Ansible - Ubuntu

DevOps is all about collaboration and learning from each other! Let's connect on Hashnode and exchange ideas, tips, and success stories! π€ π https://hashnode.com/@Kshitijaa π§ kshitijabartakke17@gmail.com πΈ www.linkedin.com/in/kshitija-bartakke-malwade-39678b141
Join me on this thrilling DevOps journey as we embrace innovation, automation, and a brighter future for software development! π Let's build a seamless digital world together! ππ» #DevOps #Automation #ContinuousInnovation #CI/CD #InfrastructureAsCode
Deploying Webpage using the ansible-playbook
Automation is the backbone of modern infrastructure management. Among the automation tools available, Ansible stands out due to its simplicity and power. In this blog, we'll demonstrate creating an Ansible playbook to install NGINX and deploy a sample webpage.
Introduction to the Project
Our goal is to automate the deployment of an NGINX server and serve a custom webpage without any manual intervention. We'll use Ansible, a configuration management tool that communicates over SSH and doesn't require any agent installation on remote hosts.
Prerequisites
Ansible is installed on your control machine.
A target server (or local machine) where you want to install NGINX.
SSH access from the control machine to the target server.
Follow my previous blog to configure all Prerequisites https://kshitijaa.hashnode.dev/understanding-configuration-management-with-ansible-1
Step 1: Setting up the Inventory
Your inventory file (inventory.ini) should look something like this:
web_server ansible_host=3.87.241.176 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem ansible_ssh_user=ubuntu
Replace host with your server's Public IP and ansible_ssh_user with the appropriate SSH username.
Now, we will set the permission as 600 for this file
sudo chmod 600 /home/ubuntu/keys/inventory.ini
Use the below Ansible ad hoc command to ping the servers:
ansible web_server -i inventory.ini -m ping

Step 2: Writing the Playbook
Create a file called nginx.yml and let's dive into the content:
- name: Configure Website with nginx
hosts: web_server
become: True
tasks:
- name: install nginx
apt: name=nginx update_cache=yes
- name: copy index.html
template: src=./sample.html dest=/usr/share/nginx/html/index.html
mode=0644
Make sure to have a sample.html file in the same directory as your playbook, or adjust the src path accordingly.
Step 3: Running the Playbook
Execute the playbook with:
ansible-playbook -i inventory.ini nginx.yml
Step 4: Verification
Visit the IP address of your server on a web browser. You should see the content of your sample.html displayed.

Conclusion
In just a few steps, we have automated the deployment of an NGINX server and a custom webpage using Ansible. This approach is scalable; with minimal adjustments, you can deploy to multiple servers or incorporate more complex configurations.
The power of Ansible lies in its simplicity and readability. By using playbooks, infrastructure as code becomes an easily attainable reality. As always, ensure you thoroughly test your playbooks in a staging environment before deploying to production.




