template module - render `None` as empty string (#85121)

* template module - render `None` as empty string

* Update changelogs/fragments/template-none.yml

Co-authored-by: Matt Davis <6775756+nitzmahone@users.noreply.github.com>

---------

Co-authored-by: Matt Davis <6775756+nitzmahone@users.noreply.github.com>
pull/85126/head
Matt Clay 7 months ago committed by GitHub
parent d33bedc48f
commit 4fe9606530
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- template action - Template files where the entire file's output renders as ``None`` are no longer emitted as the string "None", but instead render to an empty file as in previous releases.

@ -132,6 +132,9 @@ class ActionModule(ActionBase):
data_templar = self._templar.copy_with_new_env(searchpath=searchpath, available_variables=temp_vars)
resultant = data_templar.template(template_data, escape_backslashes=False, overrides=overrides)
if resultant is None:
resultant = ''
new_task = self._task.copy()
# mode is either the mode from task.args or the mode of the source file if the task.args
# mode == 'preserve'

@ -32,3 +32,7 @@
- lookup('template', 'dict.j2') is not mapping
- include_tasks: trim_blocks.yml
- name: Verify templates with no rendered content return `None`
assert:
that: "{{ lookup('template', 'none.j2') is none }}"

@ -0,0 +1 @@
{# a template with no content will render as `None` #}

@ -19,6 +19,15 @@
- set_fact:
output_dir: "{{ lookup('env', 'OUTPUT_DIR') }}"
- name: render a template which has no content
template:
src: none.j2
dest: '{{ output_dir }}/none.templated'
- name: verify a template with no content renders as an empty string
assert:
that: "{{ lookup('file', output_dir ~ '/none.templated') == '' }}"
- name: get default group
shell: id -gn
register: group

@ -0,0 +1 @@
{# a template with no content will render as `None` #}
Loading…
Cancel
Save