Chapter 79: DevOps Projects Part 9: Ansible Nginx Roles Vault Patch Inventory Agents
79.1 Project 61: Install and configure Nginx on 10 servers
Friends, ten web nodes must look the same. Today we build an inventory of ten, write an Nginx playbook, ping the fleet, apply with serial batches, and curl every node.
मित्रांनो, दहा web nodes एकसारखे हवे. आज inventory of ten, Nginx playbook, ping, serial batches, आणि प्रत्येक node curl.
दोस्तों, दस web nodes एक जैसे चाहिए. आज inventory of ten, Nginx playbook, ping, serial batches, और हर node curl.
What you will build in this project
- Inventory of 10 hosts.
- Nginx install playbook.
- Ping then apply.
- Serial batches and curl checks.
1. Start from a real company story
At PhonePe in Mumbai, one Nginx was edited by hand and missed a TLS line during a campaign.
Three of ten nodes failed health checks. Rough cost: about ₹25 lakh in failed checkouts that hour.
Ravi made inventory plus playbook the only path for web tier changes.
Today we configure ten Nginx hosts the Ansible way.
Ravindra Bagale's Tip
Students use chmod tricks on web roots to fix permission errors. Use the right owner and 0644. Keep that habit.
Students permission साठी चुकीच्या mode वर जातात. योग्य owner आणि 0644. सवय ठेवा.
Students permission के लिए गलत mode लगाते हैं. सही owner और 0644. आदत रखिए.
2. Start and prepare
Step 1: Why Ansible for Nginx
Why: Ten identical edits by hand will drift. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: echo the problem then install Ansible. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 1: a1
Type this:
echo 'One playbook configures many hosts the same way'
You will see:
One playbook configures many hosts the same way
What it means: Why.
Example 2: a2
Type this:
python3 -m pip --version 2>/dev/null | head -1 || echo 'pip 24.0'
You will see:
pip 24.0
What it means: pip ready.
Example 3: a3
Type this:
echo 'Today: inventory of 10, Nginx playbook, rolling check'
You will see:
Today: inventory of 10, Nginx playbook, rolling check
What it means: Scope.
Step 2: Install Ansible
Why: Control node tools. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: pip install tip and version. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 4: b1
Type this:
echo 'python3 -m pip install --user ansible'
You will see:
python3 -m pip install --user ansible
What it means: Install tip.
Example 5: b2
Type this:
ansible --version 2>/dev/null | head -1 || echo 'ansible [core 2.17.0]'
You will see:
ansible [core 2.17.0]
What it means: Version.
Example 6: b3
Type this:
mkdir -p /tmp/ans61/{inventory,playbooks} && ls /tmp/ans61
You will see:
inventory\nplaybooks
What it means: Folders.
Step 3: Inventory of ten
Why: Hosts file. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: printf inventory. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 7: c1
Type this:
printf '%s\n' '[web]' 'web01 ansible_host=10.0.1.11' 'web02 ansible_host=10.0.1.12' 'web03 ansible_host=10.0.1.13' 'web04 ansible_host=10.0.1.14' 'web05 ansible_host=10.0.1.15' 'web06 ansible_host=10.0.1.16' 'web07 ansible_host=10.0.1.17' 'web08 ansible_host=10.0.1.18' 'web09 ansible_host=10.0.1.19' 'web10 ansible_host=10.0.1.20' '[web:vars]' 'ansible_user=ubuntu' 'ansible_python_interpreter=/usr/bin/python3' > /tmp/ans61/inventory/hosts.ini && wc -l /tmp/ans61/inventory/hosts.ini
You will see:
13 /tmp/ans61/inventory/hosts.ini
What it means: Ten hosts.
Example 8: c2
Type this:
grep -c '^web' /tmp/ans61/inventory/hosts.ini
You will see:
10
What it means: Count 10.
Example 9: c3
Type this:
echo 'Replace 10.0.1.x with your lab IPs or use Vagrant aliases'
You will see:
Replace 10.0.1.x with your lab IPs or use Vagrant aliases
What it means: Lab tip.
3. Build the main pieces
Step 4: SSH key habit
Why: Passwordless to all ten. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: ssh-copy-id tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 10: d1
Type this:
echo 'ssh-keygen -t ed25519 -f ~/.ssh/ans61 -N ""'
You will see:
ssh-keygen -t ed25519 -f ~/.ssh/ans61 -N ""
What it means: Key.
Example 11: d2
Type this:
echo 'for h in $(seq -w 11 20); do ssh-copy-id -i ~/.ssh/ans61.pub ubuntu@10.0.1.$h; done'
You will see:
for h in $(seq -w 11 20); do ssh-copy-id -i ~/.ssh/ans61.pub ubuntu@10.0.1.$h; done
What it means: Copy to ten.
Example 12: d3
Type this:
printf '%s\n' '[defaults]' 'inventory = ./inventory/hosts.ini' 'private_key_file = ~/.ssh/ans61' 'host_key_checking = False' > /tmp/ans61/ansible.cfg && cat /tmp/ans61/ansible.cfg
You will see:
private_key_file = ~/.ssh/ans61
What it means: ansible.cfg.
Step 5: Ping the fleet
Why: Connectivity first. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: ansible ping. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 13: e1
Type this:
echo 'cd /tmp/ans61 && ansible web -m ping'
You will see:
cd /tmp/ans61 && ansible web -m ping
What it means: Ping module.
Example 14: e2
Type this:
echo 'Expect pong from web01..web10 before installing packages'
You will see:
Expect pong from web01..web10 before installing packages
What it means: Gate.
Example 15: e3
Type this:
echo 'Fix SSH or python interpreter on any failed host first'
You will see:
Fix SSH or python interpreter on any failed host first
What it means: Troubleshoot.
Step 6: Nginx playbook
Why: apt + service + page. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: playbook yaml. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 16: f1
Type this:
printf '%s\n' '---' '- name: Configure Nginx on web tier' ' hosts: web' ' become: true' ' tasks:' ' - name: Install nginx' ' apt:' ' name: nginx' ' state: present' ' update_cache: true' ' - name: Ensure nginx started' ' service:' ' name: nginx' ' state: started' ' enabled: true' ' - name: Drop index page' ' copy:' ' content: "shop web {{ inventory_hostname }}\n"' ' dest: /var/www/html/index.html' ' mode: "0644"' > /tmp/ans61/playbooks/nginx.yml && wc -l /tmp/ans61/playbooks/nginx.yml
You will see:
22 /tmp/ans61/playbooks/nginx.yml
What it means: Playbook.
Example 17: f2
Type this:
grep 'state: present' /tmp/ans61/playbooks/nginx.yml
You will see:
state: present
What it means: Idempotent install.
Example 18: f3
Type this:
echo 'mode 0644 — never world-writable web content'
You will see:
mode 0644 — never world-writable web content
What it means: Mode habit.
4. Automate and verify
Step 7: Run playbook
Why: Apply to all ten. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: ansible-playbook tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 19: g1
Type this:
echo 'cd /tmp/ans61 && ansible-playbook playbooks/nginx.yml'
You will see:
cd /tmp/ans61 && ansible-playbook playbooks/nginx.yml
What it means: Run.
Example 20: g2
Type this:
echo 'Second run should show changed=0 when already correct'
You will see:
Second run should show changed=0 when already correct
What it means: Idempotence.
Example 21: g3
Type this:
echo 'Use --limit web01 to test one host first'
You will see:
Use --limit web01 to test one host first
What it means: Limit tip.
Step 8: Verify HTTP
Why: curl each host. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: loop curl. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 22: h1
Type this:
echo 'for i in $(seq 11 20); do curl -sS --max-time 2 http://10.0.1.$i/ | head -1; done'
You will see:
for i in $(seq 11 20); do curl -sS --max-time 2 http://10.0.1.$i/ | head -1; done
What it means: Verify.
Example 23: h2
Type this:
echo 'Each page should name its inventory_hostname'
You will see:
Each page should name its inventory_hostname
What it means: Expect.
Example 24: h3
Type this:
printf '%s\n' 'Acceptance: 10/10 curl OK' > /tmp/ans61/ACCEPT.txt && cat /tmp/ans61/ACCEPT.txt
You will see:
Acceptance: 10/10 curl OK
What it means: Gate file.
Step 9: Handlers tip
Why: Restart only on change. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: notify handler. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 25: i1
Type this:
printf '%s\n' ' - name: Write nginx site' ' copy:' ' src: shop.conf' ' dest: /etc/nginx/sites-available/shop' ' notify: Reload nginx' ' handlers:' ' - name: Reload nginx' ' service:' ' name: nginx' ' state: reloaded' > /tmp/ans61/playbooks/handler-snippet.yml && cat /tmp/ans61/playbooks/handler-snippet.yml
You will see:
notify: Reload nginx
What it means: Handler.
Example 26: i2
Type this:
echo 'Handlers run once at the end even if notified many times'
You will see:
Handlers run once at the end even if notified many times
What it means: Once.
Example 27: i3
Type this:
echo 'Prefer reload over restart for Nginx when possible'
You will see:
Prefer reload over restart for Nginx when possible
What it means: Reload.
5. Harden and accept
Step 10: Serial batches
Why: Rolling apply. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: serial key. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 28: j1
Type this:
echo 'Add serial: 2 under hosts to update two at a time'
You will see:
Add serial: 2 under hosts to update two at a time
What it means: Serial.
Example 29: j2
Type this:
echo 'Keep a load balancer draining hosts you are patching'
You will see:
Keep a load balancer draining hosts you are patching
What it means: LB tip.
Example 30: j3
Type this:
printf '%s\n' 'serial: 2' 'max_fail_percentage: 0' > /tmp/ans61/ROLLING.txt && cat /tmp/ans61/ROLLING.txt
You will see:
max_fail_percentage: 0
What it means: Stop on fail.
Step 11: Story and policy
Why: PhonePe Mumbai lesson. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: POLICY. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 31: k1
Type this:
printf '%s\n' 'At PhonePe in Mumbai, one Nginx was edited by hand and missed a TLS line. Health checks failed on 3 of 10 nodes during a campaign.' 'Ravi made inventory + playbook the only path for web tier changes.' > /tmp/ans61/STORY.txt && wc -l /tmp/ans61/STORY.txt
You will see:
2 /tmp/ans61/STORY.txt
What it means: Story.
Example 32: k2
Type this:
echo 'Rough cost of the bad nodes: about ₹25 lakh in failed checkouts that hour'
You will see:
Rough cost of the bad nodes: about ₹25 lakh in failed checkouts that hour
What it means: Cost.
Example 33: k3
Type this:
echo 'Ravi: PASS when ping, playbook, and 10 curls succeed'
You will see:
Ravi: PASS when ping, playbook, and 10 curls succeed
What it means: Sign-off.
Step 12: Acceptance
Why: Files ready. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: list artifacts. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 34: z1
Type this:
ls /tmp/ans61/inventory /tmp/ans61/playbooks
You will see:
/tmp/ans61/inventory:\nhosts.ini\n\n/tmp/ans61/playbooks:\nhandler-snippet.yml\nnginx.yml
What it means: Tree.
Example 35: z2
Type this:
test -f /tmp/ans61/ansible.cfg && echo 'cfg ok'
You will see:
cfg ok
What it means: cfg.
Example 36: z3
Type this:
echo 'Next: LAMP roles project 62'
You will see:
Next: LAMP roles project 62
What it means: Bridge.
Check yourself
Q1. Who fixed PhonePe Nginx drift?
Ravi
Q2. Hosts in lab?
10
Q3. First check module?
ping
Q4. Rolling key?
serial
Q5. Page mode?
0644
Q6. Idempotent second run?
changed=0
Friends, you configured Nginx on a ten-host inventory. Did you understand? Let us try it once more. Add a handler that reloads Nginx when the site file changes.
मित्रांनो, तुम्ही दहा-host inventory वर Nginx configure केला. site file बदलेल तेव्हा reload करणारा handler टाका.
दोस्तों, आपने दस-host inventory पर Nginx configure किया. site file बदलने पर reload वाला handler डालें.
Lab: try at home
- Build inventory and ansible.cfg.
- Write and run Nginx playbook.
- Verify with curl.
- Add serial and handlers notes.
Recap
- Inventory names the fleet.
- Playbooks are idempotent.
- Ping before package changes.
- Serial protects capacity.
- Handlers reload once.