diff --git a/changelogs/fragments/delegate-to-get-vars-no-task.yaml b/changelogs/fragments/delegate-to-get-vars-no-task.yaml new file mode 100644 index 0000000000..7a2035f587 --- /dev/null +++ b/changelogs/fragments/delegate-to-get-vars-no-task.yaml @@ -0,0 +1,2 @@ +bugfixes: +- delegate_to - ensure if we get a non-Task object in _get_delegated_vars, we return early (https://github.com/ansible/ansible/pull/44934) diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py index ebbfa6aedc..921e63d92a 100644 --- a/lib/ansible/vars/manager.py +++ b/lib/ansible/vars/manager.py @@ -487,6 +487,10 @@ class VariableManager: return variables def _get_delegated_vars(self, play, task, existing_variables): + if not hasattr(task, 'loop'): + # This "task" is not a Task, so we need to skip it + return {} + # we unfortunately need to template the delegate_to field here, # as we're fetching vars before post_validate has been called on # the task that has been passed in diff --git a/test/integration/targets/include_import/apply/include_apply.yml b/test/integration/targets/include_import/apply/include_apply.yml index ca41b38df5..32c6e5e9f0 100644 --- a/test/integration/targets/include_import/apply/include_apply.yml +++ b/test/integration/targets/include_import/apply/include_apply.yml @@ -43,3 +43,8 @@ - include_role2_result is undefined tags: - always + + - include_role: + name: include_role + apply: + delegate_to: testhost2