From 3ecf438e661d39dd7dee31955e6ce5bf2428d6b0 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 12 Aug 2021 21:47:33 +0200 Subject: [PATCH] jinja2_native: short-circuit literal_eval for non-strings (#75484) --- .../fragments/native-types-short-circuit-literal-eval.yml | 2 ++ lib/ansible/template/native_helpers.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/native-types-short-circuit-literal-eval.yml diff --git a/changelogs/fragments/native-types-short-circuit-literal-eval.yml b/changelogs/fragments/native-types-short-circuit-literal-eval.yml new file mode 100644 index 00000000000..2e860e4d1fe --- /dev/null +++ b/changelogs/fragments/native-types-short-circuit-literal-eval.yml @@ -0,0 +1,2 @@ +minor_changes: + - jinja2_native - short-circuit ``ast.literal_eval`` for non-string values diff --git a/lib/ansible/template/native_helpers.py b/lib/ansible/template/native_helpers.py index 4eb630614ef..d2632051cab 100644 --- a/lib/ansible/template/native_helpers.py +++ b/lib/ansible/template/native_helpers.py @@ -15,7 +15,7 @@ from jinja2.runtime import StrictUndefined from ansible.module_utils._text import to_text from ansible.module_utils.common.collections import is_sequence, Mapping from ansible.module_utils.common.text.converters import container_to_text -from ansible.module_utils.six import PY2, text_type +from ansible.module_utils.six import PY2, text_type, string_types from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode from ansible.utils.native_jinja import NativeJinjaText @@ -73,6 +73,10 @@ def ansible_native_concat(nodes): # https://github.com/pallets/jinja/issues/1200 # https://github.com/ansible/ansible/issues/70831#issuecomment-664190894 return out + + # short-circuit literal_eval for anything other than strings + if not isinstance(out, string_types): + return out else: if isinstance(nodes, types.GeneratorType): nodes = chain(head, nodes)