Templates, inventories and variables: one source of truth, or none
An inventory says which devices, variables say what, and precedence decides who wins.

Essential answer. Three inputs turn a list of devices into a configuration. An inventory says which devices exist and how they are grouped; variables say what should be true on them; a template renders those variables into a file. The contention is precedence: when the same variable is defined twice, the rules pick one, and which one depends on where you put it. Choose that order on purpose, or the automation chooses it for you.
The mental model: three inputs and three states
The inputs are data in, text out. Inventory is the list of hosts and their groups; variables are the values attached to a host or a group; a template is a text file whose placeholders are replaced when it is rendered. The engine merges the variables onto each host, renders the template with those values, and hands the result to the device.
Hold three states apart. The declared intent is the template and variables you wrote; the rendered configuration is what the render produces; the device state is what the device runs. A render that succeeds is only the middle one.
inventory ---> which hosts, which groups
variables ---> values per host, per group ---> render ---> rendered configuration
template ---> text with placeholders |
v
declared intent = the template and variables you wrote
rendered configuration = what rendering produced
device state = what the device runs
Terms you need
An inventory is the list or group of lists of managed nodes, composed from one or more inventory sources; even with no groups defined, two default groups exist, all and ungrouped. Variables live in the inventory source or in separate group_vars/ and host_vars/ files. A variable is a named value; precedence decides which definition survives when a name is defined twice. A template is a text file, and rendering replaces its expressions and evaluates its tags; the values it receives are its context.
Where a value lives decides what it means
Precedence is not a corner case; it is the design. Variables are flattened to the host before a play runs, and the order for inventory entities, from lowest to highest, is the all group, a parent group, a child group, the host. A child group overrides its parent. Groups at the same level merge in alphabetical order, the last one loaded overwriting the earlier ones, and ansible_group_priority overrides that order for one level only.
The order of the sources is part of the decision. Pass the staging source and then the production source, and a variable defined in both resolves to production’s value; reverse the two and it resolves to staging’s. In a directory the sources load in filename order, which numeric prefixes are used to pin. Variables sourced relative to the playbook override those sourced relative to the inventory.
The practical rule: a value should live at the widest level where it is genuinely the same everywhere, and be overridden only deliberately. Group variables remove duplication for what a whole group shares; a host variable carries what is unique to one device.
Rendering: from data to configuration
A template is a text file, and rendering replaces its expressions with values and runs its tags. The order is deliberate: check the data, then render, never the reverse. A variable that was never defined does not stop the render by default - a missing value can print as nothing, and a blank line looks much like a deliberate one. Template inheritance eases the common case: a base template holds the shared skeleton and defines blocks, and each device’s template fills only its differences.
Sensible patterns, and what not to template
- Keep secrets out of plain inventory and out of what is rendered: passwords belong in a vault, and a vaulted value is decrypted only with the password that encrypted it.
- Put the data in one file per device or per role, and template a stable, small subset. A template that reproduces the whole configuration owns the parts you did not mean to manage.
- Read the configuration back from the device; a successful render proves nothing about it.
Limits and the common errors
The first error is to treat precedence as an implementation detail: the same name in two files produces a device state nobody chose, and the automation reports success while doing it. The second is to read a default as a value: a default in a data model is what the system behaves as if the node had when it is absent, not a value written into the file. The third is to confuse the three states: “the automation ran” is the declared intent, “the file rendered correctly” is the rendered configuration, and neither is evidence of the device state.
Level and prerequisites
L5 - architecting the workflow. The sheet assumes the L5 sibling on Ansible and the L1-L3 vocabulary on device configuration; it does not repeat the imperative/declarative distinction or the model of a play. Its neighbours own the rest: the Ansible sheet (the tool, idempotency), the drift sheet (declared intent versus device state), the credential sheet (secrets), the validation sheet (checking collected data) and the versioning sheet (file history). The new skill is holding inventory, variables and templates as three ordered inputs, and reading precedence as a decision rather than an accident.
Where to go next
- Networking - the area this sheet belongs to.
References
- Ansible Community Documentation - How to build your inventory (inventory sources, default groups, host and group variables, precedence, vaults).
- Ansible Community Documentation - Build Your Inventory, Network Getting Started (the inventory as the source of truth for a network).
- Ansible Community Documentation - Ansible playbooks (plays, tasks, the template module, desired state and idempotency, check mode).
- Jinja Documentation (3.1.x) - Template Designer Documentation (delimiters, variables, undefined values, filters, template inheritance).
- RFC 7950 - The YANG 1.1 Data Modeling Language (2016): the leaf’s default value and when a default is “in use”.