You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docsite/rst/porting_guides/porting_guide_core_2.19.rst
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -407,6 +407,53 @@ The result of the corrected template remains a list:
407
407
}
408
408
409
409
410
+
Example - unintentional ``None``
411
+
""""""""""""""""""""""""""""""""
412
+
413
+
If a part of a template evaluated to ``None``, it was implicitly converted to an empty string in previous versions of ansible-core.
414
+
This can now result in the template evaluating to the *value* ``None``, or can result in the templated string containing the character sequence ``None``.
415
+
416
+
The following example shows two cases where this happens:
417
+
418
+
.. code-block:: yaml+jinja
419
+
420
+
- set_fact:
421
+
# If 'foo' is not defined, the else branch basically evaluates to None.
422
+
# So value_none will not be an empty string, but None:
423
+
value_none: |-
424
+
{% if foo is defined %}foo is defined{% endif %}
425
+
426
+
# The expression 'items.append(x)' evaluates to None, so the resulting string
427
+
# will be "NoneNoneNone['a', 'b', 'c']" instead of the array ['a', 'b', 'c']:
428
+
string_with_none: |-
429
+
{% set items = [] %}
430
+
{% for x in ['a', 'b', 'c'] %}
431
+
{{- items.append(x) -}}
432
+
{% endfor %}
433
+
{{ items }}
434
+
435
+
These examples can be fixed as follows:
436
+
437
+
.. code-block:: yaml+jinja
438
+
439
+
- set_fact:
440
+
# Explicitly return an empty string in the 'else' branch.
441
+
# The value is always a string: either "foo is defined" or "".
442
+
value_none: |-
443
+
{% if foo is defined %}foo is defined{% else %}{{ "" }}{% endif %}
444
+
445
+
# Use {% set _ = expression %} instead of {{ expression }} eats the
446
+
# return value. The template evalutes to the array ['a', 'b', 'c'].
447
+
string_with_none: |-
448
+
{% set items = [] %}
449
+
{% for x in ['a', 'b', 'c'] %}
450
+
{%- set _ = items.append(x) -%}
451
+
{% endfor %}
452
+
{{ items }}
453
+
454
+
These adjustments also work fine with older ansible-core versions.
0 commit comments