StackStorm / st2

StackStorm (aka "IFTTT for Ops") is event-driven automation for auto-remediation, incident responses, troubleshooting, deployments, and more for DevOps and SREs. Includes rules engine, workflow, 160 integration packs with 6000+ actions (see https://exchange.stackstorm.org) and ChatOps. Installer at https://docs.stackstorm.com/install/index.html
https://stackstorm.com/
Apache License 2.0
6.07k stars 747 forks source link

Json definition in core.remote action command #5136

Closed pavanfhw closed 3 years ago

pavanfhw commented 3 years ago

SUMMARY

I need to insert a workflow parameter inside a Json definition for my action. The first problem is that I can't even run the command with a Json definition in it (withou the parameter in the Json).

input:
  - parameter

tasks:
  run:
    action: core.remote
    input:
      cmd: command --json= '{"key1": "someValeu", "key": "secondValue" }'

This results in the error:

result: 
  errors:
  - message: "mapping values are not allowed here
  in "<unicode string>", line 16, column 78:
     ... command --json= '{ "key1": "someValeu", "key2": "secondValue" } 
                                  ^"

The second problem is, assuming I can resolve the first one, as far as I saw in the examples and tested, <% ctx().parameter %> cannot be used inside single quotes, it only works when used inside double quotes. But the Json only works if I define it using single quotes. In the end I would like to have the workflow:

input:
  - parameter

tasks:
  run:
    action: core.remote
    input:
      cmd: command --json= '{"key1": "someValeu", "key2": <% ctx().parameter %> }'

STACKSTORM VERSION

st2 3.4dev (c422f0029), on Python 3.6.9

OS, environment, install method

Running on Kubenetes on k3os system. Deployed using stackstorm-ha

Expected Results

Is there a way to get around these problems?

amanda11 commented 3 years ago

I tried something similar on a standalone install with a core.local command to run a curl to invoke a ST2 command (which needed a JSON).

  task1:
    action: core.local
    input:
      cmd: "curl -X POST -H 'Accept-Encoding: gzip, deflate' -H  'Accept: */*' -H  'Connection: keep-alive' -H  'X-Auth-Token: token...' -H  'content-type: application/json' -H  'Content-Length: 137' --data-binary '{\"action\": \"core.local\", \"cmd\": \"echo ponies\"}, \"user\": null}' http://127.0.0.1:9101/v1/executions"

So I'm using " around the whole cmd, and then escaping any " within. Then when it comes to the data-binary I am using '.

So this should mean I think for your first example you could use:

    input:
      cmd: "command --json= '{\"key1\": \"someValeu\", \"key\": \"secondValue\" }' "
amanda11 commented 3 years ago

I also confirmed that with the above example I could then also put in variables into the command, e.g. ''' cmd: "curl -X POST -H 'Accept-Encoding: gzip, deflate' -H 'Accept: /' -H 'Connection: keep-alive' -H 'X-Auth-Token: token...' -H 'content-type: application/json' -H 'Content-Length: 137' --data-binary '{\"action\": \"core.local\", \"cmd\": \"echo ponies<% ctx().key %>\"}, \"user\": null}' http://127.0.0.1:9101/v1/executions" ''' and it then replaced <% ctx().key %> with its variable (where key was in this case a parameter to the workflow).

Could you try using the above syntax with the " after the cmd:" and the extra escapes on the ", and see if that resolves your problem.

pavanfhw commented 3 years ago

@amanda11 Using your syntax everything worked as I wanted. Thank you for the solution!