Skip to content

Commit

Permalink
Pass vars from import_playbook in early (ansible#39521)
Browse files Browse the repository at this point in the history
* Pass vars from import_playbook in early, as they may be needed to parse the imported plays. Fixes ansible#33693

* Add test for import_playbook vars
  • Loading branch information
sivel authored Apr 30, 2018
1 parent 269d682 commit cca96b8
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/ansible/playbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def load(file_name, variable_manager=None, loader=None):
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
return pb

def _load_playbook_data(self, file_name, variable_manager):
def _load_playbook_data(self, file_name, variable_manager, vars=None):

if os.path.isabs(file_name):
self._basedir = os.path.dirname(file_name)
Expand Down Expand Up @@ -103,7 +103,7 @@ def _load_playbook_data(self, file_name, variable_manager):
which = entry.get('import_playbook', entry.get('include', entry))
display.display("skipping playbook '%s' due to conditional test failure" % which, color=C.COLOR_SKIP)
else:
entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader)
entry_obj = Play.load(entry, variable_manager=variable_manager, loader=self._loader, vars=vars)
self._entries.append(entry_obj)

# we're done, so restore the old basedir in the loader
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/playbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ def _validate_variable_keys(ds):
try:
if isinstance(ds, dict):
_validate_variable_keys(ds)
return ds
return combine_vars(self.vars, ds)
elif isinstance(ds, list):
all_vars = dict()
all_vars = self.vars
for item in ds:
if not isinstance(item, dict):
raise ValueError
Expand Down
4 changes: 3 additions & 1 deletion lib/ansible/playbook/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ def get_name(self):
return self._attributes.get('name')

@staticmethod
def load(data, variable_manager=None, loader=None):
def load(data, variable_manager=None, loader=None, vars=None):
if ('name' not in data or data['name'] is None) and 'hosts' in data:
if isinstance(data['hosts'], list):
data['name'] = ','.join(data['hosts'])
else:
data['name'] = data['hosts']
p = Play()
if vars:
p.vars = vars.copy()
return p.load_data(data, variable_manager=variable_manager, loader=loader)

def preprocess_data(self, ds):
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/playbook/playbook_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def load_data(self, ds, basedir, variable_manager=None, loader=None):
if not os.path.isabs(file_name):
file_name = os.path.join(basedir, file_name)

pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager, vars=self.vars.copy())

# finally, update each loaded playbook entry with any variables specified
# on the included playbook and/or any tags which may have been set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- hosts: testhost
gather_facts: no
tasks:
- import_role:
name: "{{ import_playbook_role_name }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- debug:
msg: in import_playbook_role
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
when: include_next_playbook

- import_playbook: validate34.yml

- import_playbook: playbook_needing_vars.yml
vars:
import_playbook_role_name: import_playbook_role

0 comments on commit cca96b8

Please sign in to comment.