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
pull/78068/head
Matt Martz 4 years ago committed by GitHub
parent a90f666ab3
commit 18992b7947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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. .. 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 Accessing the name of your loop_var
----------------------------------- -----------------------------------
.. versionadded:: 2.8 .. versionadded:: 2.8

@ -283,6 +283,7 @@ class TaskExecutor:
index_var = templar.template(self._task.loop_control.index_var) index_var = templar.template(self._task.loop_control.index_var)
loop_pause = templar.template(self._task.loop_control.pause) loop_pause = templar.template(self._task.loop_control.pause)
extended = templar.template(self._task.loop_control.extended) 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 # 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 label = self._task.loop_control.label
@ -310,7 +311,6 @@ class TaskExecutor:
if extended: if extended:
task_vars['ansible_loop'] = { task_vars['ansible_loop'] = {
'allitems': items,
'index': item_index + 1, 'index': item_index + 1,
'index0': item_index, 'index0': item_index,
'first': item_index == 0, 'first': item_index == 0,
@ -319,6 +319,8 @@ class TaskExecutor:
'revindex': items_len - item_index, 'revindex': items_len - item_index,
'revindex0': items_len - item_index - 1, 'revindex0': items_len - item_index - 1,
} }
if extended_allitems:
task_vars['ansible_loop']['allitems'] = items
try: try:
task_vars['ansible_loop']['nextitem'] = items[item_index + 1] task_vars['ansible_loop']['nextitem'] = items[item_index + 1]
except IndexError: except IndexError:

@ -30,6 +30,7 @@ class LoopControl(FieldAttributeBase):
_label = FieldAttribute(isa='str') _label = FieldAttribute(isa='str')
_pause = FieldAttribute(isa='float', default=0) _pause = FieldAttribute(isa='float', default=0)
_extended = FieldAttribute(isa='bool') _extended = FieldAttribute(isa='bool')
_extended_allitems = FieldAttribute(isa='bool', default=True)
def __init__(self): def __init__(self):
super(LoopControl, self).__init__() super(LoopControl, self).__init__()

@ -10,3 +10,14 @@
- third - third
loop_control: loop_control:
extended: yes extended: yes
- debug:
var: ansible_loop
loop:
- first
- second
- third
loop_control:
extended: yes
extended_allitems: no
failed_when: ansible_loop.allitems is defined

Loading…
Cancel
Save