Defining a rule

StackStorm uses rules to run available actions when events occur. StackStorm comes with default actions and the catalog of actions can be increased by adding new actions from the community. Follow these steps to create your first rule:

  1. Rules are created in a familiar YAML format and consist of three sections: trigger, criteria, and action. Before we create the rule file, we will get familiar with the available triggers we can use in our rule. Use the following command to list the available triggers:
$ kubectl exec -it ${ST2CLIENT} -n stackstorm -- st2 trigger list
  1. Check the details of the webhook trigger. The following command will return the description, parameters, and payload schema for the trigger. Review parameters_schema since we will use this in our example rule later:
$ kubectl exec -it ${ST2CLIENT} -n stackstorm -- st2 trigger get core.st2.webhook
...
| parameters_schema | { |
| | "additionalProperties": false, |
| | "type": "object", |
| | "properties": { |
| | "url": { |
| | "required": true, |
| | "type": "string" |
...
  1. Use the following command to list the available actions:
$ kubectl exec -it ${ST2CLIENT} -n stackstorm -- st2 action list
  1. Check the details of the core.local action. This action executes an arbitrary Linux command on the localhost. The following command returns the parameters it can take, as follows:
$ kubectl exec -it ${ST2CLIENT} -n stackstorm -- st2 action get core.local
...
| parameters | { |
| | "cmd": { |
| | "required": true, |
| | "type": "string", |
| | "description": "Arbitrary Linux command to |
| | be executed on the local host." |
| | }, |
| | "sudo": { |
| | "immutable": true |
| | } |
| | } |
| metadata_file | actions/local.yaml |
...
  1. Let's use the preceding trigger and action in a rule, and set up a webhook to listen to the URL at https://{host}/api/v1/webhooks/sample using the following rule and create a first_rule.yaml file. Once you've done this, copy the file into the container. The action will be triggered when a POST request is made to this URL:
$ cat > first_rule.yaml <<EOF
name: "sample_rule_with_webhook"
pack: "examples"
description: "Sample rule dumping webhook payload to a file."
enabled: true
trigger:
type: "core.st2.webhook"
parameters:
url: "sample"
criteria:
trigger.body.name:
pattern: "st2"
type: "equals"
action:
ref: "core.local"
parameters:
cmd: "echo "{{trigger.body}}" >> ~/st2.webhook_sample.out ; sync"
EOF

With that, you've learned how to find and use available actions and triggers to construct a rule. Next, we will learn how to run it in StackStorm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.187.210