From 3add96909deeb104117202e1b4ea9715b01e9e4a Mon Sep 17 00:00:00 2001 From: Yadnesh Kulkarni Date: Fri, 20 Nov 2020 23:52:55 +0530 Subject: [PATCH] Fix parsing of values when using an empty string as key (#57132) (#72545) Signed-off-by: Yadnesh Kulkarni --- .../fragments/72545_fix_facts_value_empty_key.yml | 2 ++ lib/ansible/template/__init__.py | 2 +- test/integration/targets/set_fact/runme.sh | 3 +++ .../targets/set_fact/set_fact_empty_str_key.yml | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/72545_fix_facts_value_empty_key.yml create mode 100644 test/integration/targets/set_fact/set_fact_empty_str_key.yml diff --git a/changelogs/fragments/72545_fix_facts_value_empty_key.yml b/changelogs/fragments/72545_fix_facts_value_empty_key.yml new file mode 100644 index 00000000000..c7921c28456 --- /dev/null +++ b/changelogs/fragments/72545_fix_facts_value_empty_key.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix parsing of values when using empty string as a key (https://github.com/ansible/ansible/issues/57132) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index a5392d708fb..3bc074a3c98 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -797,7 +797,7 @@ class Templar: set to True, the given data will be wrapped as a jinja2 variable ('{{foo}}') before being sent through the template engine. ''' - static_vars = [''] if static_vars is None else static_vars + static_vars = [] if static_vars is None else static_vars # Don't template unsafe variables, just return them. if hasattr(variable, '__UNSAFE__'): diff --git a/test/integration/targets/set_fact/runme.sh b/test/integration/targets/set_fact/runme.sh index b658f594395..781894a0f03 100755 --- a/test/integration/targets/set_fact/runme.sh +++ b/test/integration/targets/set_fact/runme.sh @@ -28,3 +28,6 @@ ansible-playbook -i inventory --flush-cache "$@" set_fact_no_cache.yml # Test boolean conversions in set_fact ANSIBLE_JINJA2_NATIVE=0 ansible-playbook -v set_fact_bool_conv.yml ANSIBLE_JINJA2_NATIVE=1 ansible-playbook -v set_fact_bool_conv_jinja2_native.yml + +# Test parsing of values when using an empty string as a key +ansible-playbook -i inventory set_fact_empty_str_key.yml diff --git a/test/integration/targets/set_fact/set_fact_empty_str_key.yml b/test/integration/targets/set_fact/set_fact_empty_str_key.yml new file mode 100644 index 00000000000..286319017ec --- /dev/null +++ b/test/integration/targets/set_fact/set_fact_empty_str_key.yml @@ -0,0 +1,15 @@ +- name: Test set_fact for empty string as a key + hosts: testhost + gather_facts: no + vars: + value: 1 + tasks: + - name: Define fact with key as an empty string + set_fact: + foo: + "": "bar{{ value }}" + + - name: Verify the parsed value of the key + assert: + that: + - foo == {"":"bar1"}