ansible/ansible-rulebook

Throttle condition returns an 'unexpected' error

AdamMack2007 opened this issue · 5 comments

  • ansible-rulebook version: 0.10.1
  • Python version: 3.8.13
  • Operating System: RHEL 8.7

Description

When running a test rulebook with the 'throttle' parameter specified, ansible-rulebook returns the error:
jsonschema.exceptions.ValidationError: Additional properties are not allowed ('throttle' was unexpected)

What I Did

Rulebook.yml:

- name: Example
  hosts: all
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
  rules:
    - name: Get device info if BGP neighbor down
      condition: event.payload.message == "BGP neighbor down"
      throttle:
        once_within: 5 minutes
      action:
        run_job_template:
          name: Parsers
          job_args:
            limit: "{{ event.payload.meta.hosts }}"
          organization: MackNet

The command and error output

[amack@ansible-eda ansible-event-driven-automation]$ ansible-rulebook --rulebook rulebook.yml  -i inventory.yml --verbose
2023-02-10 17:52:42,045 - ansible_rulebook.validators - ERROR - Rulebook failed validation.
Traceback (most recent call last):
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/validators.py", line 36, in rulebook
    validate(instance=instance, schema=cls._get_schema())
  File "/usr/local/lib/python3.8/site-packages/jsonschema/validators.py", line 1121, in validate
    raise error
jsonschema.exceptions.ValidationError: Additional properties are not allowed ('throttle' was unexpected)

Failed validating 'additionalProperties' in schema['items']['properties']['rules']['items']:
    {'additionalProperties': False,
     'properties': {'action': {'oneOf': [{'$ref': '#/$defs/run-playbook-action'},
                                         {'$ref': '#/$defs/run-module-action'},
                                         {'$ref': '#/$defs/run-job-template-action'},
                                         {'$ref': '#/$defs/post-event-action'},
                                         {'$ref': '#/$defs/set-fact-action'},
                                         {'$ref': '#/$defs/retract-fact-action'},
                                         {'$ref': '#/$defs/print-event-action'},
                                         {'$ref': '#/$defs/debug-action'},
                                         {'$ref': '#/$defs/none-action'},
                                         {'$ref': '#/$defs/shutdown-action'},
                                         {'$ref': '#/$defs/echo-action'}]},
                    'condition': {'anyOf': [{'type': 'string'},
                                            {'$ref': '#/$defs/all-condition'},
                                            {'$ref': '#/$defs/any-condition'}]},
                    'name': {'minLength': 1,
                             'pattern': '\\S',
                             'type': 'string'}},
     'required': ['name', 'condition', 'action'],
     'type': 'object'}

On instance[0]['rules'][0]:
    {'action': {'run_job_template': {'name': 'Parsers',
                                     'organization': 'MackNet'}},
     'condition': 'event.payload.message == "BGP neighbor down"',
     'name': 'Get device info if BGP neighbor down',
     'throttle': {'once_within': '5 minutes'}}
2023-02-10 17:52:42,074 - ansible_rulebook.cli - ERROR - Unexpected exception
Traceback (most recent call last):
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/cli.py", line 203, in main
    asyncio.run(app.run(args))
  File "/usr/lib64/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib64/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/app.py", line 65, in run
    rulesets = load_rulebook(parsed_args)
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/app.py", line 149, in load_rulebook
    Validate.rulebook(data)
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/validators.py", line 36, in rulebook
    validate(instance=instance, schema=cls._get_schema())
  File "/usr/local/lib/python3.8/site-packages/jsonschema/validators.py", line 1121, in validate
    raise error
jsonschema.exceptions.ValidationError: Additional properties are not allowed ('throttle' was unexpected)

Failed validating 'additionalProperties' in schema['items']['properties']['rules']['items']:
    {'additionalProperties': False,
     'properties': {'action': {'oneOf': [{'$ref': '#/$defs/run-playbook-action'},
                                         {'$ref': '#/$defs/run-module-action'},
                                         {'$ref': '#/$defs/run-job-template-action'},
                                         {'$ref': '#/$defs/post-event-action'},
                                         {'$ref': '#/$defs/set-fact-action'},
                                         {'$ref': '#/$defs/retract-fact-action'},
                                         {'$ref': '#/$defs/print-event-action'},
                                         {'$ref': '#/$defs/debug-action'},
                                         {'$ref': '#/$defs/none-action'},
                                         {'$ref': '#/$defs/shutdown-action'},
                                         {'$ref': '#/$defs/echo-action'}]},
                    'condition': {'anyOf': [{'type': 'string'},
                                            {'$ref': '#/$defs/all-condition'},
                                            {'$ref': '#/$defs/any-condition'}]},
                    'name': {'minLength': 1,
                             'pattern': '\\S',
                             'type': 'string'}},
     'required': ['name', 'condition', 'action'],
     'type': 'object'}

On instance[0]['rules'][0]:
    {'action': {'run_job_template': {'name': 'Parsers',
                                     'organization': 'MackNet'}},
     'condition': 'event.payload.message == "BGP neighbor down"',
     'name': 'Get device info if BGP neighbor down',
     'throttle': {'once_within': '5 minutes'}}

Hi @AdamMack2007

This error is caused because when you use once_within, group_by_attributes is mandatory as is explained in the docs:
https://ansible-rulebook.readthedocs.io/en/latest/conditions.html#throttle-actions-to-counter-event-storms-reactive

Your rulebook must be:

- name: Example
  hosts: all
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
  rules:
    - name: Get device info if BGP neighbor down
      condition: event.payload.message == "BGP neighbor down"
      throttle:
        once_within: 5 minutes
        group_by_attributes:
          - event.payload.meta.hosts
      action:
        run_job_template:
          name: Parsers
          job_args:
            limit: "{{ event.payload.meta.hosts }}"
          organization: MackNet

Anyway I think the schema can be defined in another way to raise a clearer error, because the current message is inaccurate and confusing:

jsonschema.exceptions.ValidationError: Additional properties are not allowed ('throttle' was unexpected)

https://issues.redhat.com/browse/AAP-9220

@Alex-Izquierdo I still get the same error and failure to launch ansible-rulebook when I added the 'group_by_attributes' parameter:

  hosts: all
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
  rules:
    - name: Get device info if BGP neighbor down
      condition: event.payload.message == "BGP neighbor down"
      throttle:
         once_within: 5 minutes
         group_by_attributes:
           - event.payload.meta.hosts
      action:
        run_job_template:
          name: Parsers
          job_args:
            limit: "{{ event.payload.meta.hosts }}" 
          organization: MackNet
2023-02-15 15:36:44,313 - ansible_rulebook.validators - ERROR - Rulebook failed validation.
Traceback (most recent call last):
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/validators.py", line 36, in rulebook
    validate(instance=instance, schema=cls._get_schema())
  File "/usr/local/lib/python3.8/site-packages/jsonschema/validators.py", line 1121, in validate
    raise error
jsonschema.exceptions.ValidationError: Additional properties are not allowed ('throttle' was unexpected)

Failed validating 'additionalProperties' in schema['items']['properties']['rules']['items']:
    {'additionalProperties': False,
     'properties': {'action': {'oneOf': [{'$ref': '#/$defs/run-playbook-action'},
                                         {'$ref': '#/$defs/run-module-action'},
                                         {'$ref': '#/$defs/run-job-template-action'},
                                         {'$ref': '#/$defs/post-event-action'},
                                         {'$ref': '#/$defs/set-fact-action'},
                                         {'$ref': '#/$defs/retract-fact-action'},
                                         {'$ref': '#/$defs/print-event-action'},
                                         {'$ref': '#/$defs/debug-action'},
                                         {'$ref': '#/$defs/none-action'},
                                         {'$ref': '#/$defs/shutdown-action'},
                                         {'$ref': '#/$defs/echo-action'}]},
                    'condition': {'anyOf': [{'type': 'string'},
                                            {'$ref': '#/$defs/all-condition'},
                                            {'$ref': '#/$defs/any-condition'}]},
                    'name': {'minLength': 1,
                             'pattern': '\\S',
                             'type': 'string'}},
     'required': ['name', 'condition', 'action'],
     'type': 'object'}

On instance[0]['rules'][0]:
    {'action': {'run_job_template': {'job_args': {'limit': '{{ '
                                                           'event.payload.meta.hosts '
                                                           '}}'},
                                     'name': 'Parsers',
                                     'organization': 'MackNet'}},
     'condition': 'event.payload.message == "BGP neighbor down"',
     'name': 'Get device info if BGP neighbor down',
     'throttle': {'group_by_attributes': ['event.payload.meta.hosts'],
                  'once_within': '5 minutes'}}
2023-02-15 15:36:44,317 - ansible_rulebook.cli - ERROR - Unexpected exception
Traceback (most recent call last):
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/cli.py", line 203, in main
    asyncio.run(app.run(args))
  File "/usr/lib64/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib64/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/app.py", line 65, in run
    rulesets = load_rulebook(parsed_args)
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/app.py", line 149, in load_rulebook
    Validate.rulebook(data)
  File "/home/amack/.local/lib/python3.8/site-packages/ansible_rulebook/validators.py", line 36, in rulebook
    validate(instance=instance, schema=cls._get_schema())
  File "/usr/local/lib/python3.8/site-packages/jsonschema/validators.py", line 1121, in validate
    raise error
jsonschema.exceptions.ValidationError: Additional properties are not allowed ('throttle' was unexpected)

Failed validating 'additionalProperties' in schema['items']['properties']['rules']['items']:
    {'additionalProperties': False,
     'properties': {'action': {'oneOf': [{'$ref': '#/$defs/run-playbook-action'},
                                         {'$ref': '#/$defs/run-module-action'},
                                         {'$ref': '#/$defs/run-job-template-action'},
                                         {'$ref': '#/$defs/post-event-action'},
                                         {'$ref': '#/$defs/set-fact-action'},
                                         {'$ref': '#/$defs/retract-fact-action'},
                                         {'$ref': '#/$defs/print-event-action'},
                                         {'$ref': '#/$defs/debug-action'},
                                         {'$ref': '#/$defs/none-action'},
                                         {'$ref': '#/$defs/shutdown-action'},
                                         {'$ref': '#/$defs/echo-action'}]},
                    'condition': {'anyOf': [{'type': 'string'},
                                            {'$ref': '#/$defs/all-condition'},
                                            {'$ref': '#/$defs/any-condition'}]},
                    'name': {'minLength': 1,
                             'pattern': '\\S',
                             'type': 'string'}},
     'required': ['name', 'condition', 'action'],
     'type': 'object'}

On instance[0]['rules'][0]:
    {'action': {'run_job_template': {'job_args': {'limit': '{{ '
                                                           'event.payload.meta.hosts '
                                                           '}}'},
                                     'name': 'Parsers',
                                     'organization': 'MackNet'}},
     'condition': 'event.payload.message == "BGP neighbor down"',
     'name': 'Get device info if BGP neighbor down',
     'throttle': {'group_by_attributes': ['event.payload.meta.hosts'],
                  'once_within': '5 minutes'}}

Hi @AdamMack2007 Seems that your version doesn't support yet the feature. There is a confusion that we must fix, because the documentation portal doesn't point by default to the latest release.
https://ansible-rulebook.readthedocs.io/en/v0.10.1/

I recommend to install the latest version from the source code

@Alex-Izquierdo Confirmed resolved in version 0.11.0.