Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env-vars has to be quoted and always returned as strings #289

Closed
konstruktoid opened this issue Jan 15, 2023 · 2 comments
Closed

env-vars has to be quoted and always returned as strings #289

konstruktoid opened this issue Jan 15, 2023 · 2 comments

Comments

@konstruktoid
Copy link
Contributor

hello_events.yml test file, only one rule was tested at a time and the others commented out.

~$ cat hello_events.yml
---
- name: Hello Events
  hosts: all
  sources:
    - ansible.eda.range:
        limit: 5
  rules:
    - name: First -- Say Hello when 1
      condition: event.i == 1
      action:
        print_event:

    - name: Second -- Say Hello when {{ EVENT_INTEGER }}
      condition: event.i == {{ EVENT_INTEGER }}
      action:
        print_event:

    - name: Third -- Say Hello when "{{ EVENT_INTEGER }}"
      condition: event.i == "{{ EVENT_INTEGER }}"
      action:
        print_event:
...
~$ echo $EVENT_INTEGER
1

First:

~$ ansible-rulebook --debug -i inventory --rulebook hello_events.yml --env-vars EVENT_INTEGER 
[...]
INFO:ansible_rulebook.engine:ruleset define: {"name": "Hello Events", "hosts": ["all"], "sources": [{"EventSource": {"name": "ansible.eda.range", "source_name": "ansible.eda.range", "source_args": {"limit": 5}, "source_filters": []}}], "rules": [{"Rule": {"name": "First -- Say Hello when 1", "condition": {"AllCondition": [{"EqualsExpression": {"lhs": {"Event": "i"}, "rhs": {"Integer": 1}}}]}, "action": {"Action": {"action": "print_event", "action_args": {}}}, "enabled": true}}]}
INFO:ansible_rulebook.rule_generator:calling First -- Say Hello when 1
INFO:ansible_rulebook.engine:substitute_variables [{}] [{'EVENT_INTEGER': '1', 'event': {'i': 1}, 'fact': {'i': 1}}]
INFO:ansible_rulebook.engine:action args: {}
{'i': 1}

Second:

~$ ansible-rulebook --debug -i inventory --rulebook hello_events.yml --env-vars EVENT_INTEGER 
[...]
INFO:ansible_rulebook.engine:ruleset define: {"name": "Hello Events", "hosts": ["all"], "sources": [{"EventSource": {"name": "ansible.eda.range", "source_name": "ansible.eda.range", "source_args": {"limit": 5}, "source_filters": []}}], "rules": [{"Rule": {"name": "Second -- Say Hello when {{ EVENT_INTEGER }}", "condition": {"AllCondition": [{"Event": "i"}]}, "action": {"Action": {"action": "print_event", "action_args": {}}}, "enabled": true}}]}

Third:

~$ ansible-rulebook --debug -i inventory --rulebook hello_events.yml --env-vars EVENT_INTEGER 
[...]
INFO:ansible_rulebook.engine:ruleset define: {"name": "Hello Events", "hosts": ["all"], "sources": [{"EventSource": {"name": "ansible.eda.range", "source_name": "ansible.eda.range", "source_args": {"limit": 5}, "source_filters": []}}], "rules": [{"Rule": {"name": "Third -- Say Hello when \"{{ EVENT_INTEGER }}\"", "condition": {"AllCondition": [{"EqualsExpression": {"lhs": {"Event": "i"}, "rhs": {"String": "1"}}}]}, "action": {"Action": {"action": "print_event", "action_args": {}}}, "enabled": true}}]}

Originally posted by @konstruktoid in #286 (comment)

@Alex-Izquierdo
Copy link
Contributor

Hello @konstruktoid
Currently the latest version of ansible-rulebook doesn't support jinja expressions on the right side of conditions due to issues with type missing. Variables are exposed as a facts and must be used in combinations with assignment operators. This is already documented:
https://ansible-rulebook.readthedocs.io/en/latest/conditions.html#condition-with-fact-and-event-fact-being-passed-in-via-variables-on-command-line

@konstruktoid
Copy link
Contributor Author

Thanks @Alex-Izquierdo.

If someone else finds this issue, the format of the data in the variable file is described here: https://ansible-rulebook.readthedocs.io/en/latest/multi_events.html.

Using the following works:

 ~$ cat variables 
{ "event_integer" : 2 }
~$ cat hello_events.yml 
---
- name: Hello Events
  hosts: all
  sources:
    - ansible.eda.range:
        limit: 5
  rules:
    - name: Say Hello using variable file
      condition:
        all:
          - facts.int << fact.event_integer is defined
          - event.i == facts.int.event_integer
      action:
        run_playbook:
          name: facts_int.yml
...
~$ cat facts_int.yml 
---
- name: Event msg integer
  hosts: all
  tasks:
    - name: Integer fact test
      ansible.builtin.debug:
        msg: "This was triggered: {{ event_integer }}"
...
~$ ansible-rulebook  -i inventory --rulebook hello_events.yml --vars variables 
[...]
TASK [Integer fact test] *******************************************************
ok: [localhost] => {
    "msg": "This was triggered: 2"
}
[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants