Skip to content

Commit e6fcdc8

Browse files
patchback[bot]felixfonteinsamccann
authored
Add example of unintentional None to 2.19 porting guide (#2982) (#3013)
* Add example of unintentional None to 2.19 porting guide. * Rewrite into two sections. * Improve formulations as suggested by samccann. * Remove no longer appropriate section. * Fix typo and add note. --------- (cherry picked from commit 87d2e26) Co-authored-by: Felix Fontein <[email protected]> Co-authored-by: Sandra McCann <[email protected]>
1 parent 78b63bf commit e6fcdc8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/docsite/rst/porting_guides/porting_guide_core_2.19.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,41 @@ The result of the corrected template remains a list:
407407
}
408408
409409
410+
Example - unintentional ``None`` result
411+
"""""""""""""""""""""""""""""""""""""""
412+
413+
If 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``.
415+
416+
The following example shows a case 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+
This example can be fixed as follows:
427+
428+
.. code-block:: yaml+jinja
429+
430+
- set_fact:
431+
# Explicitly return an empty string in the 'else' branch.
432+
# The value is always a string: either "foo is defined" or "".
433+
value_none: |-
434+
{% if foo is defined %}foo is defined{% else %}{{ "" }}{% endif %}
435+
436+
This adjustment is backward-compatible with older ansible-core versions.
437+
438+
.. note::
439+
Since ansible-core 2.19.1, module options of type string accept ``None`` and convert it
440+
to an empty string. Before ansible-core 2.18, passing ``None`` to such options resulted
441+
in an error. This means that in most cases, expressions in roles and playbooks do not need
442+
to be adjusted because of unintentional ``None`` results.
443+
444+
410445
Lazy templating
411446
^^^^^^^^^^^^^^^
412447

0 commit comments

Comments
 (0)