StackStorm / StackStorm/st2

Rule failing to launch an action which has Jinja expression for default action parameter

Open
#4,781 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
6.5k
Forks
787
PR merge metrics
No merged PRs in 30d

Description

SUMMARY

When a Jinja expression is used as the default parameter for an action, the action can be run directly but fails to be launched via a Rule. This appears to only happen when the Rule includes a Jinja expression(s) to pass input to the action.

STACKSTORM VERSION

Paste the output of st2 --version: st2 2.8.1, on Python 2.7.6

OS, environment, install method

Docker from official st2-docker repo.

Steps to reproduce the problem

Consider an action with a parameter default defined by a Jinja expression that references the pack configuration:

---
name: test_default_param_with_rule_chain
pack: test
description: test
enabled: true
entry_point: chains/test_default_param_with_rule_chain.yaml
runner_type: action-chain
parameters:
  another_param:
    type: string
    required: true
  message_list:
    type: array
    required: true
    default: "{{ config_context.messages.list }}"

The pack's config file is below:

---
messages:
  list:
    - Message from pack config
    - Another message from pack config

A Rule used to launch this action:

---
name: test_default_param_with_rule
pack: test
description: A Rule that fails to launch the action due to not rendering Jinja in the action's parameter default.
enabled: true
trigger:
  type: test.test_default_param_with_rule_trigger
action:
  ref: test.test_default_param_with_rule_chain
  parameters:
    another_param: "{{ trigger.payload.message }}"

The action can be executed directly, i.e. via "st2 run".

But when the Rules Engine attempts to launch the execution, it fails with an error like the following:

root@d58bf661f7f5:~# st2 rule-enforcement get 5d6831250c23dd0037246695
+---------------------+--------------------------------------------------------------+
| Property            | Value                                                        |
+---------------------+--------------------------------------------------------------+
| id                  | 5d6831250c23dd0037246695                                     |
| rule.ref            | test.test_default_param_with_rule                            |
| trigger_instance_id | 5d6831250c23dd0037246693                                     |
| execution_id        |                                                              |
| failure_reason      | u'{{ config_context.messages.list }}' is not of type         |
|                     | u'array'                                                     |
|                     |                                                              |
|                     | Failed validating u'type' in                                 |
|                     | schema['properties'][u'message_list']:                       |
|                     |     {u'default': u'{{ config_context.messages.list }}',      |
|                     |      u'required': True,                                      |
|                     |      u'type': u'array'}                                      |
|                     |                                                              |
|                     | On instance[u'message_list']:                                |
|                     |     u'{{ config_context.messages.list }}'                    |
| enforced_at         | 2019-08-29T20:10:13.927126Z                                  |
+---------------------+--------------------------------------------------------------+

It appears as though the (unicode) string representing the Jinja expression, u'{{ config_context.messages.list }}', is getting passed as the action parameter. In other words, the Jinja expression isn't getting evaluated.

Oddly, removing the Jinja expression from the Rule fixes the issue. Re-emitting the trigger with the Rule below results in a successful action execution:

---
name: test_default_param_with_rule
pack: test
description: A Rule that fails to launch the action due to not rendering Jinja in the action's parameter default.
enabled: true
trigger:
  type: test.test_default_param_with_rule_trigger
action:
  ref: test.test_default_param_with_rule_chain
  parameters:
    another_param: "testing"

The problem appears to be that when a Jinja expression is used in a Rule to pass parameters to the action, Jinja expressions that define the action's default parameters are not getting evaluated.

Related issues:

  • #4050 may be related. Supposedly resolved in version 2.7.0 but notice that the Rule in that report did not include a Jinja expression for action input.
  • #4156 is still open. I suspect this report is reporting the same underlying bug, but can't be sure.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure using the action and Rule YAML in the report, comparing direct st2 run with Rules Engine enforcement and the rule-enforcement get output. Start by tracing how Rule action parameters and action default parameters are rendered when a Rule input contains Jinja. Done means the default config_context.messages.list value is evaluated and the Rule launches the action successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.