native types: literal_eval all the things (#68938)

With https://github.com/pallets/jinja/pull/1190 merged our short-circuit
is no longer valid (has it ever been?) as now data like ' True ' may go
through our ansible_native_concat function as opposed to going through
intermediate call to Jinja2's native_concat before. Now we need to always
send data through literal_eval to ensure native types are returned.
pull/69006/head
Martin Krizek 5 years ago committed by GitHub
parent 3591451bc7
commit acdc9eb76d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,6 +13,8 @@ import types
from jinja2.runtime import StrictUndefined from jinja2.runtime import StrictUndefined
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
from ansible.module_utils.common.text.converters import container_to_text
from ansible.module_utils.six import PY2
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
@ -48,10 +50,6 @@ def ansible_native_concat(nodes):
# See https://github.com/ansible/ansible/issues/52158 # See https://github.com/ansible/ansible/issues/52158
# We do that only here because it is taken care of by to_text() in the else block below already. # We do that only here because it is taken care of by to_text() in the else block below already.
str(out) str(out)
# short circuit literal_eval when possible
if not isinstance(out, list):
return out
else: else:
if isinstance(nodes, types.GeneratorType): if isinstance(nodes, types.GeneratorType):
nodes = chain(head, nodes) nodes = chain(head, nodes)
@ -60,6 +58,10 @@ def ansible_native_concat(nodes):
out = u''.join([to_text(v) for v in nodes]) out = u''.join([to_text(v) for v in nodes])
try: try:
return literal_eval(out) out = literal_eval(out)
if PY2:
# ensure bytes are not returned back into Ansible from templating
out = container_to_text(out)
return out
except (ValueError, SyntaxError, MemoryError): except (ValueError, SyntaxError, MemoryError):
return out return out

@ -1,8 +0,0 @@
from ansible.module_utils._text import to_text
class FilterModule(object):
def filters(self):
return {
'to_text': to_text,
}

@ -1,9 +1,9 @@
- name: cast things to other things - name: cast things to other things
set_fact: set_fact:
int_to_str: "{{ i_two|to_text }}" int_to_str: "'{{ i_two }}'"
str_to_int: "{{ s_two|int }}" str_to_int: "{{ s_two|int }}"
dict_to_str: "{{ dict_one|to_text }}" dict_to_str: "'{{ dict_one }}'"
list_to_str: "{{ list_one|to_text }}" list_to_str: "'{{ list_one }}'"
int_to_bool: "{{ i_one|bool }}" int_to_bool: "{{ i_one|bool }}"
str_true_to_bool: "{{ s_true|bool }}" str_true_to_bool: "{{ s_true|bool }}"
str_false_to_bool: "{{ s_false|bool }}" str_false_to_bool: "{{ s_false|bool }}"

@ -18,7 +18,7 @@
- name: concatenate int and string - name: concatenate int and string
set_fact: set_fact:
string_sum: "{{ [(i_one|to_text), s_two]|join('') }}" string_sum: "'{{ [i_one, s_two]|join('') }}'"
- assert: - assert:
that: that:

@ -286,8 +286,6 @@ test/integration/targets/incidental_win_dsc/files/xTestDsc/1.0.1/DSCResources/AN
test/integration/targets/incidental_win_dsc/files/xTestDsc/1.0.1/xTestDsc.psd1 pslint!skip test/integration/targets/incidental_win_dsc/files/xTestDsc/1.0.1/xTestDsc.psd1 pslint!skip
test/integration/targets/incidental_win_ping/library/win_ping_syntax_error.ps1 pslint!skip test/integration/targets/incidental_win_ping/library/win_ping_syntax_error.ps1 pslint!skip
test/integration/targets/incidental_win_reboot/templates/post_reboot.ps1 pslint!skip test/integration/targets/incidental_win_reboot/templates/post_reboot.ps1 pslint!skip
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py future-import-boilerplate
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py metaclass-boilerplate
test/integration/targets/lookup_ini/lookup-8859-15.ini no-smart-quotes test/integration/targets/lookup_ini/lookup-8859-15.ini no-smart-quotes
test/integration/targets/module_precedence/lib_with_extension/ping.py future-import-boilerplate test/integration/targets/module_precedence/lib_with_extension/ping.py future-import-boilerplate
test/integration/targets/module_precedence/lib_with_extension/ping.py metaclass-boilerplate test/integration/targets/module_precedence/lib_with_extension/ping.py metaclass-boilerplate

Loading…
Cancel
Save