diff --git a/changelogs/fragments/loop-control-label-template-error.yaml b/changelogs/fragments/loop-control-label-template-error.yaml new file mode 100644 index 00000000000..f8711f8724e --- /dev/null +++ b/changelogs/fragments/loop-control-label-template-error.yaml @@ -0,0 +1,5 @@ +bugfixes: +- loop_control - Catch exceptions when templating label individually for loop + iterations which caused the templating failure as the full + result. This instead only registers the templating exception for a single + loop result (https://github.com/ansible/ansible/issues/48879) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index af928fe5aa6..3d94531bb39 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -361,7 +361,13 @@ class TaskExecutor: res['_ansible_ignore_errors'] = task_fields.get('ignore_errors') # gets templated here unlike rest of loop_control fields, depends on loop_var above - res['_ansible_item_label'] = templar.template(label, cache=False) + try: + res['_ansible_item_label'] = templar.template(label, cache=False) + except AnsibleUndefinedVariable as e: + res.update({ + 'failed': True, + 'msg': 'Failed to template loop_control.label: %s' % to_text(e) + }) self._final_q.put( TaskResult(