From 094a0746b31f7ce9879dfa6fb2972930eb1cbab2 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Fri, 7 Jan 2022 17:13:34 +0100 Subject: [PATCH] ansible_concat: return strings only unless eval (#76634) Fixes #76610 --- changelogs/fragments/76610-empty-template-none.yml | 2 ++ lib/ansible/template/native_helpers.py | 2 +- test/integration/targets/template/tasks/main.yml | 10 ++++++++++ .../targets/template/templates/empty_template.j2 | 0 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/76610-empty-template-none.yml create mode 100644 test/integration/targets/template/templates/empty_template.j2 diff --git a/changelogs/fragments/76610-empty-template-none.yml b/changelogs/fragments/76610-empty-template-none.yml new file mode 100644 index 00000000000..a69d5114881 --- /dev/null +++ b/changelogs/fragments/76610-empty-template-none.yml @@ -0,0 +1,2 @@ +bugfixes: + - "Fix for when templating empty template file resulted in file with string 'None' (https://github.com/ansible/ansible/issues/76610)" diff --git a/lib/ansible/template/native_helpers.py b/lib/ansible/template/native_helpers.py index c75d356b38a..79091d0c9a9 100644 --- a/lib/ansible/template/native_helpers.py +++ b/lib/ansible/template/native_helpers.py @@ -60,7 +60,7 @@ def ansible_concat(nodes, convert_data, variable_start_string): head = list(islice(nodes, 2)) if not head: - return None + return '' if len(head) == 1: out = _fail_on_undefined(head[0]) diff --git a/test/integration/targets/template/tasks/main.yml b/test/integration/targets/template/tasks/main.yml index d97aa4d550d..d07a605f771 100644 --- a/test/integration/targets/template/tasks/main.yml +++ b/test/integration/targets/template/tasks/main.yml @@ -756,3 +756,13 @@ - assert: that: - out.rc == 0 + +- template: + src: empty_template.j2 + dest: "{{ output_dir }}/empty_template.templated" + +- assert: + that: + - test + vars: + test: "{{ lookup('file', '{{ output_dir }}/empty_template.templated')|length == 0 }}" diff --git a/test/integration/targets/template/templates/empty_template.j2 b/test/integration/targets/template/templates/empty_template.j2 new file mode 100644 index 00000000000..e69de29bb2d