# EDA rulebook condition on webhook header keys

**URL:** <https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139>\
**Category:** Get Help\
**Tags:** ansible-rulebook, windows, eda-server, aap\
**Created:** [November 25, 2024, 5:41pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139 "2024-11-25T17:41:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlyle705](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/carlyle705/32/5552_2.png) [@carlyle705](https://forum.ansible.com/u/carlyle705)\
**Post date:** [November 25, 2024, 5:41pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/1 "2024-11-25T17:41:54Z")

</div>

Hello EDA Experts,

I’m new to Ansible. I recently installed AAP 2.5 for learning purpose. I was trying to create a simple rulebook which is triggered by a webhook from jfrog to run a template. I included a key value pair ( “os” = “linux” ) in the header when sending webhook, but having trouble for the condition in the rulebook.

```auto
- name: Listen for events on a webhook
  hosts: all
  # Define our source for events
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
  # Define the conditions we are looking for
  rules:
    - name: Demo rule
      condition: event.meta.headers.os == "linux"
      # Define the action we should take should the condition be met
      action:
        run_job_template:
          name: Ansible-Windows-Template
          organization: MyOrg

```

The condition statement in above code block doesn’t work. The only thing I can make the template to be triggered is these conditions:

```auto
condition: event.meta.headers is defined
or
condition: event.payload.event_type == 'deployed'

```

When I send the same webhook to [webhook.site](https://webhook.site), I can see the key/value pair is in the header. Unfortunately, Jfrog only allows to customize headers, the payload is defined by jfrog and we cannot change it.

Can someone share some light on how to write the condition for matching a webhook header variable?  
I wish there is a detailed document for reference, but I couldn’t find one.

Thanks

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 28, 2024, 4:16am UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/2 "2024-11-28T04:16:23Z")

</div>

Looking at the [webhook plugin source](https://github.com/ansible/event-driven-ansible/blob/main/extensions/eda/plugins/event_source/webhook.py), I don’t see any mechanism there to expose headers to your `condition:`s. But don’t take my word for it; have a look yourself.

If you figure it out, post about it here. I’m curious to know, too. Good luck.

---

<div class="post-metadata">

**Author:** ![carlyle705](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/carlyle705/32/5552_2.png) [@carlyle705](https://forum.ansible.com/u/carlyle705)\
**Post date:** [November 29, 2024, 3:24am UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/3 "2024-11-29T03:24:47Z")

</div>

I see the header data is passed to the function.

Another thing I found is, it can only decode to headers or payload. Anything inside headers and payload cannot be accessed. For example:

```auto
'payload': {'data': {'name': 'sample.txt',
                      'path': 'sample_dir/sample.txt',
                      'repo_key': 'sample_repo',
                      'sha256': 'sample_checksum',
                      'size': 0},
             'domain': 'artifact',
             'event_type': 'deployed',
             'jpd_origin': 'http://jfrog.example.com',
             'source': 'jfrog/admin',
             'subscription_key': 'test'}

```

In above payload, I can access payload.xxx, but not payload.xxx.xxx.  
For example, these conditions will work:

```auto
condition: event.payload.event_type == 'deployed'
condition: event.payload.domain == 'artifact'
condition: event.payload.data is defined

```

but these will not work:

```auto
condition: event.payload.data.name == "sample.txt"
condition: event.payload.data.size == 0

```

Wondering why it is acting like this. Wish someone from Ansible team can explain. Thanks

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 29, 2024, 1:48pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/4 "2024-11-29T13:48:50Z")

</div>

Have you tried expressing it like this?

```yaml
condition: event.payload.data["name"] == "sample.txt"
condition: event.payload.data["size"] == 0

```

Or even

```yaml
condition: event.payload["data"]["name"] == "sample.txt"
condition: event.payload["data"]["size"] == 0

```

Grasping at straws here…

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 29, 2024, 2:59pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/5 "2024-11-29T14:59:02Z")

</div>

If the input is valid JSON it works for me, I used [yq](https://github.com/mikefarah/yq) to make your example valid JSON:

```json
{
  "payload": {
    "data": {
      "name": "sample.txt",
      "path": "sample_dir/sample.txt",
      "repo_key": "sample_repo",
      "sha256": "sample_checksum",
      "size": 0
    },
    "domain": "artifact",
    "event_type": "deployed",
    "jpd_origin": "http://jfrog.example.com",
    "source": "jfrog/admin",
    "subscription_key": "test"
  }
}

```

And [JMESPath](https://jmespath.org/) to test this:

 ![Screenshot 2024-11-29 at 14-57-53 JMESPath — JMESPath](https://us1.discourse-cdn.com/ansible/original/2X/9/9dcb2504d3a67f7c090b81b6ce902d45e3368d38.png)

---

<div class="post-metadata">

**Author:** ![carlyle705](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/carlyle705/32/5552_2.png) [@carlyle705](https://forum.ansible.com/u/carlyle705)\
**Post date:** [November 29, 2024, 3:06pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/6 "2024-11-29T15:06:11Z")

</div>

@utoddl It works!! Thank you!!

---

<div class="post-metadata">

**Author:** ![carlyle705](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/carlyle705/32/5552_2.png) [@carlyle705](https://forum.ansible.com/u/carlyle705)\
**Post date:** [November 29, 2024, 3:07pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/7 "2024-11-29T15:07:54Z")

</div>

The json is sent from jfrog, so it is valid.  
Thanks.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 29, 2024, 3:12pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/8 "2024-11-29T15:12:47Z")

</div>

The example you posted above can’t be read by `jq`, it returns:

> parse error: Invalid numeric literal at line 1, column 10

And `jpterm` returns:

> Unable to load the input JSON: Expecting value: line 1 column 1 (char 0)

🤷

---

<div class="post-metadata">

**Author:** ![carlyle705](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/carlyle705/32/5552_2.png) [@carlyle705](https://forum.ansible.com/u/carlyle705)\
**Post date:** [November 29, 2024, 4:13pm UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/9 "2024-11-29T16:13:54Z")

</div>

Sorry, that block is from AAP debug screen, not a real json format.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 30, 2024, 4:19am UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/10 "2024-11-30T04:19:48Z")

</div>

I keep re-reading [Navigate structured data](https://ansible.readthedocs.io/projects/rulebook/en/stable/conditions.html#navigate-structured-data) and the best I can come up with is that this is ~~a bug~~ not the intended behavior. The very first line says

> You can navigate strutured [sic] event, fact, var data objects using either dot notation or bracket notation:

then gives examples. But clearly this is not working as described. I’m guessing this is rooted in some ambiguity accessing properties vs. non-property attributes, and that it’s related to the near-by admonition a few lines above that section:

> **_Note_** A condition **cannot** contain Jinja style substitution when accessing variables passed in from the command line, we loose [sic] the data type information and the rule engine will not process the condition. Instead use the vars prefix to [access the data passed](https://ansible.readthedocs.io/projects/rulebook/en/stable/conditions.html#condition-with-vars-and-event) in from the command line

In fact it was reading that baffling explanation that lead me to suggest using “bracket notation” rather than “dot notation”. To me that note reads, “Until our internal data model matures, you may have to resort to tricks to access certain data.” That may sound like a dig at the current implementation, but I’m actually rather optimistic that improvements along those lines will obviate the need for such shenanigans. Ansible itself suffered similar issues earlier, but now it’s much better about not exposing users to quirks of its implementation details.

It’s unfortunate you got caught trying this now. Someday – soon I hope – users may read this thread and be all confused because by then it’ll _just work!_

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 30, 2024, 4:19am UTC](https://forum.ansible.com/t/eda-rulebook-condition-on-webhook-header-keys/11139/11 "2024-12-30T04:19:58Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
