I need help wrapping my head around how the while loop in Calm works. The documentation is unfortunately not helpful at all. :/
The first thing I can’t understand is why a while loop has a fixed number of iterations. This to me is a for loop. A while loop runs while a condition isn’t met.
The second thing that’s confusing is the exit condition that I can set to Success, Failure, or Don’t Care. I guess that Don’t Care turns the while loop into an actual for loop?
The Success or Failure condition is as far as I can tell completely out of my hands as this is based on the result of the execution of the task(s) within the loop and not a parameter I control. Do I have to fail a task on purpose to keep the loop going if I choose Success as my exit condition?
What happens if I have multiple tasks in the loop, do all tasks need to fail or succeed for the exit condition to trigger?
Is it possible at all to set a parameter in one of my scripts and then use that as an exit condition?
Thanks.
Page 1 / 1
Hi Martin,
In most programming languages, While & For loop are usually convertible. If a variable initialisation, break condition and variable update mechanisms are given together, it constitutes a For loop and if just the break condition is present in the loop, it is a While loop.
But, our implementation is inspired from a pythonic way of defining this, where For loop iterates over an iterable, such as a list, set, or a dictionary, whereas a While loop contains a condition.
You can consider our While loop to be of the following implementation.
iteration = 0
max_iteration = n
can_continue = true
while (iteration < max_iteration) or can_continue:
can_continue = loop_body()
iteration += 1
here, the can_continue can be manipulated by the task that the loop is executing and currently, can_continue can be manipulated by the status of the loop_body as you’ve found out.
Following is an example escript where I achieved the same.
This while loop exited on the 7th iteration when the task succeeded.
Please let me know if you need more details.
Hi Srinath.
Thanks for the explanation.
I’m not familiar with Python so I can’t say if this is a “valid” while loop in Python, but as someone who grew up coding C and C++ I’d say that using a for loop as a while loop isn’t exactly considered best practices. :)
Your implementation still seems like an unnecessarily complicated way of doing a loop as it forces me to specify a maximum number of iterations when I might not know how many iterations that I require.
Relying on the exit status of the script - and I still don’t know which or all of the scripts that triggers an exit condition - is extremely crude as I might use my exit codes for other purposes.
Why not have a proper while loop that monitors a parameter I set in anyone of the scripts in the loop? Calm reads the output of the “Set variable” types of scripts as is so why not use that as the condition?
For the time being we’ll be coding loops directly in the scripts, which is a shame as we’d very much like to use the UI of Calm to illustrate the logic of a runbook, without having to open up a script.
Hi Martin,
I’d suggest using the Nutanix Support portal you raise a RFE to help improving this FEAT to make it more user friendly.