Labs · DevOps
Lab: Write an Ansible Inventory and Playbook That Installs nginx, Deploys a Welcome Page with a Handler, and Runs Twice Safely
Course: DevOps · Chapter 11: Ansible Introduction · Lesson: 11.12 Lab: playbook installs nginx welcome
Chapter 11 explains inventory, modules, idempotence and handlers; this lab uses all four on one server.
Chala mitrano! Ek server la SSH karun commands chalavne sopa aahe. Pannas server la? Ansible. Aani doan da chalavla tari kahi bighadat nahi.
Suppose we are…
Suppose we are on an Infosys operations team that must set up nginx with the same welcome page on many servers. Logging in to each one and typing commands takes hours, and the results drift apart. An Ansible playbook describes the end state once, and running it again only changes what is different.
In this lab we target one server you own, install nginx with a playbook, deploy a page from a template, restart nginx only when the page changes, and prove that a second run changes nothing.
Goal of this lab
By the end you will have:
- An
inventory.iniandansible all -m pingreturningpong. - A playbook that installs nginx and deploys a templated page.
- A second run with
changed=0.
What you need
- Ansible on your laptop or WSL (
python3 -m pip install --user ansible). - One Linux server you own with SSH key login, for example a free-tier EC2 Amazon Linux 2023 instance with port 22 open only to your IP and port 80 open.
- 45 minutes.
Cost
If you launched an EC2 instance for this lab, terminate it at the end.
Part 1: Inventory and ping
Create a project folder and an inventory (replace the IP and key path):
inventory.ini
[web] web1 ansible_host=203.0.113.10 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/lab-key.pemmkdir ansible-nginx && cd ansible-nginxTest the connection:
ansible -i inventory.ini web -m pingWhat you should see:
web1 | SUCCESS => { "ping": "pong" }
Part 2: Playbook, template and handler
Create the page template:
templates/index.html.j2
<h1>Welcome to {{ site_name }}</h1> <p>Served by {{ inventory_hostname }} - managed by Ansible</p>mkdir templatesCreate the playbook:
site.yml
- name: Web servers get nginx and a welcome page hosts: web become: true vars: site_name: Badge Portal tasks: - name: Install nginx ansible.builtin.package: name: nginx state: present - name: Deploy the welcome page ansible.builtin.template: src: templates/index.html.j2 dest: /usr/share/nginx/html/index.html mode: "0644" notify: Restart nginx - name: nginx is running and starts at boot ansible.builtin.service: name: nginx state: started enabled: true handlers: - name: Restart nginx ansible.builtin.service: name: nginx state: restartedmode: "0644"lets nginx read the page and only root write it.Dry run first: check mode with a diff of what would change:
ansible-playbook -i inventory.ini site.yml --check --diffWhat you should see: tasks reported as
changedand a diff of the new page, but nothing is changed on the server yet.
Part 3: Run it twice
Run it for real:
ansible-playbook -i inventory.ini site.ymlWhat you should see: a recap like
web1 : ok=5 changed=3and the handlerRestart nginxran once.Open
http://SERVER-IP/in a browser (or curl it).What you should see: Welcome to Badge Portal.
Run the same command again:
ansible-playbook -i inventory.ini site.ymlWhat you should see:
changed=0and no handler. That is idempotence: same input, same end state, nothing touched.Change
site_nametoBadge Portal v2and run again.What you should see: only the template task is
changed, and the handler restarts nginx once.
Clean up
Terminate the lab EC2 instance in the console (or with the CLI) if you created it for this lab.
What you should see: instance state
terminated.
Ravindra Bagale's Tip
If a task shows changed on every run, it is not idempotent. Usually it is a shell or command task that should be a proper module.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
UNREACHABLE on ping | Ansible cannot SSH | Check IP, user, key path, and that port 22 allows your IP |
Forgetting become: true | Permission denied installing packages | Add become: true at play level |
Using shell: yum install nginx | Always reports changed | Use the package module |
| Restarting nginx in a normal task | Restarts on every run | Use notify and a handler |
Self-check checklist
0 of 5 done
Try-at-home challenge
Move the tasks into a role (ansible-galaxy init roles/web), call it from site.yml, and add a second server to the [web] group to see both configured in one run.
Samajla ka? Describe the end state once; run it as many times as you want. Aata pudhe jaauya monitoring.