Introduction
In modern DevOps practices, automation is not optional — it is mandatory. Managing servers manually does not scale and often leads to configuration drift and human error. This is where Ansible plays a critical role.
In this article, I will walk you through a real-world DevOps project where I automated the deployment and configuration of Nginx on AWS EC2 using Ansible.
This project follows industry-standard practices and is suitable for beginners to intermediate DevOps engineers preparing for interviews or building a strong portfolio.
Project Overview:-
The goal of this project is to:
- Automate Nginx installation on AWS EC2
- Configure Nginx using Ansible playbooks
- Manage servers using Ansible inventory
- Deploy a custom web page automatically
- Validate deployment via browser access
- Follow real DevOps workflow used in production environments
This project is fully automated, agentless, and reproducible.
Architecture Flow:-
- Ansible Control Node initiates execution
- Inventory file defines target EC2 server
- Secure SSH connection is established
- Ansible installs and configures Nginx
- Service is started and enabled
- Web page is deployed
- End user accesses application via browser
This architecture reflects how real DevOps teams manage infrastructure.
Services and Tools Used:-
AWS Services:-
- Amazon EC2 — Control node and target server
- Security Group — Allows ports 22, 80, and 81
- VPC — Default AWS networking
- Key Pair — Secure SSH authentication
DevOps Tools:-
- Ansible — Configuration management and automation
- YAML — Playbook definition
- Nginx — Web server
- Linux (Amazon Linux) — Operating system
- SSH — Remote connectivity
Project Structure:-
devops-ansible-handlers-nginx-aws-project/
├── inventory.ini
├── without_handlers.yml
└── README.md
Each file has a specific role and keeps the automation clean and maintainable.
Inventory File Explained:-
The inventory file defines which servers Ansible will manage.
[targetserver]
172.31.6.233 ansible_user=ec2-user ansible_ssh_private_key_file=/home/ec2-user/terraform.pemKey Points:
- Host group name helps organize servers
- Private IP ensures secure internal communication
- SSH key enables passwordless authentication
- No agent installation required on target server
This demonstrates Ansible’s agentless architecture.
Ansible Playbook Explained:-
Play Definition:-
The playbook targets the EC2 server group and runs with elevated privileges.
- name: deploy nginx without handler
hosts: targetserver
become: yesVariables Section:-
Using variables improves reusability and readability.
vars:
pkg: nginx
svc: nginx
file_path: /usr/share/nginx/html/index.html
conf_file: /etc/nginx/conf.d/custom.confInstalling Nginx:-
- name: install nginx
ansible.builtin.dnf:
name: "{{ pkg }}"
state: presentThis ensures idempotent installation — Ansible installs Nginx only if it is not already present.
Starting and Enabling the Service:-
- name: start and enable nginx
ansible.builtin.systemd_service:
name: "{{ svc }}"
state: started
enabled: yesThis guarantees Nginx:
- Starts immediately
- Automatically starts after system reboot
Configuring Nginx:-
- name: configure nginx server block
ansible.builtin.blockinfile:
path: "{{ conf_file }}"
create: yes
block: |
server {
listen 81;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}This dynamically creates a custom Nginx configuration and listens on port 81, demonstrating real configuration management.
Deploying the Web Page:-
- name: deploy index.html
ansible.builtin.copy:
dest: "{{ file_path }}"
content: "Deployed by Ansible"This task deploys a static web page automatically without manual file transfer.
Playbook Execution:-
The entire automation is executed using a single command:-
ansible-playbook -i inventory.ini without_handlers.yml
This single command performs installation, configuration, and deployment.
Deployment Verification
After successful execution:-
- Nginx is running
- Configuration is applied
- Web page is accessible
Access URL:-
http://<EC2-PUBLIC-IP>:81
This confirms the automation worked correctly.
Why This Project Matters:-
Many tutorials show toy examples, but this project reflects how:
- DevOps engineers automate servers in production
- Configuration consistency is maintained
- Manual errors are eliminated
- Infrastructure becomes scalable and repeatable
This makes it interview-ready and portfolio-worthy.
Author
Mahesh Shelke
AWS & DevOps Trainee
- GitHub: https://github.com/Maheshshelke05
- Portfolio: https://www.maheshbhau.xyz
- LinkedIn: https://www.linkedin.com/in/mahesh-shelke-7497a7315
- Medium: https://medium.com/@maheshshelke05
