Variable Precedence
The same variable can be set in a dozen places. When two set it for the same host, Ansible picks the one with the higher precedence. The official list has 22 levels; you don't need to memorise it, but you do need its shape.
The list, weakest first
| # | Source |
|---|---|
| 1 | command-line values that aren't variables (e.g. -u my_user) |
| 2 | role defaults (roles/x/defaults/main.yml) |
| 3 | inventory file or script group vars |
| 4 | inventory group_vars/all |
| 5 | playbook group_vars/all |
| 6 | inventory group_vars/* |
| 7 | playbook group_vars/* |
| 8 | inventory file or script host vars |
| 9 | inventory host_vars/* |
| 10 | playbook host_vars/* |
| 11 | host facts and cached set_fact |
| 12 | play vars: |
| 13 | play vars_prompt: |
| 14 | play vars_files: |
| 15 | role vars (roles/x/vars/main.yml) |
| 16 | block vars: |
| 17 | task vars: |
| 18 | include_vars |
| 19 | set_fact and registered variables |
| 20 | role and include_role parameters |
| 21 | include parameters |
| 22 | extra vars (-e): always win |
Three rules of thumb cover most real cases:
- Role defaults lose to everything. Put every setting a user might change there.
- Inventory beats nothing in the play:
group_vars<host_vars< playvars< rolevars< taskvars<set_fact<-e. -ewins over everything, evenset_fact. Don't use it for values a role must be able to change.
Seeing it happen
The same variable greeting is set in five places here:
mkdir -p roles/hello/{defaults,tasks} group_vars host_vars
cat > hosts.ini <<'INI'
[web]
web1 ansible_connection=local
INI
echo 'greeting: from role defaults' > roles/hello/defaults/main.yml
echo 'greeting: from group_vars/web' > group_vars/web.yml
echo 'greeting: from host_vars/web1' > host_vars/web1.yml
cat > roles/hello/tasks/main.yml <<'YML'
- name: Say it
ansible.builtin.debug:
msg: "{{ greeting }}"
YML
cat > site.yml <<'YML'
- name: Precedence
hosts: web
gather_facts: false
roles: [hello]
YML
cat > site-vars.yml <<'YML'
- name: Precedence with play vars
hosts: web
gather_facts: false
vars:
greeting: from play vars
roles: [hello]
YML
run() { ansible-playbook -i hosts.ini "$@" | grep '"msg"'; }
run site.yml
run site-vars.yml
run site-vars.yml -e 'greeting="from -e"'
rm host_vars/web1.yml group_vars/web.yml
run site.yml "msg": "from host_vars/web1"
"msg": "from play vars"
"msg": "from -e"
"msg": "from role defaults"
A model of the lookup
Precedence is just "sort the sources by level, last one wins". This Python model resolves a variable the same way:
LEVELS = [ # weakest first (a subset of the 22)
"role defaults", "group_vars/all", "group_vars/<group>", "host_vars/<host>",
"play vars", "role vars", "task vars", "set_fact", "extra vars",
]
def resolve(name, sources):
"""sources: {level: {var: value}}. Returns (value, level) like Ansible would."""
winner = None
for level in LEVELS:
if name in sources.get(level, {}):
winner = (sources[level][name], level)
return winner
sources = {
"role defaults": {"nginx_port": 80, "nginx_workers": 2},
"group_vars/all": {"nginx_workers": 4},
"group_vars/<group>": {"nginx_port": 8080},
"role vars": {"nginx_user": "www-data"},
}
for var in ["nginx_port", "nginx_workers", "nginx_user"]:
print(var, "=", *resolve(var, sources))
sources["extra vars"] = {"nginx_port": 9000}
print("with -e:", *resolve("nginx_port", sources))nginx_port = 8080 group_vars/<group>
nginx_workers = 4 group_vars/all
nginx_user = www-data role vars
with -e: 9000 extra vars
Dictionaries are replaced, not merged
If group_vars/all.yml sets app_db: {host: db1, port: 5432} and host_vars/web1.yml sets app_db: {port: 6432}, web1's app_db is {port: 6432}: the whole dictionary is replaced, and host is gone. Either keep dictionaries flat (app_db_host, app_db_port) or merge explicitly with the combine filter (Filters & Lookups). There is a global hash_behaviour = merge setting, but the documentation discourages it: it changes how every playbook behaves and makes your code depend on a setting outside the repository. Leave it at the default replace.
Debugging "where did this value come from?"
ansible-inventory --host web1shows the inventory-level values.ansible -m ansible.builtin.debug -a var=greeting web1shows the effective value at host level.- In a play,
ansible.builtin.debug: var=greetingwith-vprints it. - Keep each variable defined in as few places as possible. If you need the table above to understand your own repository, simplify.
Test yourself
A role default sets x: 1, group_vars/web.yml sets x: 2. A web host runs the role. What is x?
- 1
- 2
- Error
- Undefined
Which source beats every other?
set_fact- role vars
- extra vars (
-e) - host_vars
group_vars/all sets db: {host: a, port: 1}, host_vars/h sets db: {port: 2}. What is h's db?
{host: a, port: 2}{port: 2}{host: a, port: 1}- An error