Browse code

[2.9] Handle post_validate templating errors and add tests (#70240) (#70390)

* Handle post_validate templating errors and fix tests (#70240)

* Handle unexpected templating errors

* Fixes #70050

Fix up tests that weren't running and add tests for graceful templating error handling

(cherry picked from commit 30e70f4b6356e692c7ade3dd95f2e55d07f3e8f5)

* changelog

ci_complete

Sloane Hertel authored on 2020/07/18 06:35:07
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+bugfixes:
1
+  - TaskExecutor - Handle unexpected errors as failed while post validating loops (https://github.com/ansible/ansible/issues/70050).
... ...
@@ -584,7 +584,12 @@ class TaskExecutor:
584 584
             return dict(include_args=include_args)
585 585
 
586 586
         # Now we do final validation on the task, which sets all fields to their final values.
587
-        self._task.post_validate(templar=templar)
587
+        try:
588
+            self._task.post_validate(templar=templar)
589
+        except AnsibleError:
590
+            raise
591
+        except Exception:
592
+            return dict(changed=False, failed=True, _ansible_no_log=self._play_context.no_log, exception=to_text(traceback.format_exc()))
588 593
         if '_variable_params' in self._task.args:
589 594
             variable_params = self._task.args.pop('_variable_params')
590 595
             if isinstance(variable_params, dict):
591 596
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+- name: Task that fails due to templating error for plugin option
1
+  debug: msg="{{ 5 / 0 | int }}"
2
+  ignore_errors: true
3
+  register: result
4
+
5
+- assert:
6
+    that:
7
+      - result.failed
8
+      - result.exception
9
+
10
+- name: Loop that fails due to templating error in first entry and ignores errors
11
+  debug: msg="{{ 5 / item }}"
12
+  ignore_errors: true
13
+  register: result
14
+  loop: [0, 0, 1]
15
+
16
+- debug: var=result
17
+
18
+- assert:
19
+    that:
20
+      - result.results[0].failed
21
+      - result.results[0].exception
22
+      - result.results[0].item == 0
23
+
24
+      - result.results[1].failed
25
+      - result.results[1].exception
26
+      - result.results[1].item == 0
27
+
28
+      - not result.results[2].failed
29
+      - result.results[2].exception is undefined
30
+      - result.results[2].item == 1
... ...
@@ -288,6 +288,8 @@
288 288
     that:
289 289
       - "hello_world_string|trim == 'Hello world!'"
290 290
 
291
+- include_tasks: ./errors.yml
292
+
291 293
 # Vars lookups
292 294
 
293 295
 - name: Test that we can give it a single value and receive a single value