variable precedence
The fixed order Ansible uses to decide which value wins when the same variable is set in several places.
In more detail
The general rule is that the more specific source wins: a host beats its group, a child group beats its parent, and every group beats all. Role defaults are near the bottom and extra variables given on the command line are at the top. Merging happens one variable at a time, so a host keeps values from its groups that its own file does not override. Rather than reason from the files, ask ansible-inventory --host what a host resolves to.
Related terms
- Ansible variable — A named value, such as http_port or site_name, that tasks and templates refer to instead of writing the value in directly.
- group_vars — A directory whose files hold variables for inventory groups, one file or folder per group, named exactly after the group.
- host_vars — A directory whose files hold variables for single hosts, each file named exactly as the host is spelled in the inventory.
- extra variables — Variables passed on the command line when a playbook starts, which override values from every other source.
- role defaults — The variables in a role's defaults/main.yml: safe starting values that callers are expected to override.
Taught in
- Host Groups And Host Variables — Ansible Configuration Management at Scale
- Connections And Privilege Escalation — Ansible Configuration Management at Scale
- Variables And Precedence — Ansible Configuration Management at Scale
- Inventory Variables: Ranges, group_vars And host_vars — RHCE in Ansible (EX294): Automate Red Hat Systems
- Roles: The Directory Ansible Expects, Defaults And Handlers — RHCE in Ansible (EX294): Automate Red Hat Systems