Skip to content

Commit 6a16f24

Browse files
flowerysongpatchback[bot]
authored andcommitted
porting guide 2.19: document range() changes (#2832)
Signed-off-by: Paul Arthur <[email protected]> (cherry picked from commit 0ff0517)
1 parent 97e543c commit 6a16f24

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,65 @@ and intermediate nested or indirected templated results are cached for the durat
423423
reducing repetitive templating.
424424
These changes have shown exponential performance improvements for many real-world complex templating scenarios.
425425

426+
Consistent handling of range
427+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
428+
429+
The result of using the Jinja global function ``range()`` was heavily dependent on the context in which it was used and
430+
whether Jinja's native mode was enabled.
431+
To preserve the ability to use very large ranges in filter chains the result is now always a range object, which means
432+
it cannot be returned from a template unless you convert it to a returnable type.
433+
434+
Example - intentional list conversion
435+
"""""""""""""""""""""""""""""""""""""
436+
437+
.. code-block:: yaml+jinja
438+
439+
- debug:
440+
loop: "{{ range(0, 2) }}"
441+
442+
Ranges not embedded in containers would usually be converted to lists during template finalization.
443+
They will now result in this error:
444+
445+
.. code-block:: text
446+
447+
Error rendering template: Type 'range' is unsupported for variable storage.
448+
449+
450+
This can be resolved by making the conversion explicit:
451+
452+
.. code-block:: yaml+jinja
453+
454+
- debug:
455+
loop: "{{ range(0, 2) | list }}"
456+
457+
458+
Example - unintentional string conversion
459+
"""""""""""""""""""""""""""""""""""""""""
460+
461+
.. code-block:: yaml+jinja
462+
463+
- debug:
464+
msg: "{{ [range(0,2), range(7,10)] }}"
465+
466+
467+
Ranges embedded in containers would usually be converted to string representations of the range object.
468+
469+
.. code-block:: ansible-output
470+
471+
ok: [localhost] => {
472+
"msg": "[range(0, 2), range(7, 10)]"
473+
}
474+
475+
Attempting to do this will now result in an error; you can mimic the old behaviour by explicitly converting the container
476+
to a string, or convert the ranges to lists if you actually want to do something useful with them.
477+
478+
.. code-block:: yaml+jinja
479+
480+
- debug:
481+
msg: "{{ [range(0,2), range(7,10)] | string }}"
482+
483+
- debug:
484+
msg: "{{ [range(0,2), range(7,10)] | map('list') }}"
426485

427486
Error handling
428487
--------------

0 commit comments

Comments
 (0)