Send Slack Message Once ruled is run

(LuisN) #1

Whats the best practice to send a slack message IF the output of an executed command = “Server Done”

(Lindsay Hill) #2

Write a workflow that looks at the result of the first task. There’s examples of this in the examples directory.

(LuisN) #3

Which one in specific? Just want to narrow down the search. I have been using the rules in example as a good source.

(LuisN) #4

This looks like a good start,

---
  chain:
    -
      name: "task1"
      ref: "core.local"
      parameters:
          cmd: "echo 'Task 1'"
      on-success: "task2"
    -
      name: "task2"
      ref: "examples.local_command_runner_print_to_stdout_and_stderr"
      parameters:
          count: "{{ count }}"
          sleep_delay: "{{ sleep_delay }}"
      on-success: "task3"

There are ways to get output from cmd run task1 and check if word exist ?

(Lindsay Hill) #5

You should be looking at workflows (docs.stackstorm.com/workflows.html) rather than rules.

(Lindsay Hill) #6

See st2ci/st2_pkg_e2e_test.yaml at master · StackStorm/st2ci · GitHub as an example of checking for task success and a specific variable value

(LuisN) #7

Ok getting close, but my script gets permission denied and i tried all sorts of perms on files/folder. what user runs workflow cmds

(LuisN) #8

I guess ill use core.local_sudo instead to run. so going to the link you sent, each item is defined. but is the output of the output suppose to be in yaml for that to work?

(LuisN) #9

The action for chatops is where i fail now, im sure i have it wrong


    examples.mistral-snipeit-netbox:
        description: A basic workflow that runs two Linux commands (one in each task).
        type: direct
        output:
            stdout: <% $.stdout %>
        tasks:
            task1:
                action: core.local_sudo cmd="/home/test1/script.sh"
                publish:
                    stdout: <% task(task1).result.stdout %>
                    stderr: <% task(task1).result.stderr %>
                on-success: post_success_to_slack
            task2:
                action: core.local cmd="echo task2"
                publish:
                    stdout: <% task(task2).result.stdout %>
                    stderr: <% task(task2).result.stderr %>

                post_success_to_slack:
                action: chatops.post_message
                input:
                    channel: "#development"
                    message: "Works"```

I get
```| Valid | False                                                                                                      |
| Error | Invalid DSL: Additional properties are not allowed ('post_success_to_slack' was unexpected)```
(Lindsay Hill) #10

There are numerous issues with that workflow. No on-success condition for task2, post_success_to_slack is over-indented.

(Lindsay Hill) #11

There’s probably also some other issues going on, but those might just be related to your formatting of the post here.

(LuisN) #12

Trying this now

version: '2.0'

examples.mistral-snipeit-netbox:
  description: A basic workflow that runs two Linux commands (one in each task).
  type: direct
  output:
      stdout: <% $.stdout %>
  input:
    - channel
  tasks:
    task1:
      action: core.local_sudo cmd="/home/test1/script.sh"
      publish:
          stdout: <% task(task1).result.stdout %>
          stderr: <% task(task1).result.stderr %>
      on-success: post_success_to_slack

      post_success_to_slack:
        action: chatops.post_message
        input:
          channel: "{{ ctx('channel') }}"
          #channel: "#development"
          message: "Works"
    task2:
      action: core.local cmd="echo task2"
      publish:
          stdout: <% task(task2).result.stdout %>
          stderr: <% task(task2).result.stderr %>
      on-error: post_error_to_slack
      post_error_to_slack:
        action: chatops.post_message
        input:
          channel: "{{ ctx('channel') }}"
          #channel: "#development"
          message: "Fail"

Via the GUI , how do i pass the input? via context? if so how would it look { “#development” } or {channel:“development”} ?

(Lindsay Hill) #13

You still have formatting issues with that workflow.Indentation is still wrong.

Passing in parameters to a workflow is the same as passing them in to an action. Define them in the metadata file, register the workflow, and they appear as parameters when you select that Action on the Actions tab.

(LuisN) #14

After leaving it for a bit, and coming back with a fresh mind i was able to get the format correctly and i also used the mistral check workflow cmd . helped a lot to debug

if my output from said script is just a string, is there a format i should output in in order to extract each object within?

(Lindsay Hill) #15

You can do fancy regex stuff if you have to, but it will make your life easier if you can output a JSON blob.

(LuisN) #16

Awesome, thank you! @lhill you have been a lot of help