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 →
- Create roles:
roles/common,roles/nginx,roles/postgres,roles/node_exporter. - Make
site.ymlimport small playbooks, such asweb.ymlanddb.yml. - Move values to
defaults/main.ymland 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.
More on Roles, Collections and Project Structure
Where do you put a variable users of your role are expected to override?BeginnerTwo roles need to run in a fixed order, such as `common` before `nginx`.BeginnerA role is used twice with different parameters in one play, but only runs once.IntermediateYou want to share roles across several repos and teams.IntermediateHow do you pin Ansible itself so a CI run today behaves like one last month?IntermediateA role installs a package and writes a config, but must support Ubuntu and Rocky Linux.Intermediate