This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.

⚠️ We've moved!

Hi there!

To reduce project dependency on 3rd party paid services the StackStorm TSC has decided to move the Q/A from this forum to Github Discussions. This will make user experience better integrated with the native Github flow, as well as the questions closer to the community where they can provide answers.

Use 🔗 Github Discussions to ask your questions.

Pass variable to YAQL query

I would like to publish data based on input from a variable in the context using a YAQL expression

something along the lines

vars:
   - myvarible: test

tasks:
  task1:
    action: myaction
    next:
      - when: <% succeeded() %>
         publish: 
           - service: <% result().result.<myvariable> %> 

Is it possible to somehow pass a variable from ctx into a YAQL expression ? or are there any other techniques to query data based on a variable input ?

workflow variables are available in the workflow context as soon as the workflow is executed so you don’t need to publish it, just use it in the YAQL expression. ctx(myvariable)

Thanks for the quick response. I don’t fully understand and perhaps I am missing something, so let me expand

I have a key in the datastore containing some configuration data and I would like to extract the relevant parts of this after I have obtained it from the datastore.

The data in the store looks like this

{
 "homebridge": {
     "compute_size": "Medium Instance",
    "name": "homebridge",
    "os": "debian-10.0",
    "service": "homebridge"
},
"plexmedia": {
    "compute_size": "Medium Instance",
    "name": "plexmedia",
    "os": "debian-10.0",
    "service": "plexmedia"
  }
}

And my code looks like this

version: 1.0

description: Stackstorm Test Action
input:
 - terraform_vars
vars:
  - var_config: null
  - var_service: homebridge

tasks:
  task1:
    action: st2.kv.get_object
    input:
      key: cloudstack_config
    next:
      - when: <% succeeded() %>
        publish:
          - var_config: <% result().result.homebridge %>
output:
  - vars: <% ctx(var_config) %>

I want to be able to make the selection of the query based on the variable var_service

<% result().result.homebridge %>

Is this possible ?

What you’re attempting to achieve is possible and quite a common use case.

The data store can be access directly from the workflow using the <% st2kv() %> function. You can read about it here StackStorm Runtime — StackStorm 3.3.0 documentation. You only need to use the st2 pack when you want to write to the kvstore.

I believe this should do it.

publish:
      - var_config: <% result().result.get(ctx().var_service) %>

That’s the solution, appreciate the help !