diff --git a/changelogs/fragments/57399-finalize-task-in-strategy.yml b/changelogs/fragments/57399-finalize-task-in-strategy.yml new file mode 100644 index 00000000000000..2a0e096a8c9ac4 --- /dev/null +++ b/changelogs/fragments/57399-finalize-task-in-strategy.yml @@ -0,0 +1,5 @@ +bugfixes: +- Strategy - When building the task in the Strategy from the Worker, + ensure it is properly marked as finalized and squashed. Addresses an + issue with ``ansible_failed_task``. + (https://github.com/ansible/ansible/issues/57399) diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index df04592831a6cd..5fc050895f4130 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -545,6 +545,13 @@ def from_attrs(self, attrs): else: setattr(self, attr, value) + # from_attrs is only used to create a finalized task + # from attrs from the Worker/TaskExecutor + # Those attrs are finalized and squashed in the TE + # and controller side use needs to reflect that + self._finalized = True + self._squashed = True + def serialize(self): ''' Serializes the object derived from the base object into diff --git a/test/integration/targets/blocks/finalized_task.yml b/test/integration/targets/blocks/finalized_task.yml new file mode 100644 index 00000000000000..300401b51c4a33 --- /dev/null +++ b/test/integration/targets/blocks/finalized_task.yml @@ -0,0 +1,17 @@ +- hosts: localhost + gather_facts: false + tasks: + - block: + - include_role: + name: '{{ item }}' + loop: + - fail + rescue: + - debug: + msg: "{{ ansible_failed_task.name }}" + + - assert: + that: + - ansible_failed_task.name == "Fail" + - ansible_failed_task.action == "fail" + - ansible_failed_task.parent is not defined diff --git a/test/integration/targets/blocks/roles/fail/tasks/main.yml b/test/integration/targets/blocks/roles/fail/tasks/main.yml new file mode 100644 index 00000000000000..176fe5423dd61c --- /dev/null +++ b/test/integration/targets/blocks/roles/fail/tasks/main.yml @@ -0,0 +1,3 @@ +- name: Fail + fail: + msg: fail