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.
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.
Nginx servers
Configure Nginx packages and virtual-host templates. Use a handler to reload Nginx when its configuration changes.
Application servers
Apply application packages and service configuration using variables and templates appropriate to the application you choose.
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.
Check your Ansible installation
ansible --version
Organise the project files
Create a project directory with inventories, group variables, reusable roles and a top-level playbook.
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.
Separate environments and server groups
Start with a staging inventory. Production uses the same group structure but different host addresses and environment-specific values.
Inspect the inventory
ansible-inventory -i inventory/staging.ini --graph
ansible -i inventory/staging.ini all -m ansible.builtin.ping
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.
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.
web_server_name. Ansible renders the template with the values defined for the relevant host or group.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.