CertGrid CertGrid
Hands-on Lab·Ansible

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

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.

One managed node. Precedence is per host, and a second host would only repeat the same result.
Server NameIP AddressOSRolesCPURAMHDD
ANS-CTL01192.168.0.36Ubuntu 26.04 LTSAnsible Control Node2 Core3 GB50 GB
ANS-A01192.168.0.37Ubuntu 26.04 LTSManaged Node (group: web)2 Core3 GB50 GB

Before you start

  1. The configuration this guide assumes

  2. The same variable in five places

  3. Play vars beat the files

  4. Take them away one at a time

  5. Where a variable actually resolved

  6. Registering a result as a variable

Official sources