@@ -423,6 +423,65 @@ and intermediate nested or indirected templated results are cached for the durat
423
423
reducing repetitive templating.
424
424
These changes have shown exponential performance improvements for many real-world complex templating scenarios.
425
425
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') }}"
426
485
427
486
Error handling
428
487
--------------
0 commit comments