Foreach
Foreach syntax add the flexibility of running action per result instead of only once on all results.
Usage
There are two main operations mode for foreach
:
When you enter a foreach
context, you can use {{ foreach.value }}
to use the specific value.
Let’s review how to use foreach
.
Steps section
Using foreach
in steps
, let you run a step for each result of a previous step.
In other words:
- Run some step.
- For each result of the previous step, run another step.
For example, in this alert, we:
- Get all node id’s (
get-node-ids
step). - For each node, get the data for result id (
get-filesystems-by-node-id
step).
In this case, foreach.value
contains a row from the database, and foreach.value[0]
is the first column of this row.
Actions section
Now, let’s see how foreach
can be used in the actions
section.
In the following example, we are using foreach
twice:
foreach: "{{ steps.get-filesystems-by-node-id.results }}"
- iterate over the results ofget-filesystems-by-node-id
results{{#foreach.stddev}}
- using mustache syntax, we iterate overforeach.stddev
results.
Wait, but what’s foreach.stddev
?
tldr: conditions can extend
foreach
with other attributes, to support more context.
Due to the fact that conditions work on foreach.value
, we can extend foreach
with other attributes.
For example, the threshold
condition extends foreach
with level
, so you can use foreach.level
, and stddev
condition extends foreach
with stddev
attribute.