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: 8080
4. 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
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?