Objective
- Inventory groups
- Deploy different software to different servers
- Run playbook against specific group
- Use variables per group
- Understand real-world environment separation
Architecture
Control Node (Mac)
↓ SSH
EC2-1 → Web Server
EC2-2 → DB Server
Step 1 — Launch 2 EC2 Instances
Both:
- Ubuntu 22.04
- Port 22 open
- Port 80 open (for web)
- Same key pair
Example IPs:
Web: 3.141.22.178
DB: 3.141.25.200
Step 2 — Create Inventory with Groups
Edit inventory.ini
[web]
3.141.22.178 ansible_user=ubuntu ansible_ssh_private_key_file=~/Downloads/pem/key.pem
[db]
3.141.25.200 ansible_user=ubuntu ansible_ssh_private_key_file=~/Downloads/pem/key.pem
Test:
ansible all -i inventory.ini -m ping
Both should return pong.
Step 3 — Create Group-Based Playbook
Create:
vim multi-server.yml
Paste:
- name: Configure Web Server
hosts: web
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started
enabled: yes
- name: Configure Database Server
hosts: db
become: yes
tasks:
- name: Install mysql
apt:
name: mysql-server
state: present
- name: Start mysql
service:
name: mysql
state: started
enabled: yes
Step 4 — Run Playbook
ansible-playbook -i inventory.ini multi-server.yml
Students will observe:
- Web server installs nginx only
- DB server installs MySQL only
- Separate execution blocks
Step 5 — Verify
Web server:
http://3.141.22.178
Should show nginx.
SSH into DB server:
systemctl status mysql
Step 6 — Run Against Specific Group
Only deploy to web:
ansible-playbook -i inventory.ini multi-server.yml --limit web
This teaches targeting.
Step 7 — Add Group Variables (Important)
Create folder:
mkdir group_vars
Create:
vim group_vars/web.yml
package_name: nginx
Create:
vim group_vars/db.yml
package_name: mysql-server
Now modify playbook:
- name: Install package
apt:
name: "{{ package_name }}"
state: present
Group → Variable → Behavior change
This is real DevOps practice.
Real Industry Connection
In production:
[web] → Auto Scaling Group
[db] → RDS or DB cluster
[monitoring] → Prometheus servers
[workers] → Background processors
Ansible handles configuration layer.



Top comments (0)