Skip to content

Commit

Permalink
Fix variable precedence issue where set facts beat role params
Browse files Browse the repository at this point in the history
Also updates doc on variable precedence, as it was incorrect for the
order of play vars/vars_prompt/vars_files in relation to set_fact and
registered variables.

Fixes ansible#14702
Fixes ansible#14826
  • Loading branch information
jimi-c committed Jun 7, 2016
1 parent 1eab7e1 commit b37b51d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docsite/rst/playbooks_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,11 @@ In 2.x, we have made the order of precedence more specific (with the last listed
* playbook group_vars
* playbook host_vars
* host facts
* registered vars
* set_facts
* play vars
* play vars_prompt
* play vars_files
* registered vars
* set_facts
* role and include vars
* block vars (only for tasks in block)
* task vars (only for the task)
Expand Down
15 changes: 7 additions & 8 deletions lib/ansible/vars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,19 @@ def get_vars(self, loader, play=None, host=None, task=None, include_hostvars=Tru
for role in play.get_roles():
all_vars = combine_vars(all_vars, role.get_vars(include_params=False))

if host:
all_vars = combine_vars(all_vars, self._vars_cache.get(host.get_name(), dict()))
all_vars = combine_vars(all_vars, self._nonpersistent_fact_cache.get(host.name, dict()))

if task:
if task._role:
all_vars = combine_vars(all_vars, task._role.get_vars(include_params=False))
all_vars = combine_vars(all_vars, task._role.get_role_params(task._block.get_dep_chain()))
all_vars = combine_vars(all_vars, task.get_vars())

if host:
all_vars = combine_vars(all_vars, self._vars_cache.get(host.get_name(), dict()))
all_vars = combine_vars(all_vars, self._nonpersistent_fact_cache.get(host.name, dict()))

# special case for include tasks, where the include params
# may be specified in the vars field for the task, which should
# have higher precedence than the vars/np facts above
if task:
# special case for include tasks, where the include params
# may be specified in the vars field for the task, which should
# have higher precedence than the vars/np facts above
all_vars = combine_vars(all_vars, task.get_include_params())

all_vars = combine_vars(all_vars, self._extra_vars)
Expand Down

0 comments on commit b37b51d

Please sign in to comment.