Skip to content

Commit

Permalink
Further tweaks to variable precedence to make it match our docs
Browse files Browse the repository at this point in the history
Also removes looking at role variables from the Block, as those are
merged in separately via VariableManager
  • Loading branch information
jimi-c committed Jun 7, 2016
1 parent b37b51d commit 068e447
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 0 additions & 2 deletions lib/ansible/playbook/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ def get_vars(self):

all_vars = self.vars.copy()

if self._role:
all_vars.update(self._role.get_vars(self._dep_chain, include_params=False))
if self._parent_block:
all_vars.update(self._parent_block.get_vars())
if self._task_include:
Expand Down
16 changes: 14 additions & 2 deletions lib/ansible/vars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,37 @@ def get_vars(self, loader, play=None, host=None, task=None, include_hostvars=Tru
display.vvv("skipping vars_file '%s' due to an undefined variable" % vars_file_item)
continue

# By default, we now merge in all vars from all roles in the play,
# unless the user has disabled this via a config option
if not C.DEFAULT_PRIVATE_ROLE_VARS:
for role in play.get_roles():
all_vars = combine_vars(all_vars, role.get_vars(include_params=False))

# next, we merge in the vars from the role, which will specifically
# follow the role dependency chain, and then we merge in the tasks
# vars (which will look at parent blocks/task includes)
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.get_vars())

# next, we merge in the vars cache (include vars) and nonpersistent
# facts cache (set_fact/register), in that order
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()))

# next, we merge in role params and task include params
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())

# 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())

# finally, we merge in extra vars and the magic variables
all_vars = combine_vars(all_vars, self._extra_vars)
all_vars = combine_vars(all_vars, magic_variables)

Expand Down

0 comments on commit 068e447

Please sign in to comment.