Include section on lazy vs eager variable evaluation #2955
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a new section to the Variables guide titled “Lazy vs. Eager Variable Evaluation”. It expands on the existing documentation around variable precedence by demonstrating the difference between eager and lazy evaluation of variables — specifically comparing set_fact (eager) with vars: (lazy).
It includes a self-contained example that walks through:
How variables set with set_fact are evaluated and fixed at the time they are run, how variables defined in vars: blocks are lazily re-evaluated each time they're referenced, and the effect of --extra-vars on both eager and lazy variables.
Also includes an illustration of variable shadowing — showing that vars: blocks can override some variables but not those set using set_fact (due to variable precedence rules).
The section is placed immediately after Variable Precedence: Where Should I Put a Variable? and before Using Advanced Variable Syntax to provide a natural learning progression from where and how variables are resolved, to when they're evaluated. All code and output are copy/paste testable, and align with live Ansible behavior as Ansible core 2.14.2.
This update aims to clarify a subtle aspect of Ansible variable resolution.