diff --git a/changelogs/fragments/82675-fix-unsafe-templating-leading-to-type-error.yml b/changelogs/fragments/82675-fix-unsafe-templating-leading-to-type-error.yml new file mode 100644 index 00000000000..650990d4912 --- /dev/null +++ b/changelogs/fragments/82675-fix-unsafe-templating-leading-to-type-error.yml @@ -0,0 +1,2 @@ +bugfixes: + - template - Fix error when templating an unsafe string which corresponds to an invalid type in Python (https://github.com/ansible/ansible/issues/82600). diff --git a/lib/ansible/template/native_helpers.py b/lib/ansible/template/native_helpers.py index 7c4ce922151..612ed50113f 100644 --- a/lib/ansible/template/native_helpers.py +++ b/lib/ansible/template/native_helpers.py @@ -65,7 +65,7 @@ def ansible_eval_concat(nodes): ) ) ) - except (ValueError, SyntaxError, MemoryError): + except (TypeError, ValueError, SyntaxError, MemoryError): pass return out @@ -127,7 +127,7 @@ def ansible_native_concat(nodes): # parse the string ourselves without removing leading spaces/tabs. ast.parse(out, mode='eval') ) - except (ValueError, SyntaxError, MemoryError): + except (TypeError, ValueError, SyntaxError, MemoryError): return out if isinstance(evaled, string_types): diff --git a/test/integration/targets/template/unsafe.yml b/test/integration/targets/template/unsafe.yml index bef9a4b4500..6f163881496 100644 --- a/test/integration/targets/template/unsafe.yml +++ b/test/integration/targets/template/unsafe.yml @@ -3,6 +3,7 @@ vars: nottemplated: this should not be seen imunsafe: !unsafe '{{ nottemplated }}' + unsafe_set: !unsafe '{{ "test" }}' tasks: - set_fact: @@ -12,11 +13,15 @@ - set_fact: this_always_safe: '{{ imunsafe }}' + - set_fact: + this_unsafe_set: "{{ unsafe_set }}" + - name: ensure nothing was templated assert: that: - this_always_safe == imunsafe - imunsafe == this_was_unsafe.strip() + - unsafe_set == this_unsafe_set.strip() - hosts: localhost