Interview Prep/Ansible/Roles, Collections and Project Structure

You have a 600-line playbook that sets up web, db and monitoring. How do you restructure it?

BeginnerScenarioRoles, Collections and Project Structure
Answer

Situation read → A monolith that is hard to reuse and test.

What I'd do →

  1. Create roles: roles/common, roles/nginx, roles/postgres, roles/node_exporter.
  2. Make site.yml import small playbooks, such as web.yml and db.yml.
  3. Move values to defaults/main.yml and inventory.
- hosts: web
  roles: [common, nginx, node_exporter]

Why → Roles give reuse, a standard layout and a natural unit for Molecule tests.

Interview tip → Keep playbooks as thin mappings of hosts to roles.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Project Structure