2.14: jinja2_native: preserve quotes in strings (#79119) (#79144)

* jinja2_native: preserve quotes in strings (#79119)

Fixes #79083

(cherry picked from commit d34b578685)

* Fix test for jinja2_native preserve quotes (#79131)

Fixes https://github.com/ansible/ansible/pull/79119#discussion_r993752129

(cherry picked from commit 3a6eca6670)
pull/79402/head
Martin Krizek 3 years ago committed by GitHub
parent 59f3c0238b
commit c5065264c2
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]) out = ''.join([to_text(v) for v in nodes])
try: try:
return ast.literal_eval( evaled = ast.literal_eval(
# In Python 3.10+ ast.literal_eval removes leading spaces/tabs # In Python 3.10+ ast.literal_eval removes leading spaces/tabs
# from the given string. For backwards compatibility we need to # from the given string. For backwards compatibility we need to
# parse the string ourselves without removing leading spaces/tabs. # parse the string ourselves without removing leading spaces/tabs.
@ -136,3 +136,9 @@ def ansible_native_concat(nodes):
) )
except (ValueError, SyntaxError, MemoryError): except (ValueError, SyntaxError, MemoryError):
return out 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 --vault-password-file test_vault_pass test_vault.yml -v "$@"
ansible-playbook test_hostvars.yml -v "$@" ansible-playbook test_hostvars.yml -v "$@"
ansible-playbook nested_undefined.yml -v "$@" ansible-playbook nested_undefined.yml -v "$@"
ansible-playbook test_preserving_quotes.yml -v "$@"
unset ANSIBLE_JINJA2_NATIVE unset ANSIBLE_JINJA2_NATIVE

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

@ -22,7 +22,7 @@
- assert: - assert:
that: that:
- 'string_sum == "12"' - string_sum == "'12'"
- 'string_sum|type_debug in ["str", "unicode"]' - 'string_sum|type_debug in ["str", "unicode"]'
- name: add two lists - 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_str: '""'
Loading…
Cancel
Save