mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Change insertion order of apply block to not affect the include_X task itself (#44912)
This commit is contained in:
parent
2dfff57f57
commit
a0d7d4b82f
6 changed files with 50 additions and 32 deletions
2
changelogs/fragments/include-apply-insert-order.yaml
Normal file
2
changelogs/fragments/include-apply-insert-order.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- include - Change order of where the new block is inserted with apply so that apply args are not applied to the include also (https://github.com/ansible/ansible/pull/44912)
|
|
@ -100,14 +100,16 @@ class IncludeRole(TaskInclude):
|
||||||
dep_chain = list(self._parent_role._parents)
|
dep_chain = list(self._parent_role._parents)
|
||||||
dep_chain.append(self._parent_role)
|
dep_chain.append(self._parent_role)
|
||||||
|
|
||||||
|
p_block = self.build_parent_block()
|
||||||
|
|
||||||
blocks = actual_role.compile(play=myplay, dep_chain=dep_chain)
|
blocks = actual_role.compile(play=myplay, dep_chain=dep_chain)
|
||||||
for b in blocks:
|
for b in blocks:
|
||||||
b._parent = self
|
b._parent = p_block
|
||||||
|
|
||||||
# updated available handlers in play
|
# updated available handlers in play
|
||||||
handlers = actual_role.get_handler_blocks(play=myplay)
|
handlers = actual_role.get_handler_blocks(play=myplay)
|
||||||
for h in handlers:
|
for h in handlers:
|
||||||
h._parent = self
|
h._parent = p_block
|
||||||
myplay.handlers = myplay.handlers + handlers
|
myplay.handlers = myplay.handlers + handlers
|
||||||
return blocks, handlers
|
return blocks, handlers
|
||||||
|
|
||||||
|
@ -143,22 +145,11 @@ class IncludeRole(TaskInclude):
|
||||||
from_key = key.replace('_from', '')
|
from_key = key.replace('_from', '')
|
||||||
ir._from_files[from_key] = basename(ir.args.get(key))
|
ir._from_files[from_key] = basename(ir.args.get(key))
|
||||||
|
|
||||||
apply_attrs = ir.args.pop('apply', {})
|
apply_attrs = ir.args.get('apply', {})
|
||||||
if apply_attrs and ir.action != 'include_role':
|
if apply_attrs and ir.action != 'include_role':
|
||||||
raise AnsibleParserError('Invalid options for %s: apply' % ir.action, obj=data)
|
raise AnsibleParserError('Invalid options for %s: apply' % ir.action, obj=data)
|
||||||
elif apply_attrs:
|
elif not isinstance(apply_attrs, dict):
|
||||||
apply_attrs['block'] = []
|
raise AnsibleParserError('Expected a dict for apply but got %s instead' % type(apply_attrs), obj=data)
|
||||||
p_block = Block.load(
|
|
||||||
apply_attrs,
|
|
||||||
play=block._play,
|
|
||||||
parent_block=block,
|
|
||||||
role=role,
|
|
||||||
task_include=task_include,
|
|
||||||
use_handlers=block._use_handlers,
|
|
||||||
variable_manager=variable_manager,
|
|
||||||
loader=loader,
|
|
||||||
)
|
|
||||||
ir._parent = p_block
|
|
||||||
|
|
||||||
# manual list as otherwise the options would set other task parameters we don't want.
|
# manual list as otherwise the options would set other task parameters we don't want.
|
||||||
for option in my_arg_names.intersection(IncludeRole.OTHER_ARGS):
|
for option in my_arg_names.intersection(IncludeRole.OTHER_ARGS):
|
||||||
|
|
|
@ -69,22 +69,11 @@ class TaskInclude(Task):
|
||||||
if not task.args.get('_raw_params'):
|
if not task.args.get('_raw_params'):
|
||||||
task.args['_raw_params'] = task.args.pop('file')
|
task.args['_raw_params'] = task.args.pop('file')
|
||||||
|
|
||||||
apply_attrs = task.args.pop('apply', {})
|
apply_attrs = task.args.get('apply', {})
|
||||||
if apply_attrs and task.action != 'include_tasks':
|
if apply_attrs and task.action != 'include_tasks':
|
||||||
raise AnsibleParserError('Invalid options for %s: apply' % task.action, obj=data)
|
raise AnsibleParserError('Invalid options for %s: apply' % task.action, obj=data)
|
||||||
elif apply_attrs:
|
elif not isinstance(apply_attrs, dict):
|
||||||
apply_attrs['block'] = []
|
raise AnsibleParserError('Expected a dict for apply but got %s instead' % type(apply_attrs), obj=data)
|
||||||
p_block = Block.load(
|
|
||||||
apply_attrs,
|
|
||||||
play=block._play,
|
|
||||||
parent_block=block,
|
|
||||||
role=role,
|
|
||||||
task_include=task_include,
|
|
||||||
use_handlers=block._use_handlers,
|
|
||||||
variable_manager=variable_manager,
|
|
||||||
loader=loader,
|
|
||||||
)
|
|
||||||
task._parent = p_block
|
|
||||||
|
|
||||||
return task
|
return task
|
||||||
|
|
||||||
|
@ -115,3 +104,24 @@ class TaskInclude(Task):
|
||||||
del all_vars['when']
|
del all_vars['when']
|
||||||
|
|
||||||
return all_vars
|
return all_vars
|
||||||
|
|
||||||
|
def build_parent_block(self):
|
||||||
|
'''
|
||||||
|
This method is used to create the parent block for the included tasks
|
||||||
|
when ``apply`` is specified
|
||||||
|
'''
|
||||||
|
apply_attrs = self.args.pop('apply', {})
|
||||||
|
if apply_attrs:
|
||||||
|
apply_attrs['block'] = []
|
||||||
|
p_block = Block.load(
|
||||||
|
apply_attrs,
|
||||||
|
play=self._parent._play,
|
||||||
|
task_include=self,
|
||||||
|
role=self._role,
|
||||||
|
variable_manager=self._variable_manager,
|
||||||
|
loader=self._loader,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
p_block = self
|
||||||
|
|
||||||
|
return p_block
|
||||||
|
|
|
@ -833,8 +833,7 @@ class StrategyBase:
|
||||||
block_list = load_list_of_blocks(
|
block_list = load_list_of_blocks(
|
||||||
data,
|
data,
|
||||||
play=iterator._play,
|
play=iterator._play,
|
||||||
parent_block=None,
|
parent_block=ti_copy.build_parent_block(),
|
||||||
task_include=ti_copy,
|
|
||||||
role=included_file._task._role,
|
role=included_file._task._role,
|
||||||
use_handlers=is_handler,
|
use_handlers=is_handler,
|
||||||
loader=self._loader,
|
loader=self._loader,
|
||||||
|
|
|
@ -29,3 +29,17 @@
|
||||||
- include_role_result is defined
|
- include_role_result is defined
|
||||||
tags:
|
tags:
|
||||||
- always
|
- always
|
||||||
|
|
||||||
|
- include_role:
|
||||||
|
name: include_role2
|
||||||
|
apply:
|
||||||
|
tags:
|
||||||
|
- foo
|
||||||
|
tags:
|
||||||
|
- not_specified_on_purpose
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- include_role2_result is undefined
|
||||||
|
tags:
|
||||||
|
- always
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
- set_fact:
|
||||||
|
include_role2_result: true
|
Loading…
Reference in a new issue