jinja2_native: preserve quotes in strings (#79119)

Fixes #79083
pull/79122/head
Martin Krizek 2 years ago committed by GitHub
parent f9cb679675
commit d34b578685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- "jinja2_native: preserve quotes in strings (https://github.com/ansible/ansible/issues/79083)"

@ -128,7 +128,7 @@ def ansible_native_concat(nodes):
out = ''.join([to_text(v) for v in nodes])
try:
return ast.literal_eval(
evaled = ast.literal_eval(
# In Python 3.10+ ast.literal_eval removes leading spaces/tabs
# from the given string. For backwards compatibility we need to
# parse the string ourselves without removing leading spaces/tabs.
@ -136,3 +136,9 @@ def ansible_native_concat(nodes):
)
except (ValueError, SyntaxError, MemoryError):
return out
if isinstance(evaled, string_types):
quote = out[0]
return f'{quote}{evaled}{quote}'
return evaled

@ -7,4 +7,5 @@ ansible-playbook runtests.yml -v "$@"
ansible-playbook --vault-password-file test_vault_pass test_vault.yml -v "$@"
ansible-playbook test_hostvars.yml -v "$@"
ansible-playbook nested_undefined.yml -v "$@"
ansible-playbook test_preserving_quotes.yml -v "$@"
unset ANSIBLE_JINJA2_NATIVE

@ -13,7 +13,7 @@
- assert:
that:
- 'int_to_str == "2"'
- int_to_str == "'2'"
- 'int_to_str|type_debug in ["str", "unicode"]'
- 'int_to_str2 == "2"'
- 'int_to_str2|type_debug in ["NativeJinjaText"]'

@ -22,7 +22,7 @@
- assert:
that:
- 'string_sum == "12"'
- string_sum == "'12'"
- 'string_sum|type_debug in ["str", "unicode"]'
- name: add two lists

@ -0,0 +1,14 @@
- hosts: localhost
gather_facts: false
tasks:
- assert:
that:
- quoted_str == '"hello"'
- empty_quoted_str == '""'
vars:
third_nested_lvl: '"hello"'
second_nested_lvl: "{{ third_nested_lvl }}"
first_nested_lvl: "{{ second_nested_lvl }}"
quoted_str: "{{ first_nested_lvl }}"
empty_quoted_str: '""'
empty_str: "{{ empty_quoted_str}}"
Loading…
Cancel
Save