Skip to content

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 vendor

The 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: ansible

molecule 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 --diff

ansible-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 — nginx configures 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.yml collections, requirements.txt core, gitleaks in CI.
  • Retire aggressively — a playbooks/oneoff/ folder of 2023 migrations is a trap someone will -l all one 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.yml covers collections and standalone roles: — pin both kinds or pin neither; half-pinning is the version of supply-chain roulette that actually happens.
  • import_playbook in site.yml is static — it's what makes --list-tasks and tag filtering accurate; include_playbook exists and hides all of that from you.
  • The healthy codebase smells like nothing: predictable paths, green second runs, and a --check --diff CI comment that says "no changes" when nothing changed.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.