mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 06:31:23 -07: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)
|
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
|
||||||
return pb
|
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):
|
if os.path.isabs(file_name):
|
||||||
self._basedir = os.path.dirname(file_name)
|
self._basedir = os.path.dirname(file_name)
|
||||||
|
@ -103,7 +103,7 @@ class Playbook:
|
||||||
which = entry.get('import_playbook', entry.get('include', entry))
|
which = entry.get('import_playbook', entry.get('include', entry))
|
||||||
display.display("skipping playbook '%s' due to conditional test failure" % which, color=C.COLOR_SKIP)
|
display.display("skipping playbook '%s' due to conditional test failure" % which, color=C.COLOR_SKIP)
|
||||||
else:
|
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)
|
self._entries.append(entry_obj)
|
||||||
|
|
||||||
# we're done, so restore the old basedir in the loader
|
# we're done, so restore the old basedir in the loader
|
||||||
|
|
|
@ -486,9 +486,9 @@ class Base(with_metaclass(BaseMeta, object)):
|
||||||
try:
|
try:
|
||||||
if isinstance(ds, dict):
|
if isinstance(ds, dict):
|
||||||
_validate_variable_keys(ds)
|
_validate_variable_keys(ds)
|
||||||
return ds
|
return combine_vars(self.vars, ds)
|
||||||
elif isinstance(ds, list):
|
elif isinstance(ds, list):
|
||||||
all_vars = dict()
|
all_vars = self.vars
|
||||||
for item in ds:
|
for item in ds:
|
||||||
if not isinstance(item, dict):
|
if not isinstance(item, dict):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
|
@ -101,13 +101,15 @@ class Play(Base, Taggable, Become):
|
||||||
return self._attributes.get('name')
|
return self._attributes.get('name')
|
||||||
|
|
||||||
@staticmethod
|
@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 ('name' not in data or data['name'] is None) and 'hosts' in data:
|
||||||
if isinstance(data['hosts'], list):
|
if isinstance(data['hosts'], list):
|
||||||
data['name'] = ','.join(data['hosts'])
|
data['name'] = ','.join(data['hosts'])
|
||||||
else:
|
else:
|
||||||
data['name'] = data['hosts']
|
data['name'] = data['hosts']
|
||||||
p = Play()
|
p = Play()
|
||||||
|
if vars:
|
||||||
|
p.vars = vars.copy()
|
||||||
return p.load_data(data, variable_manager=variable_manager, loader=loader)
|
return p.load_data(data, variable_manager=variable_manager, loader=loader)
|
||||||
|
|
||||||
def preprocess_data(self, ds):
|
def preprocess_data(self, ds):
|
||||||
|
|
|
@ -69,7 +69,7 @@ class PlaybookInclude(Base, Conditional, Taggable):
|
||||||
if not os.path.isabs(file_name):
|
if not os.path.isabs(file_name):
|
||||||
file_name = os.path.join(basedir, 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
|
# finally, update each loaded playbook entry with any variables specified
|
||||||
# on the included playbook and/or any tags which may have been set
|
# 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
|
when: include_next_playbook
|
||||||
|
|
||||||
- import_playbook: validate34.yml
|
- import_playbook: validate34.yml
|
||||||
|
|
||||||
|
- import_playbook: playbook_needing_vars.yml
|
||||||
|
vars:
|
||||||
|
import_playbook_role_name: import_playbook_role
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue