mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-08 09:24:01 -07:00
Support 'apply' to apply attributes to included tasks - Impl 1 (#39236)
* Support 'apply' to apply attributes to included tasks * Cannot validate args for task_include * Only allow apply on include_ * Re-enable arg validation, but only for include_tasks and import_tasks * s/task/ir/ * Add tests for include_ apply * Include context with AnsibleParserError * Add docs for apply * version_added * Add free-form documentation back * Add example of free-form with apply
This commit is contained in:
parent
961484e00d
commit
da4ff18406
9 changed files with 175 additions and 4 deletions
|
@ -23,6 +23,7 @@ from os.path import basename
|
|||
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.playbook.attribute import FieldAttribute
|
||||
from ansible.playbook.block import Block
|
||||
from ansible.playbook.task_include import TaskInclude
|
||||
from ansible.playbook.role import Role
|
||||
from ansible.playbook.role.include import RoleInclude
|
||||
|
@ -45,7 +46,7 @@ class IncludeRole(TaskInclude):
|
|||
|
||||
BASE = ('name', 'role') # directly assigned
|
||||
FROM_ARGS = ('tasks_from', 'vars_from', 'defaults_from') # used to populate from dict in role
|
||||
OTHER_ARGS = ('private', 'allow_duplicates') # assigned to matching property
|
||||
OTHER_ARGS = ('apply', 'private', 'allow_duplicates') # assigned to matching property
|
||||
VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args
|
||||
|
||||
# =================================================================================
|
||||
|
@ -134,6 +135,23 @@ class IncludeRole(TaskInclude):
|
|||
from_key = key.replace('_from', '')
|
||||
ir._from_files[from_key] = basename(ir.args.get(key))
|
||||
|
||||
apply_attrs = ir.args.pop('apply', {})
|
||||
if apply_attrs and ir.action != 'include_role':
|
||||
raise AnsibleParserError('Invalid options for %s: apply' % ir.action, obj=data)
|
||||
elif apply_attrs:
|
||||
apply_attrs['block'] = []
|
||||
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.
|
||||
for option in my_arg_names.intersection(IncludeRole.OTHER_ARGS):
|
||||
setattr(ir, option, ir.args.get(option))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue