Adding new / custom webhook subscriptions?

Are there any guidelines available for adding new event triggers for the webhook plugin?

I’m looking into triggering a webhook when ansible variables/roles are updated/imported.

Can’t find the edit button but I wanted to add that I forgot to mention that I’ve already confirmed there are no events triggered for ForemanWebhooks when updating the ansible variables by looking at /var/log/foreman/production.log
But I do see that it triggers audit events, so if there was a way to subscribe to audit events then I could use that, it would however require some filtering to be possible as I don’t want to trigger webhooks on every audit event.

From my understanding of the code Webhooks subscribes to all events which are events of a class ObservableModel, so it could be enough to add “include Foreman::ObservableModel” to AnsibleRole. You can hopefully simply try this out and see what you get.

1 Like

Yes that seems to be true indeed that the observable events are what the webhooks are subscribing to, by looking at Foreman::EventSubscribers.all_observable_events in the foreman-rake console I see the same list that I get from the webhooks.

I tried adding include Foreman::ObservableMode to ansible_role.rb as you suggested but unfortunately that did not suffice. I’ll try to dig around more and see if I can find out how it’s being done in other plugins.

After some trial and error I finally figured it out, the only thing missing was to set the crud hooks.
I’ve opened up a PR with the changes as I believe others could benefit from this: fixes #37524 - feat: enable ansible events for webhooks by gardar · Pull Request #721 · theforeman/foreman_ansible · GitHub

Many thanks for getting me on the right track with this! :slight_smile:


Thanks @gardar, it would be great to write a little howto for other devs (e.g. on our blogpost) or perhaps demo this on our community demo. Would you be interested?

Also, I’d like to learn about the usecase for these ansible webhook triggers. What are you triggering after these actions?

1 Like

Are you talking about a howto/demo for creating new webhook triggers or are you talking about a demo with the ansible webhooks?
If it’s for creating the webhook triggers then perhaps @Dirk might be the correct person for that. I just followed his hints along with a trial and error and by looking looked through old commits in few of the foreman git repos to figure it out.

At the moment I’m going to be using the webhook that triggers when ansible variables are updated to trigger a gitlab-ci pipeline which exports the ansible variable overrides and stores them in a git repo, it’s to solve this issue: Feature #36770: Import/Export ansible variable overwrites - Ansible - Foreman
I’m also tinkering with the idea of making the same webhook trigger a ansible role run job in Satellite itself. That way when someone modifies a variable the change is immediately applied to the respective hosts.

This doc helped me gain some understanding how the events work: foreman/developer_docs/how_to_create_a_plugin.asciidoc at f4117bf05bc5ec11f0699fdd6a53ff3df19af341 · theforeman/foreman · GitHub
Unfortunately it only explained how to subscribe to events and not how to add new “subscribable” events.

P.S. where is that edit button? I swear I’ve been able to edit posts before.

As Discourse also functions via email, editing is limited to 5 minutes if I remember correctly.

And regarding the Community demo I think why Marek suggested it is that it is always a great chance to get to know why people are interested in something and how their journey to get this into Foreman was, especially in his role but also for the wider community. Such stories can then inspire others. So if you could make it and spare 5 minutes it would be great.

I meant showing the step by step how you added the new trigger and then showing, how you benefit from the new ansible triggers. The first part is important for any other contributors, who’d like to extend Foreman abilities. The second would be important for foreman_ansible maintainers, so they know the use case and the reason to keep these triggers in future. Also it would potentially inspire other users how they could integrate their Gitlab.