Better loop variable state handling
* Loops caused local scripts variables to be backed up then restored, which would have caused a set_var in a loop to have no effect. * Instead, we back up the state of the variable (existing or not, value) and either restore the original value or delete the variable if it didn't exist.
This commit is contained in:
parent
1b1c1281a2
commit
a5b1ac5c4c
1 changed files with 22 additions and 12 deletions
|
@ -220,9 +220,13 @@ class RcInstruction(abc.ABC):
|
||||||
if self._loop is None:
|
if self._loop is None:
|
||||||
self._display.vvvv("%s : running action %s" % (host_name, self._action))
|
self._display.vvvv("%s : running action %s" % (host_name, self._action))
|
||||||
return self.run_once(host_name, merged_vars, host_vars, script_vars)
|
return self.run_once(host_name, merged_vars, host_vars, script_vars)
|
||||||
loop_values = self.evaluate_loop(host_name, merged_vars)
|
# Save previous loop variable state
|
||||||
script_vars = script_vars.copy()
|
had_loop_var = self._loop_var in script_vars
|
||||||
for value in loop_values:
|
if had_loop_var:
|
||||||
|
old_loop_var = script_vars[self._loop_var]
|
||||||
|
try:
|
||||||
|
# Loop over all values
|
||||||
|
for value in self.evaluate_loop(host_name, merged_vars):
|
||||||
self._display.vvvv(
|
self._display.vvvv(
|
||||||
"%s : running action %s for item %s"
|
"%s : running action %s for item %s"
|
||||||
% (host_name, self._action, repr(value))
|
% (host_name, self._action, repr(value))
|
||||||
|
@ -232,6 +236,12 @@ class RcInstruction(abc.ABC):
|
||||||
if not self.run_once(host_name, merged_vars, host_vars, script_vars):
|
if not self.run_once(host_name, merged_vars, host_vars, script_vars):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
finally:
|
||||||
|
# Restore loop variable state
|
||||||
|
if had_loop_var:
|
||||||
|
script_vars[self._loop_var] = old_loop_var
|
||||||
|
else:
|
||||||
|
del script_vars[self._loop_var]
|
||||||
|
|
||||||
def run_once(self, host_name, merged_vars, host_vars, script_vars):
|
def run_once(self, host_name, merged_vars, host_vars, script_vars):
|
||||||
if self.evaluate_condition(host_name, merged_vars):
|
if self.evaluate_condition(host_name, merged_vars):
|
||||||
|
|
Loading…
Reference in a new issue