Ansible Variables and Precedence
Ansible has twenty-two levels of variable precedence and you do not need to memorise them - you need the five that occur in practice, in the right order. This sets the same variable in all five at once and removes them one by one, so each level is revealed by what takes over when the one above it disappears.
Plays and Playbooks Guide 11 of 45 Intermediate
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 16 min
- Reviewed23 August 2026
Written against the versions above. The full precedence list is in the documentation and is worth reading once. The order that matters day to day, highest first: `-e` on the command line, then play `vars`, then `host_vars/`, then `group_vars/<group>`, then `group_vars/all`. Note that `-e` beats absolutely everything, which is what makes it useful and dangerous.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
Before you start
- A control node configured as shown in the first step.
- The session creates
~/pb/group_vars/,~/pb/host_vars/and a small playbook, then deletes files from them one at a time.
-
The configuration this guide assumes
-
The same variable in five places
-
Play vars beat the files
-
Take them away one at a time
-
Where a variable actually resolved
-
Registering a result as a variable