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.

Preparing for an interview?

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

← All Infrastructure as Code (Terraform, Ansible) questions