fix handler include_tasks templating (#85015)

* Add test for a handler including tasks from a variable filename

* Add FieldAttributeBase attribute to indicate if the object should be post validated

Co-authored-by: Matt Davis <6775756+nitzmahone@users.noreply.github.com>
(cherry picked from commit d3977ebc88)
pull/85305/head
Sloane Hertel 6 months ago committed by Matt Davis
parent 96b97dee7c
commit 58496bed29

@ -0,0 +1,2 @@
bugfixes:
- include_tasks - fix templating options when used as a handler (https://github.com/ansible/ansible/pull/85015).

@ -83,6 +83,11 @@ class _ClassProperty:
class FieldAttributeBase:
_post_validate_object = False
"""
`False` skips FieldAttribute post-validation on intermediate objects and mixins for attributes without `always_post_validate`.
Leaf objects (e.g., `Task`) should set this attribute `True` to opt-in to post-validation.
"""
fattributes = _ClassProperty()
@classmethod
@ -566,8 +571,8 @@ class FieldAttributeBase:
# only import_role is checked here because import_tasks never reaches this point
return Sentinel
# FIXME: compare types, not strings
if not attribute.always_post_validate and self.__class__.__name__ not in ('Task', 'Handler', 'PlayContext', 'IncludeRole', 'TaskInclude'):
# Skip post validation unless always_post_validate is True, or the object requires post validation.
if not attribute.always_post_validate and not self._post_validate_object:
# Intermediate objects like Play() won't have their fields validated by
# default, as their values are often inherited by other objects and validated
# later, so we don't want them to fail out early

@ -71,6 +71,8 @@ class PlayContext(Base):
connection/authentication information.
"""
_post_validate_object = True
# base
module_compression = FieldAttribute(isa='string', default=C.DEFAULT_MODULE_COMPRESSION)
shell = FieldAttribute(isa='string')

@ -65,6 +65,8 @@ class Task(Base, Conditional, Taggable, CollectionSearch, Notifiable, Delegatabl
Task.something(...)
"""
_post_validate_object = True
# =================================================================================
# ATTRIBUTES
# load_<attribute_name> and

@ -9,8 +9,15 @@
debug:
msg: notifying handler
changed_when: yes
notify: include a task from the handlers section
notify:
- include a task from the handlers section
- include a task in a loop from the handlers section
handlers:
- name: include a task from the handlers section
include_tasks: handlers.yml
- name: include a task in a loop from the handlers section
include_tasks: "{{ item }}"
with_first_found:
- handlers.yml

Loading…
Cancel
Save