Lesson 6 of 9 · 25m
Role Architecture: Reuse Without Losing Control
Design roles with narrow contracts, sane defaults, explicit dependencies, and testable interfaces instead of piles of convenient task files.
The first Ansible role most teams write is a junk drawer. It installs packages, edits twelve unrelated files, creates users, restarts services, and encodes one engineer's memory of how production happened to look that month. A role should instead be a small operational component with an input contract, predictable ownership, and behavior you can test without understanding the entire estate.
A role owns a component, not a host
Strong role boundaries sound like this:
nginx install and configure the nginx service
app_user create the application runtime account and directories
app_deploy install one application release and activate it
firewall enforce host firewall policyWeak boundaries sound like common, base, setup, and misc. Those names usually mean ownership has not been decided. A host can consume several roles, but each role should be able to explain what it owns in one sentence.
Defaults are the public interface
Put safe, overridable inputs in defaults/main.yml:
nginx_worker_processes: auto
nginx_worker_connections: 1024
nginx_tls_protocols:
- TLSv1.2
- TLSv1.3
nginx_config_validate: trueUse vars/main.yml for internal implementation constants, not operator inputs. Defaults exist to be overridden; vars exist because the role needs its own stable machinery. When teams reverse this, every environment ends up editing role internals.
Validate inputs early
A role that accepts nonsense should fail before mutating the host:
- name: Validate nginx role inputs
ansible.builtin.assert:
that:
- nginx_worker_connections is number
- nginx_worker_connections >= 128
- nginx_tls_protocols | length > 0
fail_msg: nginx role inputs are invalid; check worker connections and TLS protocolsGood assertions are part of the contract. They catch inventory mistakes before creating a partial configuration state across two hundred machines.
Dependencies are not a shortcut to composition
meta/main.yml dependencies can pull other roles in automatically:
dependencies:
- role: app_userThis is useful for hard prerequisites but dangerous when used as hidden orchestration. If reading the top-level playbook no longer tells you what will run, the dependency graph has become too clever. Prefer explicit composition in playbooks unless the dependency is genuinely inseparable.
Templates need ownership and provenance
A template is a promise about file ownership:
# Managed by Ansible role nginx
worker_processes {{ nginx_worker_processes }};
events {
worker_connections {{ nginx_worker_connections }};
}Do not template entire vendor configuration files unless you have a review process for upstream changes. Use drop-in directories when supported. They reduce merge pain and make rollback narrower.
Collections and versioning are operational controls
Pin external content in requirements.yml:
collections:
- name: ansible.posix
version: 1.5.4
- name: community.general
version: 8.6.0Unversioned dependencies turn upstream maintenance into unplanned production change. Review updates deliberately. Convenience is not worth a surprise behavior change in a sysctl or firewall module.
Field notes
- If a role cannot be applied twice safely, fix it before reusing it.
- Do not put inventory-specific secrets or hostnames into reusable role defaults.
- A role with twenty unrelated tags is usually several roles wearing one trench coat.
- Avoid
include_taskschains so deep that no one can predict execution order during an incident. - Publish role interfaces through examples, not only variable names; users copy what they can see.
Was this lesson helpful?