Lesson 13 of 13 · 24m
The 200-Playbook Codebase: Layout, Testing, and Conventions
The layout, tagging vocabulary, Molecule and lint in CI, and the conventions that keep a shared Ansible repo navigable.
Every team that adopts Ansible follows the same arc: one playbook, then ten, then a shared repo nobody can navigate, then a quiet period where only one person is allowed to run anything. The repos that survive past playbook fifty all converged on the same set of ideas — a predictable layout, entry points that mean something, roles that are tested instead of trusted, and conventions enforced by lint instead of memory. This is that repo.
The layout that scales
ansible/
ansible.cfg # forks, roles_path, vault_identity_list — in the repo
requirements.yml # pinned collections and external roles
requirements.txt # pinned ansible-core
site.yml # THE entry point — converges an environment
inventories/
production/ # hosts.yml, group_vars/, host_vars/
staging/ # same shape, different truth
playbooks/ # web.yml, db.yml, maintenance/ — one concern each
roles/ # common/, nginx/, postgres/ — project-owned
collections/ # vendored, if you vendorThe organizing rule is one sentence: ansible-playbook -i inventories/production site.yml converges the whole environment. Everything else is a subset you reach through -l and --tags, and environments differ by inventory, never by forked playbooks. The repo where site-prod.yml and site-staging.yml diverged is the repo where "works in staging" stops meaning anything.
ansible.cfg lives in the repo — team config beats "ask Dave what his forks setting is." One gotcha: Ansible refuses to load a config from a world-writable directory, so CI checkouts at permissive modes can silently ignore it; the fix is checkout perms, not confusion.
Tags: small vocabulary, import-level application
# site.yml
- import_playbook: playbooks/common.yml
tags: [base]
- import_playbook: playbooks/web.yml
tags: [web]
- import_playbook: playbooks/db.yml
tags: [db]Tag at the import_playbook level — it stamps every task inside — not hand-applied on forty tasks. A small agreed vocabulary (base, web, db, deploy, certs) beats a bespoke tag per task nobody can spell. ansible-playbook site.yml --list-tasks --tags deploy should tell an operator exactly what a deploy touches; when it lists 300 tasks, your tagging is failing the person at 3 a.m.
Test roles, not playbooks
Playbooks orchestrate; roles hold logic — so roles are what you test. Molecule spins a container or VM, runs the role, and lets you assert:
# roles/postgres/molecule/default/molecule.yml
driver:
name: docker
platforms:
- name: postgres-mol
image: geerlingguy/docker-debian12-ansible:latest
provisioner:
name: ansible
verifier:
name: ansiblemolecule test runs the lifecycle — create → converge → idempotence → verify → destroy — and the idempotence step is the one that earns its keep: it runs your role a second time and fails on any changed, which is the honesty contract enforced in CI instead of in production. The verify step is an Ansible playbook asserting outcomes (postgres listening, config line present), and the pattern generalizes: molecule/default/ per role, scenario dirs beside it for molecule/cluster/ when the role has HA behavior.
ansible-test is the collection-side sibling — ansible-test sanity, units, integration — and it only runs inside a collection layout. If your content is project roles, Molecule is the tool; if you're building a collection, ansible-test is.
Lint and dry-run in CI, not by memory
ansible-lint # rule-driven: fqcn, name[casing], risky-*
ansible-playbook -i inventories/staging site.yml --check --diffansible-lint enforces the conventions — FQCN names, named tasks, no with_items, risky shell idioms — and its production profile is the bar a shared repo should clear. The second line is the killer feature: --check --diff against staging turns every PR into a reviewable delta of what changes, not a reading of YAML. Combined with gitleaks (vault covers secrets; gitleaks catches the day someone forgot), this is the whole CI story for an Ansible repo.
The conventions that compound
- FQCN everywhere, every task named — lint enforces both; the run log is the runbook.
- One role, one concern —
nginxconfigures nginx; TLS certs are their own role; a role that needs another role's code gets restructured, not chained. - No secrets outside vault; pin everything —
requirements.ymlcollections,requirements.txtcore,gitleaksin CI. - Retire aggressively — a
playbooks/oneoff/folder of 2023 migrations is a trap someone will-l allone day. Delete or archive; git remembers either way. - New hire test — can they predict where a change lives from the hostname it affects? If not, the layout's failing them, not the other way around.
Field notes
requirements.ymlcovers collections and standaloneroles:— pin both kinds or pin neither; half-pinning is the version of supply-chain roulette that actually happens.import_playbookinsite.ymlis static — it's what makes--list-tasksand tag filtering accurate;include_playbookexists and hides all of that from you.- The healthy codebase smells like nothing: predictable paths, green second runs, and a
--check --diffCI comment that says "no changes" when nothing changed.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.