mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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:
parent
269d682f70
commit
cca96b8c9d
7 changed files with 20 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
- hosts: testhost
|
||||
gather_facts: no
|
||||
tasks:
|
||||
- import_role:
|
||||
name: "{{ import_playbook_role_name }}"
|
|
@ -0,0 +1,2 @@
|
|||
- debug:
|
||||
msg: in import_playbook_role
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue