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.

Passing yaml list(dict) to alias

I have a task that for now publishes a variable (interface_field) that is a dict within a list

        get_device_interface_info:
            action: mypack.get_device_interface_info hostname=<% $.hostname %>
            publish:
                interface_stdout: <% task(get_device_interface_info).result.stdout  %>
                interface_field: <% list(dict('title'=> server1, 'value'=> test1, 'short'=> True)) %>

During the task I see the output as such

        "interface_field": [
          {
            "short": "True",
            "value": "test1",
            "title": "server1"
          }
        ]

But once that is called inside the alias (msg being sent to chatops/slack)

          fields: "{{ execution.result.interface_field }}"

the output is not the same

            "fields": "[{u'short': u'True', u'value': u'test1', u'title': u'server1'}]",

The reason I am doing this is because I can’t create a for loop to create each “fields” as this is the format being sent to slack

    "extra": {
      "slack": {
        "attachments": [
          {
            "color": "#00AA00",
            "text": "Host Details:",
            "fields": [
              {
                "short": true,
                "value": "test",
                "title": "Hostname"
              }

Have searched and tried many diff variations

Probably needs something like to_json_string in there, or to_yaml_string

Tried and got the following errors

used fields: "{{ execution.result.interface_field | to_json_string }}"

  "output": null,
  "errors": [
    {
      "message": "Execution failed. See result for details.",
      "type": "error",
      "result": {
        "exit_code": 1,
        "result": "None",
        "stderr": "Traceback (most recent call last):
  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/python_runner/python_action_wrapper.py\", line 334, in <module>
    obj.run()
  File \"/opt/stackstorm/st2/local/lib/python2.7/site-packages/python_runner/python_action_wrapper.py\", line 193, in run
    output = action.run(**self._parameters)
  File \"/opt/stackstorm/packs/chatops/actions/format_execution_result.py\", line 47, in run
    result['extra'] = jinja_utils.render_values(alias.result['extra'], context)
  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/st2common/util/jinja.py\", line 170, in render_values
    rendered_v = json.loads(rendered_v)
  File \"/usr/lib/python2.7/json/__init__.py\", line 339, in loads
    return _default_decoder.decode(s)
  File \"/usr/lib/python2.7/json/decoder.py\", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File \"/usr/lib/python2.7/json/decoder.py\", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 511 (char 510)
",
        "stdout": ""
      },
      "task_id": "format_execution_result"
    },
    {
      "message": "JinjaEvaluationException: Unable to evaluate expression '{{ result().result.enabled == true }}'. UndefinedError: 'unicode object' has no attribute 'enabled'",
      "task_transition_id": "post_message__t0",
      "type": "error",
      "route": 0,
      "task_id": "format_execution_result"
    },
    {
      "message": "JinjaEvaluationException: Unable to evaluate expression '{{ result().result.enabled == false }}'. UndefinedError: 'unicode object' has no attribute 'enabled'",
      "task_transition_id": "result_disabled__t0",
      "type": "error",
      "route": 0,
      "task_id": "format_execution_result"
    },
    {
      "message": "JinjaEvaluationException: Unable to evaluate expression '{{ ctx('msg') }}'. VariableUndefinedError: The variable \"msg\" is undefined.",
      "type": "error"
    }
  ]
}```

Any thoughts? still stuck

I suspect a bug in ST2. @LuisN: If you provide the entire action alias YAML, and the full output from the get_device_interface_info task, I can work on reproducing this next week.

Thank you @blag
below is the output, the end goal is to create the for loop from the info to create the needed info/structure needed for “field” for slack

{
  "succeeded": true,
  "failed": false,
  "return_code": 0,
  "stderr": "",
  "stdout": {
    "count": 2,
    "next": null,
    "results": [
      {
        "connected_endpoint_type": "dcim.interface",
        "lag": null,
        "connection_status": {
          "value": true,
          "label": "Connected"
        },
        "name": "eth0",
        "cable": {
          "url": "http://devserver1/api/dcim/cables/49/",
          "id": 49,
          "label": ""
        },
        "connected_endpoint": {
          "connection_status": {
            "value": true,
            "label": "Connected"
          },
          "name": "xe-0/0/2",
          "cable": 49,
          "url": "http://devserver1/api/dcim/interfaces/92/",
          "device": {
            "url": "http://devserver1/api/dcim/devices/23/",
            "display_name": "server2-42",
            "id": 23,
            "name": "server2-42"
          },
          "id": 92
        },
        "mgmt_only": false,
        "type": {
          "value": 1200,
          "label": "SFP+ (10GE)"
        },
        "tagged_vlans": [],
        "enabled": true,
        "untagged_vlan": null,
        "mtu": null,
        "device": {
          "url": "http://devserver1/api/dcim/devices/29/",
          "display_name": "test",
          "id": 29,
          "name": "test"
        },
        "mode": null,
        "mac_address": "1D:33:DA:33:DD:FE",
        "form_factor": {
          "value": 1200,
          "label": "SFP+ (10GE)"
        },
        "count_ipaddresses": 1,
        "id": 93,
        "tags": [],
        "description": ""
      },
      {
        "connected_endpoint_type": "dcim.interface",
        "lag": null,
        "connection_status": {
          "value": true,
          "label": "Connected"
        },
        "name": "eth2",
        "cable": {
          "url": "http://devserver1/api/dcim/cables/50/",
          "id": 50,
          "label": ""
        },
        "connected_endpoint": {
          "connection_status": {
            "value": true,
            "label": "Connected"
          },
          "name": "xe-0/1/2",
          "cable": 50,
          "url": "http://devserver1/api/dcim/interfaces/95/",
          "device": {
            "url": "http://devserver1/api/dcim/devices/28/",
            "display_name": "server1-46",
            "id": 28,
            "name": "server1-46"
          },
          "id": 95
        },
        "mgmt_only": false,
        "type": {
          "value": 1200,
          "label": "SFP+ (10GE)"
        },
        "tagged_vlans": [],
        "enabled": true,
        "untagged_vlan": null,
        "mtu": null,
        "device": {
          "url": "http://devserver1/api/dcim/devices/29/",
          "display_name": "test",
          "id": 29,
          "name": "test"
        },
        "mode": null,
        "mac_address": "20:33:D3:DD:AA:33",
        "form_factor": {
          "value": 1200,
          "label": "SFP+ (10GE)"
        },
        "count_ipaddresses": 0,
        "id": 94,
        "tags": [],
        "description": ""
      }
    ],
    "previous": null
  }
}

Hi @LuisN, thank you for that. Please also post your action alias file.

@blag

Please note I have 2 Fields, the top one works bc I am not using any for loops, 2nd field is where I am passing the info from mistral

---
name: "get_device_info"
pack: "mypack"
action_ref: "examples.mistral-get-device-info"
description: "Get Device Info"
formats:
  - "st2 get device info {{hostname}}"
result:
  extra:
    slack:
       attachments:
        -
          #fallback: "Host Details:"
          text: "Host Details:"
          fields:
            - title: Hostname
              value: "{{execution.result.device_stdout.display_name}}"
              short: true
            - title: Primary IP
              value: "{{execution.result.device_stdout.primary_ip4.address}}"
              short: true
            - title: Device Type
              value: "{{execution.result.device_stdout.device_type.display_name}}"
              short: true
            - title: Location
              value: "{{execution.result.device_stdout.site.name}}"
              short: true
            - title: Rack
              value: "{{execution.result.device_stdout.rack.name}}"
              short: true
          color: "#00AA00"
          footer:  ID "{{execution.result.device_stdout.id}}"
        -
          #fallback: "Connection Info:"
          text: "Connection Info:"
          fields: "{{ execution.result.interface_field }}"
          color: "#00AA00"
          footer:  ID "{{execution.result.device_stdout.id}}"```

@blag lmk if you are missing any more info

@blag did you have a chance to go over the setup?