ansible / event-driven-ansible

Ansible Collection for EDA
https://ansible.readthedocs.io/projects/ansible-eda/
Apache License 2.0
274 stars 106 forks source link

EDA does not like either slashes or dashes within the condition path #60

Open burnyd opened 1 year ago

burnyd commented 1 year ago

Version of ansible-rulebook

ansible-rulebook --version
__version__ = '0.9.4'
fd66d7cb-4b33-4949-b826-11a854ba7dc2

I am trying to develop a new EDA source plugin. I need to key off data coming from a pub/sub bus. Some sample data will look as the following

{"name":"sub1","timestamp":1670273612118710138,"tags":{"interface_name":"Ethernet1","source":"172.20.20.3","subscription-name":"sub1"},"values":{"/interfaces/interface/state/admin-status":"UP"}}

The very last portion is what I need to key off of the values key

"values":{"/interfaces/interface/state/admin-status":"UP"}

The condition portion of the rule-book looks as follows.

rules:
    - name: check down interface
      condition: event.values./interfaces/interface/state/admin-status == "DOWN"

Resulting in the following error.

self._session_id = self._api.createRuleset(self.serialized_ruleset)

I have tried dotted notation, brackets and also vars all within the same error message. Either I can key off of this value or if there is a way to be able to check to see if the word DOWN is within that array would work as well. But generally speaking it does not like slashes or dashes.

mkanoor commented 1 year ago

@burnyd In the source plugin are you getting array data? Currently we don't support array in conditions. In the source plugin you should be able to morph the data the way you want to check in the rules.

burnyd commented 1 year ago

@burnyd In the source plugin are you getting array data? Currently we don't support array in conditions. In the source plugin you should be able to morph the data the way you want to check in the rules.

Yes, I am getting a python dictionary for the data referenced within the issue. So the output pushed into the python async queue would look like this.

{"name":"sub1","timestamp":1670273612118710138,"tags":{"interface_name":"Ethernet1","source":"172.20.20.3","subscription-name":"sub1"},"values":{"/interfaces/interface/state/admin-status":"UP"}}

If I can understand what you are saying is I need to edit the source plugin so I am only returning the value of a string of "UP" and that the return value cannot be an array?

mkanoor commented 1 year ago

@burnyd I was thinking you would have multiple interfaces in that case you would have to use indexing e.g. interface[0], interface[1] that is currently not supported. In your example it looks like the key has embedded slashes and that might be throwing it off. I will look into it

burnyd commented 1 year ago

Okay please let me know. Otherwise, It looks I have to replace strings with "/" or "-" with underscores.

mkanoor commented 1 year ago

@burnyd The identifiers used for keys needs to follow the rules for Ansible variable names and it can only contain letters, numbers, and underscores

https://docs.ansible.com/ansible/2.5/user_guide/playbooks_variables.html#what-makes-a-valid-variable-name

I think in your case you would have to convert the / to underscore

burnyd commented 1 year ago

When you say convert the / to underscore you are referring to the actual source EDA plugin? So have that use python string replace for "/" to "" and within the condition where I am keying use "" ? Just want to make sure I am clear on this.

benthomasson commented 1 year ago

You can use event filters for this: https://github.com/benthomasson/ci-cd-demo/blob/main/event_filters/dashes_to_underscores.py

See how to use them here: https://github.com/benthomasson/ci-cd-demo/blob/main/github-ci-cd-rules.yml


    - name: azure_service_bus
      ansible.eda.azure_service_bus:
        conn_str: "{{connection_str}}"
        queue_name: "{{queue_name}}"
      filters:
        - json_filter:
            include_keys: ['clone_url']
            exclude_keys: ['*_url', '_links', 'base', 'sender', 'owner', 'user']
        - dashes_to_underscores:```