Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Write an Ansible Inventory and Playbook That Installs nginx, Deploys a Welcome Page with a Handler, and Runs Twice Safely

Beginner45 minAnsible (pip or package) · One Linux server you own (EC2 Amazon Linux 2023 or a local VM) · SSH key access

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.ini and ansible all -m ping returning pong.
  • 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

  1. 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.pem
    
    mkdir ansible-nginx && cd ansible-nginx
    
  2. Test the connection:

    ansible -i inventory.ini web -m ping
    

    What you should see: web1 | SUCCESS => { "ping": "pong" }

Part 2: Playbook, template and handler

  1. Create the page template:

    templates/index.html.j2

    <h1>Welcome to {{ site_name }}</h1>
    <p>Served by {{ inventory_hostname }} - managed by Ansible</p>
    
    mkdir templates
    
  2. Create 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: restarted
    

    mode: "0644" lets nginx read the page and only root write it.

  3. Dry run first: check mode with a diff of what would change:

    ansible-playbook -i inventory.ini site.yml --check --diff
    

    What you should see: tasks reported as changed and a diff of the new page, but nothing is changed on the server yet.

Part 3: Run it twice

  1. Run it for real:

    ansible-playbook -i inventory.ini site.yml
    

    What you should see: a recap like web1 : ok=5 changed=3 and the handler Restart nginx ran once.

  2. Open http://SERVER-IP/ in a browser (or curl it).

    What you should see: Welcome to Badge Portal.

  3. Run the same command again:

    ansible-playbook -i inventory.ini site.yml
    

    What you should see: changed=0 and no handler. That is idempotence: same input, same end state, nothing touched.

  4. Change site_name to Badge Portal v2 and run again.

    What you should see: only the template task is changed, and the handler restarts nginx once.

Clean up

  1. 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

MistakeWhat happensFix
UNREACHABLE on pingAnsible cannot SSHCheck IP, user, key path, and that port 22 allows your IP
Forgetting become: truePermission denied installing packagesAdd become: true at play level
Using shell: yum install nginxAlways reports changedUse the package module
Restarting nginx in a normal taskRestarts on every runUse 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.