Skip to content

Commit 29f354d

Browse files
committed
Add example for unintentional string conversion
1 parent ad0ec65 commit 29f354d

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Diff for: docs/docsite/rst/porting_guides/porting_guide_core_2.19.rst

+39-1
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,50 @@ In both modes, ansible-core evaluated template string results as Python literals
7777
Selection of the templating mode was controlled by configuration, defaulting to Jinja's original string templating.
7878

7979
Starting with this release, use of Jinja's native templating is required.
80+
The configuration option for setting the templating mode is deprecated and no longer has any effect.
81+
8082
Preservation of native types in templating has been improved to correct gaps in the previous implementation,
8183
entirely eliminating the final literal evaluation pass (a frequent source of confusion, errors, and performance issues).
8284
In rare cases where playbooks relied on implicit object conversion from strings, an explicit conversion will be
8385
required.
8486

85-
The configuration option for setting the templating mode is deprecated and no longer has any effect.
87+
Some existing templates may unintentionally convert non-strings to strings.
88+
In previous versions this was sometimes masked by the evaluation of strings as Python literals.
89+
90+
Example - Unintentional String Conversion
91+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92+
93+
This expression erroneously passes a list to the ``replace`` filter, which operates only on strings.
94+
The filter silently converts the list input to a string.
95+
Due to some string results previously parsing as lists, this mistake often went undetected in earlier versions.
96+
97+
.. code-block:: yaml+jinja
98+
99+
- debug:
100+
msg: "{{ ['test1', 'test2'] | replace('test', 'prod') }}"
101+
102+
The result of this template becomes a string::
103+
104+
ok: [localhost] => {
105+
"msg": "['prod1', 'prod2']"
106+
}
107+
108+
109+
This can be resolved by using the ``map`` filter to apply the ``replace`` filter to each list element:
110+
111+
.. code-block:: yaml+jinja
112+
113+
- debug:
114+
msg: "{{ ['test1', 'test2'] | map('replace', 'test', 'prod') }}"
115+
116+
The result of the corrected template remains a list::
117+
118+
ok: [localhost] => {
119+
"msg": [
120+
"prod1",
121+
"prod2"
122+
]
123+
}
86124

87125

88126
Lazy Templating

0 commit comments

Comments
 (0)