Testing Rulebooks, how do you folks do it?

Just wondering, I can’t find really anything meaty on how to test rulebooks using ansible-rulebook.
When developing new rulebooks, how do you experienced folks test them?

2 Likes

Hello @skeer
I think having a chapter in the documentation for this purpose is a good idea. For now I can share some tips and tricks that may be useful.

Usually, when you develop a rulebook, one of the things that you want to know is how looks the event. In that case, the flag --print-events allows you to print in the stdout all the events received.

As alternative to “–print-events” you can always start with a basic condition that will match any event. The debug action is your best friend.

condition: true
action:
  debug: "{{ event }}"

If its complicated to produce/get real events from your source but you know how they looks like, you can use the “generic” plugin in ansible.eda collection that allows you to define your custom event.

Also you have the flag “–hot-reload” which allows you run your ansible-rulebook process only one time and modify you rulebook.

Writing conditions might be a challenge if your logic or the event is complex. Always have the documentation at hand to see all the operators and functionalities that ansible-rulebook provides.

Finally, as a trick, I did some experiments with prompt engineering in LLM models, providing the documentation and some examples as prompt, and using the model to describe a condition and a sample payload to get the condition.

4 Likes

Tools like https://jsonpathfinder.com/ can help you to navigate and find the path for some value in big and complex payloads.

I could not add a third link in my previous message :s

1 Like

Thank you for this and yes I’d agree wholeheartedly about the doc addition. Every Tom, Dick, and Harry out there has websites, blog/reddit posts and YT videos on creating Playbooks, Roles and Collections. There’s almost zilch for rulebooks though, and since an RB will almost always have a tie back to an Ansible controller, it makes a testing scenario that much more complicated.
I appreciate your info!