What are Ansible roles and how do they organize automation?
Learn what Ansible roles are, their standard directory structure, defaults vs vars, and how they make automation reusable and shareable via Galaxy.
Expected Interview Answer
Ansible roles are a standardized way to package related tasks, variables, files, templates, and handlers into a reusable directory structure, so complex automation can be organized, shared, and applied to hosts by simply referencing the role name.
A role follows a fixed layout — tasks/, handlers/, defaults/, vars/, files/, templates/, meta/ — and Ansible auto-loads main.yml from each directory. This separation of concerns lets you build a self-contained 'nginx' or 'postgres' role once and reuse it across playbooks and projects, override behavior through defaults and vars, declare dependencies in meta/, and share roles via Ansible Galaxy. Roles turn sprawling monolithic playbooks into composable, testable units.
- Reusable across playbooks and projects
- Standard directory structure everyone recognizes
- Separation of concerns (tasks, vars, templates, handlers)
- Shareable via Ansible Galaxy
- Support dependencies and clean variable precedence via defaults/vars
AI Mentor Explanation
A role is like a fully-equipped kit bag for a specialist position: the wicketkeeper's bag holds gloves, pads, inners, and a care checklist, all in known compartments, ready to hand to whoever keeps wicket. Instead of assembling gear match by match, you grab the bag. An Ansible role bundles tasks, variables, files, and handlers in a fixed layout so any playbook can pick up 'the nginx bag' and apply it instantly.
Step-by-Step Explanation
Step 1
Create the role skeleton
Run ansible-galaxy init <role> to generate the standard directories: tasks, handlers, defaults, vars, files, templates, meta.
Step 2
Write tasks/main.yml
Put the core steps here; Ansible auto-loads this file when the role is applied to a host.
Step 3
Add defaults and vars
Expose overridable settings in defaults/main.yml (lowest precedence) and fixed values in vars/main.yml (higher precedence).
Step 4
Include files, templates, handlers
Static files in files/, Jinja2 templates in templates/, and service-restart logic in handlers/main.yml triggered by notify.
Step 5
Declare dependencies and apply
List required roles in meta/main.yml, then reference the role in a playbook via 'roles:' or include_role/import_role.
What Interviewer Expects
- Knowledge of the standard role directory structure
- Understanding that main.yml is auto-loaded per directory
- Difference between defaults/ and vars/ precedence
- How roles are applied (roles: vs include_role/import_role)
- Awareness of Ansible Galaxy for sharing roles
Common Mistakes
- Putting overridable settings in vars/ instead of defaults/
- Confusing include_role (dynamic) with import_role (static)
- Hardcoding values into tasks instead of parameterizing via defaults
- Ignoring meta/main.yml dependencies and duplicating setup
- Forgetting that files/ and templates/ are resolved relative to the role
Best Answer (HR Friendly)
“Ansible roles are a tidy way to package all the pieces needed to set something up — like a web server — into one reusable folder. Instead of one giant, messy script, you build a self-contained 'role' once and reuse it anywhere, which keeps automation organized and easy to share.”
Code Example
roles/
nginx/
tasks/main.yml # core steps (auto-loaded)
handlers/main.yml # e.g. restart nginx
defaults/main.yml # overridable variables
vars/main.yml # fixed variables
files/ # static files to copy
templates/ # Jinja2 templates
meta/main.yml # role dependencies + metadata- name: Configure web tier
hosts: web
become: true
roles:
- role: nginx
vars:
nginx_worker_processes: 4
# Or apply dynamically at runtime
- hosts: web
tasks:
- ansible.builtin.include_role:
name: nginxFollow-up Questions
- What is the difference between defaults/ and vars/ in a role?
- How do import_role and include_role differ?
- How do role dependencies in meta/main.yml work?
- What is Ansible Galaxy and how do you pull roles from it?
- How does variable precedence work between roles, playbooks, and inventory?
MCQ Practice
1. Which file does Ansible auto-load as a role's core task list?
Ansible automatically loads main.yml from each standard role directory, so tasks/main.yml holds the role's core steps.
2. Where should you put variables that users are expected to override?
defaults/main.yml has the lowest precedence, making it the right place for sensible, easily-overridable default values.
3. Which directory holds Jinja2 templates rendered by a role?
templates/ stores Jinja2 templates; files/ holds static files copied verbatim without rendering.
Flash Cards
What is an Ansible role? — A reusable, standardized directory bundling tasks, vars, files, templates, and handlers to organize and share automation.
What are the main role directories? — tasks, handlers, defaults, vars, files, templates, and meta — each with an auto-loaded main.yml.
defaults/ vs vars/? — defaults/ holds low-precedence overridable values; vars/ holds higher-precedence fixed values.
How do you create a role skeleton? — Run ansible-galaxy init <role-name> to generate the standard directory structure.