Conditions
Attach a condition to any step or action to decide at runtime whether it should run. A condition is a mustache expression that can reference outputs from earlier steps, workflow variables, or any other data in the execution context.
Using conditions, you can introduce decision-making into workflows by asserting values, thresholds, or specific states.
Simple if
condition
actions:
- name: notify-slack
if: "{{ alert.cpu_load }} == '70'"
provider:
type: slack
config: "{{ providers.slack }}"
with:
message: "The CPU load exceeded the threshold!"
Values of variables will be quoted when evaluated. For example, if
alert.cpu_load
is 70
, it will resolve to '70'
(number quoted with single
quotes).
Using results of other steps in condition
workflow:
id: query-and-alert
description: "Query a database and notify only if a threshold is met"
steps:
- name: get-disk-usage
provider:
type: mysql
config: "{{ providers.mysql-prod }}"
with:
query: "SELECT disk_usage FROM metrics WHERE server = 'db1'"
single_row: true
actions:
- name: notify-slack
if: "{{ steps.get-disk-usage.results.disk_usage }} > 90"
provider:
type: slack
config: "{{ providers.slack }}"
with:
message: "Disk usage is critical: {{ steps.get-disk-usage.results.disk_usage }}%"
Complex logic
actions:
- name: create-incident
if: "{{ steps.get-alert.results.severity }} == 'critical' and {{ steps.get-alert.results.source }} == 'datadog'"
provider:
type: servicenow
config: "{{ providers.servicenow }}"
with:
table_name: INCIDENT
payload:
short_description: "Critical Datadog alert received"
Condition with foreach
actions:
- name: process-pods
foreach: "{{ steps.get-pods.results }}"
if: "{{ foreach.value.status.phase }} == 'Failed'"
provider:
type: slack
with:
message: "Pod {{ foreach.value.metadata.name }} has failed!"
Condition with constants
consts:
max_load: 70
actions:
- name: process-pods
if: "{{ alert.cpu_load }} > {{ consts.max_load }}"
provider:
type: slack
with:
message: "Pod {{ foreach.value.metadata.name }} has failed!"
Explicit condition blocks (deprecated)
Explicit condition blocks are deprecated and will be discontinued. Use the
if
syntax instead.
assert (deprecated)
Checks whether a specific assertion is true.
condition:
- name: assert-condition
type: assert
assert: "{{ steps.get-data.results.value }} == 'expected'"
threshold (deprecated)
Compares a value to a threshold using operators like >
(gt) and <
(lt), defaults to >
(gt).
condition:
- name: threshold-condition
type: threshold
value: "{{ steps.get-data.results.value }}"
compare_to: 100
compare_type: gt