post_validate LoopControl (#75715)

avoid having to template/post_validate each part of the object
pull/78771/head
Martin Krizek 2 years ago committed by GitHub
parent 540442db2e
commit 8090ee3e78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- "``LoopControl`` is now templated through standard ``post_validate`` method (https://github.com/ansible/ansible/pull/75715)"

@ -262,35 +262,18 @@ class TaskExecutor:
into an array named 'results' which is inserted into the final result
along with the item for which the loop ran.
'''
results = []
# make copies of the job vars and task so we can add the item to
# the variables and re-validate the task with the item variable
# task_vars = self._job_vars.copy()
task_vars = self._job_vars
templar = Templar(loader=self._loader, variables=task_vars)
loop_var = 'item'
index_var = None
label = None
loop_pause = 0
extended = False
templar = Templar(loader=self._loader, variables=self._job_vars)
# FIXME: move this to the object itself to allow post_validate to take care of templating (loop_control.post_validate)
if self._task.loop_control:
loop_var = templar.template(self._task.loop_control.loop_var)
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
self._task.loop_control.post_validate(templar=templar)
loop_var = self._task.loop_control.loop_var
index_var = self._task.loop_control.index_var
loop_pause = self._task.loop_control.pause
extended = self._task.loop_control.extended
extended_allitems = self._task.loop_control.extended_allitems
# ensure we always have a label
if label is None:
label = '{{' + loop_var + '}}'
label = self._task.loop_control.label or '{{' + loop_var + '}}'
if loop_var in task_vars:
display.warning(u"%s: The loop variable '%s' is already in use. "
@ -298,9 +281,9 @@ class TaskExecutor:
u" to something else to avoid variable collisions and unexpected behavior." % (self._task, loop_var))
ran_once = False
no_log = False
items_len = len(items)
results = []
for item_index, item in enumerate(items):
task_vars['ansible_loop_var'] = loop_var
@ -333,10 +316,7 @@ class TaskExecutor:
# pause between loop iterations
if loop_pause and ran_once:
try:
time.sleep(float(loop_pause))
except ValueError as e:
raise AnsibleError('Invalid pause value: %s, produced error: %s' % (loop_pause, to_native(e)))
time.sleep(loop_pause)
else:
ran_once = True

@ -25,12 +25,12 @@ from ansible.playbook.base import FieldAttributeBase
class LoopControl(FieldAttributeBase):
loop_var = FieldAttribute(isa='str', default='item')
index_var = FieldAttribute(isa='str')
loop_var = FieldAttribute(isa='str', default='item', always_post_validate=True)
index_var = FieldAttribute(isa='str', always_post_validate=True)
label = FieldAttribute(isa='str')
pause = FieldAttribute(isa='float', default=0)
extended = FieldAttribute(isa='bool')
extended_allitems = FieldAttribute(isa='bool', default=True)
pause = FieldAttribute(isa='float', default=0, always_post_validate=True)
extended = FieldAttribute(isa='bool', always_post_validate=True)
extended_allitems = FieldAttribute(isa='bool', default=True, always_post_validate=True)
def __init__(self):
super(LoopControl, self).__init__()

@ -76,7 +76,7 @@ class Task(Base, Conditional, Taggable, CollectionSearch):
delegate_facts = FieldAttribute(isa='bool')
failed_when = FieldAttribute(isa='list', default=list)
loop = FieldAttribute()
loop_control = NonInheritableFieldAttribute(isa='class', class_type=LoopControl)
loop_control = NonInheritableFieldAttribute(isa='class', class_type=LoopControl, default=LoopControl)
notify = FieldAttribute(isa='list')
poll = FieldAttribute(isa='int', default=C.DEFAULT_POLL_INTERVAL)
register = FieldAttribute(isa='string', static=True)

Loading…
Cancel
Save