Add new loop_control.extended_allitems option (#75760)

* Add new `loop_control.extended_allitems` option. Fixes #75216

* Add test for extended_allitems

* docs code block fix
This commit is contained in:
Matt Martz
2022-06-16 08:56:13 -05:00
committed by GitHub
parent a90f666ab3
commit 18992b7947
5 changed files with 28 additions and 1 deletions
@@ -0,0 +1,3 @@
minor_changes:
- Loops - Add new ``loop_control.extended_allitems`` to allow users to disable tracking all loop items for each loop
(https://github.com/ansible/ansible/issues/75216)
@@ -452,6 +452,16 @@ Variable Description
.. note:: When using ``loop_control.extended`` more memory will be utilized on the control node. This is a result of ``ansible_loop.allitems`` containing a reference to the full loop data for every loop. When serializing the results for display in callback plugins within the main ansible process, these references may be dereferenced causing memory usage to increase.
.. versionadded:: 2.14
To disable the ``ansible_loop.allitems`` item, to reduce memory consumption, set ``loop_control.extended_allitems: no``.
::
loop_control:
extended: yes
extended_allitems: no
Accessing the name of your loop_var
-----------------------------------
.. versionadded:: 2.8
+3 -1
View File
@@ -283,6 +283,7 @@ class TaskExecutor:
index_var = templar.template(self._task.loop_control.index_var)
loop_pause = templar.template(self._task.loop_control.pause)
extended = templar.template(self._task.loop_control.extended)
extended_allitems = templar.template(self._task.loop_control.extended_allitems)
# This may be 'None',so it is templated below after we ensure a value and an item is assigned
label = self._task.loop_control.label
@@ -310,7 +311,6 @@ class TaskExecutor:
if extended:
task_vars['ansible_loop'] = {
'allitems': items,
'index': item_index + 1,
'index0': item_index,
'first': item_index == 0,
@@ -319,6 +319,8 @@ class TaskExecutor:
'revindex': items_len - item_index,
'revindex0': items_len - item_index - 1,
}
if extended_allitems:
task_vars['ansible_loop']['allitems'] = items
try:
task_vars['ansible_loop']['nextitem'] = items[item_index + 1]
except IndexError:
+1
View File
@@ -30,6 +30,7 @@ class LoopControl(FieldAttributeBase):
_label = FieldAttribute(isa='str')
_pause = FieldAttribute(isa='float', default=0)
_extended = FieldAttribute(isa='bool')
_extended_allitems = FieldAttribute(isa='bool', default=True)
def __init__(self):
super(LoopControl, self).__init__()
@@ -10,3 +10,14 @@
- third
loop_control:
extended: yes
- debug:
var: ansible_loop
loop:
- first
- second
- third
loop_control:
extended: yes
extended_allitems: no
failed_when: ansible_loop.allitems is defined