Pass vars from import_playbook in early (#39521)

* Pass vars from import_playbook in early, as they may be needed to parse the imported plays. Fixes #33693

* Add test for import_playbook vars
This commit is contained in:
Matt Martz 2018-04-30 16:13:43 -05:00 committed by GitHub
parent 269d682f70
commit cca96b8c9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 6 deletions

View file

@ -54,7 +54,7 @@ class Playbook:
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)
@ -103,7 +103,7 @@ class Playbook:
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

View file

@ -486,9 +486,9 @@ class Base(with_metaclass(BaseMeta, object)):
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

View file

@ -101,13 +101,15 @@ class Play(Base, Taggable, Become):
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):

View file

@ -69,7 +69,7 @@ class PlaybookInclude(Base, Conditional, Taggable):
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

View file

@ -0,0 +1,6 @@
---
- hosts: testhost
gather_facts: no
tasks:
- import_role:
name: "{{ import_playbook_role_name }}"

View file

@ -0,0 +1,2 @@
- debug:
msg: in import_playbook_role

View file

@ -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