Interview › Infrastructure as Code (Terraform, Ansible)
What are Ansible roles, and what is the standard directory structure?
Infrastructure as Code (Terraform, Ansible) · Intermediate level
Answer
Ansible roles package tasks, handlers, templates, files, defaults, variables, and metadata into reusable units. The standard structure includes tasks/main.yml, handlers/main.yml, templates/, files/, defaults/main.yml, vars/main.yml, and meta/main.yml.
Technical explanation
defaults/main.yml is for low-precedence defaults callers can override.
vars/main.yml is higher precedence and should be used sparingly.
Roles become easier to test and version when each role has a clear responsibility.
Prefer idempotent modules over shell so repeated runs are safe and change reporting is meaningful.
Separate reusable role logic from inventory-specific variables so the same automation works across environments.
Run lint, syntax checks, check mode where useful, and staged rollouts before production-wide changes.
Hands-on example
1. Package reusable Ansible content for: What are Ansible roles, and what is the standard directory structure?
2. Create a role structure:
roles/web/
defaults/main.yml
tasks/main.yml
handlers/main.yml
templates/nginx.conf.j2
files/
meta/main.yml
3. Call the role from a playbook:
- name: Configure web tier
hosts: web
roles:
- role: web
vars:
web_listen_port: 80804. If distributing modules/plugins/roles together, package them as a collection and pin it in requirements.yml.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- Why do handlers run once at the end rather than immediately?
- What is the order of variable precedence in Ansible at a high level?
- What is a handler, and how is it triggered with notify?
- What are group_vars and host_vars?
- What are Ansible modules, and why prefer them over shell commands?
- What is Ansible Vault, and how do you protect secrets with it?
- What is the creates argument on a command task, and how does it add idempotency?
- What is the difference between a variable and a fact?