Unicode in json payload?

(LuisN) #1

Is stackstorm inserting these characters?

{u’result’: [{u’switch’: {u’interface’: {u’type’: {u’ae’: u’ae1’, u’vlan_name’: u’testvlan’}, u’port’: u’ge-0/0/0’, u’description’: u’group1 - test1 - eth0’}, u’ip’: u’10.0.0.12’, u’name’: u’switch1’}, u’server’: {u’interface’: u’eth0’, u’name’: u’test1’, u’tenant’: u’group1’}}]}"

Making it difficult to extract the objects, is there a way to remove that?

(W Chan) #2

This is a byproduct of loading/dumping JSON using the json standard library. This is still a valid JSON object that is very much accessible.

(LuisN) #3

Sorry meant the u’ character, anyway to cycle through objects ?

(Lindsay Hill) #4

What do you mean by “cycle through the objects” ? You can reference individual elements with normal notation - e.g. result[0].switch.interface.type

You don’t need to explicitly include u when referencing an object.

How are you trying to access the contents of that list/dictionary? In a rule/workflow/action/alias? What elements are you trying to extract? What have you tried so far, and what results are you getting? What results do want to get?

(LuisN) #5

its a rule that sets off a workflow and wanted to use the results from the script. how do i reference u’result?

.u’result.u’switch ? etc

(LuisN) #6

Let me try to put some more info on this

tasks:
    task1:
        action: default.net-interface-connect
        input:
          lag_name: <% $.lag_name %>
          lag_url: <% $.lag_url %>
          switch_name: <% $.switch_name %>
          switch_port: <% $.switch_port %>
          switch_id: <% $.switch_id %>
          device_name: <% $.device_name %>
          device_interface: <% $.device_interface %>
        publish:
            #stdout: <% task(task1).result.stdout %>
            stdout: <% task(task1) %>
            stderr: <% task(task1) %>
        on-success: post_success_to_slack

    post_success_to_slack:
        action: chatops.post_message
        input:
           channel: <% $.channel %>
           #channel: "#development"
           message: <% $.stdout %>
#        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: <% $.channel %>
           #channel: "#development"
           message: Error, please check snipeit logs  <% $.stdout %>

now if i use stdout: <% task(task1) %>
i get the entire output of task1

IF i use stdout: <% task(task1).name %>
i get task1

if i use stdout: <% task(task1).state %> i get SUCCESS

now if i try .result it fails and dumps the entire stdout.

my result from task1 below

"state_info": null,
      "name": "task1",
      "workflow_name": "default.mistral-config-ae-net",
      "created_at": "2019-04-11 20:10:40",
      "updated_at": "2019-04-11 20:10:49",
      "workflow_execution_id": "82b95739-317b-4b26-b707-892e5d86dc15",
      "state": "SUCCESS",
      "result": {
        "failed": false,
        "stderr": "",
        "return_code": 0,
        "succeeded": true,
        "stdout": {
          "result": [
            {
              "switch": {
                "interface": {
                  "type": {
                    "ae": "ae1",
                    "vlan_name": "group5"
                  },
                  "port": "ge-0/0/0",
                  "description": "group1 - test1 - eth0"
                },
                "ip": "10.0.0.12",
                "name": "switch1"
              },
              "server": {
                "interface": "eth0",
                "name": "test1",
                "tenant": "group1"
              }
(Lindsay Hill) #7

What do you mean by it fails? The workflow fails with an error message, or you get more content published than you want?

Which field are you trying to extract? Start with the task result, see what that looks like, then progressively narrow it down. I’m not sure which field you’re trying to extract.

Something looks a little odd about this section:

"result": {
        "failed": false,
        "stderr": "",
        "return_code": 0,
        "succeeded": true,
        "stdout": {
          "result": [
            {
              "switch": {
                "interface": {
                  "type": {
                    "ae": "ae1",
                    "vlan_name": "group5"

Looks like you need to use something like result.stdout.result[0].switch. The formatting looks a bit odd though, I don’t know how you’ve ended up with result.stdout containing a result object. Seems like you’ve done something a bit screwy there.

(LuisN) #8

result.stdout.result[0] fails
the task result only appears with nothing
<% task(task1) %>
<% task(task1).result %>

this is .stderr

{u’state_info’: None, u’name’: u’task1’, u’created_at’: u’2019-04-11 22:16:22’, u’updated_at’: None, u’id’: u’f8d9738b-4454-4125-a13d-99fb3d3a1b2f’, u’workflow_execution_id’: u’1ac1e599-af25-4b97-b835-3ad23a2df6bd’, u’state’: u’SUCCESS’, u’result’: {u’failed’: False, u’stderr’: u’’, u’return_code’: 0, u’succeeded’: True, u’stdout’: {u’result’: [{u’switch’: {u’interface’: {u’type’: {u’ae’: u’ae1’, u’vlan_name’: u’vlangrp1’}, u’port’: u’ge-0/0/0’, u’description’: u’group1 - test1 - eth0’}, u’ip’: u’10.0.0.14’, u’name’: u’switch1’}, u’server’: {u’interface’: u’eth0’, u’name’: u’test1’, u’tenant’: u’group1’}}]}}, u’published’: {}, u’type’: u’ACTION’, u’spec’: {u’name’: u’task1’, u’on-success’: u’post_success_to_slack’, u’publish’: {u’stderr’: u’<% task(task1) %>’, u’stdout’: u’<% task(task1).result.stdout %>’}, u’version’: u’2.0’, u’action’: u’st2.action’, u’input’: {u’ref’: u’default.mistral-config-ae-net’, u’parameters’: {u’switch_port’: u’<% .switch_port %>', u'device_interface': u'<% .device_interface %>’, u’switch_id’: u’<% .switch_id %>', u'device_name': u'<% .device_name %>’, u’switch_name’: u’<% .switch_name %>', u'lag_name': u'<% .lag_name %>’, u’lag_url’: u’<% $.lag_url %>’}}, u’type’: u’direct’}}

(LuisN) #9

sorry format is awful

(Lindsay Hill) #10

What field are you trying to extract?

From the above, it looks like you’re assigning the wrong thing to stderr somewhere. That looks like a successful result from an action.

Maybe just go back to the start: What is the problem you’re trying to solve? Explain that, showing the overall workflow, the action you’re running, the output you’re getting from that, and what you’re trying to do with that output. I’m having trouble following the above snippets - they all seem a bit disconnected.

(LuisN) #11

=| (ok breath , lol) i posb just need to step away from it

let me try to explain

Webhook -> rule w/ criteria -> run action (workflow) task1 run action (local script) , get result and use referenced output to execute slack channel post.

(W Chan) #12

Looking at your workflow, I’m not clear. It fail at publish on task(task1).result or task(task1).result.stdout or both? Can you post the entire st2 execution get -j?

(LuisN) #13

i think stderr and stdout are both being triggered

(Lindsay Hill) #14

OK, but which bit is causing the problem? Sounds like it’s just within your workflow, and the web hook + rule are fine?

So if the problem is just within your workflow, what is your current workflow definition, what’s the result of the first task, and which parts of the result do you want to extract for the second task?

(LuisN) #15
{
    "action": {
        "ref": "default.mistral-config-ae-net"
    },
    "end_timestamp": "2019-04-11T22:55:24.906926Z",
    "id": "5cafc5d155fdfa04508c539b",
    "parameters": {
        "channel": "#development",
        "device_interface": "eth0",
        "device_name": "test1",
        "lag_name": "bond",
        "lag_url": "/api/dcim/interfaces/87/",
        "switch_id": "22",
        "switch_name": "switch1",
        "switch_port": "ge-0/0/0"
    },
    "result": {
        "failed": false,
        "return_code": 0,
        "stderr": "",
        "stdout": {
            "result": [
                {
                    "server": {
                        "interface": "eth0",
                        "name": "test1",
                        "tenant": "group1"
                    },
                    "switch": {
                        "interface": {
                            "description": "group1 - test1 - eth0",
                            "port": "ge-0/0/0",
                            "type": {
                                "ae": "ae1",
                                "vlan_name": "vlangrp1"
                            }
                        },
                        "ip": "10.0.0.12",
                        "name": "switch1"
                    }
                }
            ]
        },
        "succeeded": true
    },
    "result_task": "task1",
    "start_timestamp": "2019-04-11T22:55:13.194869Z",
    "status": "succeeded"
}
[
    {
        "action": "default.net-interface-connect",
        "id": "5cafc5d255fdfa071bc8d92a",
        "start_timestamp": "2019-04-11T22:55:14.287606Z",
        "status": "succeeded (8s elapsed)",
        "task": "task1"
    },
    {
        "action": "chatops.post_message",
        "id": "5cafc5db55fdfa071bc8d92c",
        "start_timestamp": "2019-04-11T22:55:23.615270Z",
        "status": "succeeded (1s elapsed)",
        "task": "post_error_to_slack"
    }
]
(W Chan) #16

Any error message to the task post_success_to_slack? The task post_error_to_slack was triggered by failure of post_success_to_slack.

(LuisN) #17

Right, thats where im getting at.

        publish:
            stdout: <% task(task1) %>
            stderr: <% task(task1) %>
        on-success: post_success_to_slack

    post_success_to_slack:
        action: chatops.post_message
        input:
           channel: <% $.channel %>
           message: <% $.stdout %>
        on-error: post_error_to_slack

    post_error_to_slack:
        action: chatops.post_message
        input:
           channel: <% $.channel %>
           message: Error, please check snipeit logs  <% $.stderr %>

if i leave this as such
stdout: <% task(task1) %>
stderr: <% task(task1) %>

then i get error posted to slack
if i do

            stdout: <% task(task1).name %>
            stderr: <% task(task1) %>

it will post task1
if i dig into the result , it will run error to slack

(W Chan) #18

What is the error then? Have you check the logs to see? From what I can tell, you’ve passed a JSON object to message of chats.post_message which requires string type. Of course post_success_to_slack will fail and run post_error_to_slack.

(LuisN) #19

Not sure which log you refer to, but i think you hit the nail on the head. so if i use .name for stdout , works fine bc it sends a string value.

if i have

           stderr: <% task(task1).result.stderr %>

since there is a unicode string inside it always fails (confirm?) {u’failed’: False, u’stderr’: u’’,

gotta figure out how to remove those u’ from my bash script

(W Chan) #20

Not sure why you are so insistent on removing the unicode label. They are still strings. Anyway, the problem here seems to be in your WF passing data with wrong type to a parameter that expects string type. My advice is to remove post_error_to_slack for now until you’re able to figure where your problem is.