From 77d2008150b26a14911006d1c542612408dc5bc4 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 30 Jul 2018 15:44:38 -0500 Subject: [PATCH] Don't perform string to bool conversion in set_fact when jinja2 native types is enabled (#43425) * Don't perform string to bool conversion in set_fact when jinja2 native types is enabled. Fixes #42599 * Add tests for boolean conversions in set_fact --- lib/ansible/plugins/action/set_fact.py | 4 +++- test/integration/targets/set_fact/runme.sh | 4 ++++ .../targets/set_fact/set_fact_bool_conv.yml | 20 +++++++++++++++++++ .../set_fact_bool_conv_jinja2_native.yml | 20 +++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/integration/targets/set_fact/set_fact_bool_conv.yml create mode 100644 test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml diff --git a/lib/ansible/plugins/action/set_fact.py b/lib/ansible/plugins/action/set_fact.py index 167220fe0b..d7fe573c1a 100644 --- a/lib/ansible/plugins/action/set_fact.py +++ b/lib/ansible/plugins/action/set_fact.py @@ -23,6 +23,8 @@ from ansible.module_utils.parsing.convert_bool import boolean from ansible.plugins.action import ActionBase from ansible.utils.vars import isidentifier +import ansible.constants as C + class ActionModule(ActionBase): @@ -49,7 +51,7 @@ class ActionModule(ActionBase): "letters, numbers and underscores." % k) return result - if isinstance(v, string_types) and v.lower() in ('true', 'false', 'yes', 'no'): + if not C.DEFAULT_JINJA2_NATIVE and isinstance(v, string_types) and v.lower() in ('true', 'false', 'yes', 'no'): v = boolean(v, strict=False) facts[k] = v diff --git a/test/integration/targets/set_fact/runme.sh b/test/integration/targets/set_fact/runme.sh index b1cf5afb84..14bed98f6d 100755 --- a/test/integration/targets/set_fact/runme.sh +++ b/test/integration/targets/set_fact/runme.sh @@ -17,3 +17,7 @@ export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR ansible-playbook -i ../../inventory "$@" set_fact_cached_1.yml ansible-playbook -i ../../inventory "$@" set_fact_cached_2.yml ansible-playbook -i ../../inventory --flush-cache "$@" set_fact_no_cache.yml + +# Test boolean conversions in set_fact +ansible-playbook -v set_fact_bool_conv.yml +ANSIBLE_JINJA2_NATIVE=1 ansible-playbook -v set_fact_bool_conv_jinja2_native.yml diff --git a/test/integration/targets/set_fact/set_fact_bool_conv.yml b/test/integration/targets/set_fact/set_fact_bool_conv.yml new file mode 100644 index 0000000000..2e06d75c87 --- /dev/null +++ b/test/integration/targets/set_fact/set_fact_bool_conv.yml @@ -0,0 +1,20 @@ +- hosts: localhost + gather_facts: false + vars: + string_var: "no" + tasks: + - set_fact: + this_is_string: "yes" + this_is_not_string: yes + this_is_also_string: "{{ string_var }}" + this_is_another_string: !!str "{% set thing = '' + string_var + '' %}{{ thing }}" + this_is_more_strings: '{{ string_var + "" }}' + + - assert: + that: + - string_var == 'no' + - this_is_string == True + - this_is_not_string == True + - this_is_also_string == False + - this_is_another_string == False + - this_is_more_strings == False diff --git a/test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml b/test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml new file mode 100644 index 0000000000..4721f4374f --- /dev/null +++ b/test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml @@ -0,0 +1,20 @@ +- hosts: localhost + gather_facts: false + vars: + string_var: "no" + tasks: + - set_fact: + this_is_string: "yes" + this_is_not_string: yes + this_is_also_string: "{{ string_var }}" + this_is_another_string: !!str "{% set thing = '' + string_var + '' %}{{ thing }}" + this_is_more_strings: '{{ string_var + "" }}' + + - assert: + that: + - string_var == 'no' + - this_is_string == 'yes' + - this_is_not_string == True + - this_is_also_string == 'no' + - this_is_another_string == 'no' + - this_is_more_strings == 'no'