ansible / ansible-rulebook

Apache License 2.0
193 stars 77 forks source link

Can't use ansible.builtin filters in rulebook #691

Closed mooky31 closed 3 months ago

mooky31 commented 4 months ago

Please confirm the following

Bug Summary

When I try to manipulate some data I want to sent to the playbook in extra_vars using ansible builtin filters, it fails

Environment

Centos stream 9

$ ansible-rulebook --version
1.1.0
  Executable location = /home/bzjr2686/ansible-rulebook/venv/bin/ansible-rulebook
  Drools_jpy version = 0.3.9
  Java home = /usr/lib/jvm/java-17-openjdk-17.0.6.0.10-3.el9.x86_64
  Java version = 17.0.6
  Python version = 3.9.18 (main, Jan 24 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)]

Steps to reproduce

$ cat rule-test.yml
---
- name: Test webhook
  hosts: localhost

  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 8081

  rules:
    - name: Run playbook
      condition: True
      action:
        run_playbook:
          name: play-test.yml
          extra_vars:
            testkey1: testval1
            testfilter1: "{{ event.payload.hostname | ansible.builtin.regex_replace('\\w+_(\\w+)', '\\1') }}"

$ ansible-rulebook --rulebook rule-test.yml -i inventory.yaml $ curl -k --header "Content-Type: application/json" --data '{ "hostname": "ABC_Hostname" }' http://localhost:8081

Actual results

2024-06-05 18:02:09,481 - ansible_rulebook.rule_set_runner - ERROR - Error calling action run_playbook, err No filter named 'ansible.builtin.regex_replace'.

Expected results

Playbook starting with testfilter1: Hostname in extra_vars

Additional information

No response

mkanoor commented 4 months ago

@mooky31 ansible playbook filters are not supported in ansible-rulebook. There is support for builtin in Jinja filters. e.g.

- name: Replace Jinja filter
  hosts: all
  sources:
    - ansible.eda.generic:
        payload:
          - msg: hello_world
  rules:
    - name: Say Hello
      condition: true
      action:
        debug:
          msg: "This is a replace example {{ event.msg|replace('_','-') }}"

Or the other option to do is use the filter in the playbook vars section. All the event data would be passed into the playbook under ansible_eda. as extra vars and there you can apply the ansible playbook filters. Example (https://github.com/ansible/ansible-rulebook/blob/857ce658cae1b6dea4e05e87f384907c5b793793/tests/e2e/files/playbooks/print_event.yml#L7)

mooky31 commented 4 months ago

Ok thanks I understand I'll figure out something. Are there any plan to support them ?

Alex-Izquierdo commented 3 months ago

Hi @mooky31 There are no official plans to include it at this time. You can open a feature request. If we see traction we may consider it as a higher priority.

I close this issue since it is not a bug.