LEARNING PATH  ›  Reusable Configuration Management
LEVEL 02 / INTERMEDIATE~2–3 HOURS
● BUILD REUSABLE INFRASTRUCTURE CONFIGURATION

Manage multiple tiers with reusable roles.

Organise web and application servers into separate inventory groups. Build reusable Ansible roles, manage environment-specific variables, render Jinja2 templates and use handlers to keep configurations consistent.

ANSIBLE
group_vars/
web + app tiers
reusable roles
staging / prod
02Infrastructure tiers
03Reusable roles
02Environment inventories
02 / ARCHITECTURE

How the project works

One control node manages multiple server groups. A common baseline role configures every host, while dedicated roles configure web and application tiers independently.

01 · INVENTORYStaging or production
02 · VARIABLESGroup-specific settings
03 · ROLESBaseline, web, app
04 · VERIFYServices and health
WEB TIER

Nginx servers

Configure Nginx packages and virtual-host templates. Use a handler to reload Nginx when its configuration changes.

APPLICATION TIER

Application servers

Apply application packages and service configuration using variables and templates appropriate to the application you choose.

Scope note: This lab demonstrates the application role with a configurable example service placeholder. It does not assume a specific application runtime or deploy a real application binary.
03 / PREREQUISITES

Prepare the environment

Use four existing Ubuntu/Debian servers for the full lab: two web servers and two application servers. For a smaller practice environment, one server per tier is sufficient.

Control node

Ansible installed, terminal access, SSH client and permission to read the project files.

Managed nodes

Ubuntu/Debian hosts reachable over SSH, Python 3 available and an authorised account with sudo access.

Important: This project configures existing hosts. It does not create EC2 instances, networks or virtual machines. Replace all example IP addresses and hostnames before execution.

Check your Ansible installation

Control node terminal
ansible --version
04 / PROJECT STRUCTURE

Organise the project files

Create a project directory with inventories, group variables, reusable roles and a top-level playbook.

ansible-intermediate/
ansible-intermediate/
├── ansible.cfg
├── inventory/
│   ├── staging.ini
│   └── production.ini
├── group_vars/
│   ├── all.yml
│   ├── webservers.yml
│   └── appservers.yml
├── roles/
│   ├── baseline/
│   │   ├── defaults/main.yml
│   │   └── tasks/main.yml
│   ├── nginx/
│   │   ├── defaults/main.yml
│   │   ├── tasks/main.yml
│   │   ├── handlers/main.yml
│   │   └── templates/site.conf.j2
│   └── application/
│       ├── defaults/main.yml
│       ├── tasks/main.yml
│       ├── handlers/main.yml
│       └── templates/app.conf.j2
├── site.yml
└── requirements.yml

Inventory

Defines which machines belong to each environment and tier.

Group variables

Stores shared values for all hosts or a particular server group.

Roles

Packages related tasks, defaults, handlers and templates for reuse.

05 / INVENTORY & VARIABLES

Separate environments and server groups

Start with a staging inventory. Production uses the same group structure but different host addresses and environment-specific values.

inventory/staging.ini
Host-key checking stays enabled. Connect to each host and verify its SSH fingerprint before using it in the inventory. Do not disable host-key checking as a shortcut.

Inspect the inventory

ansible-inventory -i inventory/staging.ini --graph
ansible -i inventory/staging.ini all -m ansible.builtin.ping
06 / REUSABLE ROLES

Build reusable configuration roles

A role groups related Ansible content into a predictable structure. The examples below show a common baseline role and an Nginx role for the web tier.

roles/baseline/tasks/main.yml

Baseline role

Applies shared package and configuration tasks to all managed hosts.

Nginx role

Installs Nginx, deploys a virtual-host template and reloads the service when needed.

Application role

Use the same role structure for your chosen application package, template and service.

Handlers

Run only when notified by a task that reports a change, avoiding unnecessary service restarts.

Jinja2: The template uses variables such as web_server_name. Ansible renders the template with the values defined for the relevant host or group.
07 / EXECUTE & VERIFY

Validate before applying configuration

1. Check playbook syntax

ansible-playbook -i inventory/staging.ini site.yml --syntax-check

2. Preview changes

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

Check mode is a simulation; not every module supports it fully. Review the output before applying changes.

3. Apply configuration

ansible-playbook -i inventory/staging.ini site.yml

4. Verify the web tier

ansible -i inventory/staging.ini webservers -b -m ansible.builtin.command -a "systemctl is-active nginx"

Verify the application tier using the service name and health endpoint for the application you selected.

Production safety: Do not point the production inventory at real systems until you have reviewed variables, secrets, access permissions and the staging results. Use Ansible Vault for sensitive values rather than storing passwords in plain text.

Completion checklist

08 / KNOWLEDGE CHECK

Test your understanding

1. What is the main purpose of an Ansible role?
2. What triggers an Ansible handler?
3. What is a Jinja2 template used for?
4. What does --check do?