π§ Automated Multi-Server Deployment using Ansible Roles & AWS EC2
π Project Overview
This project demonstrates automated deployment of a Flask web application across multiple AWS EC2 instances using Ansible.
The setup follows industry best practices with roles, systemd services, and an Nginx reverse proxy.
π§± Architecture
Component | Description |
---|---|
Master Node | Runs Ansible playbooks and manages worker nodes |
Worker Nodes (2) | Hosts the Flask web app and Nginx reverse proxy |
Ansible | Used to automate app deployment, dependency installation, and configuration |
AWS EC2 | Cloud infrastructure for hosting the servers |
βοΈ AWS Infrastructure
- 1 Master EC2 Instance (Ubuntu)
- 2 Worker EC2 Instances (Ubuntu)
- All instances connected via same key pair for SSH authentication
π§© Project Structure
ansible-flask-deploy/
βββ inventory.ini
βββ site.yml
βββ roles/
β βββ web/
β βββ tasks/
β β βββ main.yml
β βββ templates/
β β βββ flaskapp.service.j2
β βββ files/
β βββ nginx.conf
βββ deploy_page.yml
βββ README.md
π§ Playbook: site.yml
- name: Deploy Flask Web App
hosts: servers
become: yes
roles:
- web
π§Ή Role: roles/web/tasks/main.yml
- name: Install dependencies
apt:
name:
- python3
- python3-pip
- nginx
state: present
update_cache: yes
- name: Copy application files
git:
repo: "https://github.com/yashsonawane/flask-sample-app.git"
dest: /var/www/flaskapp
version: main
- name: Install Flask
pip:
name: flask
- name: Create systemd service
copy:
dest: /etc/systemd/system/flaskapp.service
content: |
[Unit]
Description=Flask App
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/var/www/flaskapp
ExecStart=/usr/bin/python3 app.py
Restart=always
[Install]
WantedBy=multi-user.target
- name: Start Flask service
systemd:
name: flaskapp
state: started
enabled: yes
- name: Configure Nginx reverse proxy
copy:
dest: /etc/nginx/sites-available/flaskapp
content: |
server {
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
- name: Enable Nginx site
file:
src: /etc/nginx/sites-available/flaskapp
dest: /etc/nginx/sites-enabled/flaskapp
state: link
force: yes
- name: Restart Nginx
service:
name: nginx
state: restarted
π Inventory: inventory.ini
[servers]
server1 ansible_host=54.82.25.149
server2 ansible_host=18.215.175.106
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible_master-key-demo.pem
π How to Run
- Clone the repository
git clone https://github.com/yourusername/ansible-flask-deploy.git
cd ansible-flask-deploy
Update inventory file
Replace the IPs and key path ininventory.ini
.Run the playbook
ansible-playbook -i inventory.ini site.yml
- Access the web app
http://<worker-node-public-ip>
π§ Key Learning
- Using Ansible roles for modular automation
- Automating multi-server app deployment
- Setting up systemd services for Flask apps
- Configuring Nginx reverse proxy
- Managing AWS EC2 instances via Ansible inventory
π§Ί Resume Line Example
Automated multi-server web application deployment on AWS using Ansible roles, systemd, and Nginx reverse proxy with dynamic inventory management.
π§βπ» Author
Yash Sonawane
π DevOps Engineer in Progress | Cloud & Automation Enthusiast
π GitHub Profile
Top comments (0)