Ansible variable precedence: trace the value a task actually receives
Diagnose Ansible overrides by separating configuration, keywords and variables. Run a local typed-input example, then trace the launch values that can override reviewed inventory.
TL;DR: Diagnose the value at the task that consumes it, then trace the definitions and launch inputs that could supply it. Extra variables can override inventory and play variables; a reviewed inventory file alone does not establish what a job will execute.
A correct file can lose the precedence contest
Consider a teaching fixture: inventory sets worker_port: 8080, the play sets worker_port: 8090, and a job launches with an extra variable of 9090. The task receives the extra-variable value. Editing the inventory again leaves the job unchanged because the higher-precedence input still wins.
Start with the exact launch invocation and selected inventory sources. Inspect the role defaults, play variables and any runtime includes that define the affected name. Use a narrow diagnostic immediately before the consuming task for a nonsecret value. For a credential, inspect whether it is defined and whether its type is valid without logging its contents.
The Ansible variable guide documents the ordering inside the variable category. A useful debugging record contains a variable name, candidate source, observed value or safe property, and the reason one source takes precedence. A list of every precedence level from memory is less useful than finding the obsolete launch parameter.
Command options and extra variables are different inputs
An ordinary CLI option such as -u deployer is not an extra variable. An inventory connection variable such as ansible_user can override that option. -e ansible_user=deployer supplies a variable at a different precedence. See the general precedence rules before diagnosing a connection under the assumption that everything on the command line wins.
| Evidence | Establishes | Does not establish |
|---|---|---|
| Inventory inspection | Host membership and inventory-derived inputs | All values introduced during play execution |
| Resolved value before the task | The value at that point for that host | Which earlier definition supplied it |
| Launch arguments and variable files | Explicit caller overrides | All role or runtime inputs |
| Configuration inspection | Selected Ansible settings | The final value of every task variable |
Reproduce a typed override locally
Save this as inputs.yml in a temporary directory. It uses localhost, requires no privilege escalation and changes no files. The integer test comes before the range comparisons so a string fails with the intended validation message.
- name: Inspect a worker input contract
hosts: localhost
connection: local
gather_facts: false
vars:
worker_port: 8080
tasks:
- name: Require an integer port
ansible.builtin.assert:
that: worker_port is integer
fail_msg: Supply worker_port as an integer
- name: Require an allowed port
ansible.builtin.assert:
that:
- worker_port >= 1024
- worker_port <= 65535
- name: Inspect the nonsecret effective value
ansible.builtin.debug:
var: worker_port
Run these independently; the final command is intentionally invalid:
ansible-playbook -i localhost, inputs.yml
ansible-playbook -i localhost, inputs.yml -e '{"worker_port": 9090}'
ansible-playbook -i localhost, inputs.yml -e 'worker_port=9090'
The first execution prints 8080; the JSON extra variable selects integer 9090. The key-value form supplies a string and fails the type assertion. The example's policy disallows privileged ports; that is a chosen worker contract, not a universal Ansible restriction. Passing integer 80 tests the separate range failure.
Do not cast every unexpected value to an integer merely to make validation pass. Decide whether the role accepts strings at its public boundary. If it does, normalize deliberately and reject malformed values before rendering a template. Role argument validation gives reusable roles a place to document this contract.
Repair the source of authority
Suppose a deployment should follow inventory but a launch wrapper still injects last month's port. Remove that override from the wrapper and repeat the job in the intended environment. Adding another set_fact creates another competing definition and can still lose to extra variables.
Keep freely adjustable inputs separate from safety decisions. A caller who can override both a requested destination and the variable used as its supposed allowlist can defeat that comparison. Protect authorization in the job's permissions and deployment controls; an assertion in caller-controlled automation is a useful error check, not an independent security boundary.
Self-check: ansible-inventory --host worker-1 shows port 8080, but the task prints 9090. Has Ansible ignored inventory? No. Inventory inspection is only one part of the evidence. Inspect play/runtime definitions and launch extra variables, then reproduce the final value at the consuming task.
Practise this diagnosis in the unexpected Ansible override scenario, then examine check-mode evidence before trusting a dry run of the corrected job.