What are roles and why use them?
⚡
Quick Answer
Roles bundle tasks, handlers, templates, defaults, and vars into a reusable, shareable unit.
Detailed Answer
Once a playbook grows, split it into roles (ansible-galaxy role init). A role has a fixed layout (tasks/, handlers/, templates/, defaults/, vars/) so structure is predictable and reusable across projects and via Ansible Galaxy. Playbooks then become a thin list of roles applied to host groups.
Code Example
- hosts: web become: true roles: [nginx, app, monitoring]
💡
Interview Tip
Know the standard role directory layout.
ansiblerolesreuse