-
|
When using NetBox as a source for automation, I came across a small problem: As I understand it, an event is always fired for the object that is changed. This means that if my rule is bound to a Is there a way to be notified of all changes related to the specified object? A practical example would be to rerun the Ansible playbook to change the switch configuration, e.g. if an interface changes or the rack name changes. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Definitely not API-compliant, so needless to say use at your own risk. Signal handlers could be used to add additional events for other components as well. from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from dcim.models import Interface
from extras.choices import ObjectChangeActionChoices
from extras.events import enqueue_object
from extras.signals import is_same_object
from netbox.context import current_request, events_queue
@receiver((post_save, post_delete), sender=Interface)
def handle_changed_object(sender, instance, **kwargs):
"""
Fires when an object is created or updated.
"""
queue = events_queue.get()
request = current_request.get()
action = ObjectChangeActionChoices.ACTION_UPDATE
rel_obj = instance.device
# check if already in queue
if queue and any([is_same_object(rel_obj, e, request.id) for e in queue]):
return
enqueue_object(queue, rel_obj, request.user, request.id, action)Right now this is a bit hacky, but it does the job. Maybe in the future I'll make this more general or provide it as a small plugin. |
Beta Was this translation helpful? Give feedback.
Definitely not API-compliant, so needless to say use at your own risk. Signal handlers could be used to add additional events for other components as well.