Skip to content

Add related object context to Zabbix tag and hostgroup templates#102

Open
taofineberg wants to merge 3 commits into
OpensourceICTSolutions:developmentfrom
taofineberg:feature/template-related-context
Open

Add related object context to Zabbix tag and hostgroup templates#102
taofineberg wants to merge 3 commits into
OpensourceICTSolutions:developmentfrom
taofineberg:feature/template-related-context

Conversation

@taofineberg

Copy link
Copy Markdown

Add related object context to Zabbix tag and hostgroup templates

Summary

Add related NetBox object context to templated Zabbix tags and hostgroups.

Previously, templates only had access to the assigned object through object, plus the tag or hostgroup fields. This change exposes common related objects directly in the Jinja2 context when available.

What Changed

This PR adds the following template variables for Zabbix tag and hostgroup templates:

  • device
  • site
  • tenant
  • role
  • device_type
  • manufacturer

This allows templates such as:

{{ site.name }}
{{ device.name }}
{{ device_type.model }}
{{ manufacturer.name }}

Why

This makes Zabbix tag and hostgroup templates easier to write and more flexible.

For example, users can now build dynamic tag or hostgroup values from related NetBox data without having to access everything indirectly through object.

Implementation

  • Updated ZabbixTagAssignment.get_context() to include related object context.
  • Updated ZabbixHostgroupAssignment.get_context() to include related object context.
  • Added tests for the new context variables.
  • Added render tests confirming templates can use related fields.

Testing

  • git diff --check passed.
  • ruff was not run because it is not installed in this container.
  • Django tests were not run from this checkout because there is no local manage.py in /opt/nbxsync.

bvbaekel and others added 3 commits May 3, 2026 21:17
- Updated the documentation

### New features

- Updated the Zabbix Template Assignment and Zabbix Tag Assignment forms so no duplicate templates/tags can't be assigned ([OpensourceICTSolutions#78])
- Zabbix Macro's now support Jinja2 templated values ([OpensourceICTSolutions#83])

### Bug fixes

- Fixed issue with internationalization of field set names on forms ([OpensourceICTSolutions#39])
- Fixed typo in last_sync_message ([OpensourceICTSolutions#77])
- Fixed issue with API Schema not being able to generated ([OpensourceICTSolutions#79])
- Manual device sync fails for templated Zabbix hostgroups when the rendered local hostgroup does not already exist ([OpensourceICTSolutions#81])
- Ensure that the worker doesn't crash on certain race conditions with regards to the hostinterfacesync job ([OpensourceICTSolutions#86])
- Fix issue with DeleteHost so it now actually removed the device/object from Zabbix when its deleted from NetBox ([OpensourceICTSolutions#88])
@bvbaekel

bvbaekel commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Hey @taofineberg,

Thanks for taking the time to work on this: exposing related object context directly in the Jinja2 templates is a great quality-of-life improvement. Really appreciate the initiative.

I do have a few things that need to be addressed before we can merge this:

Bug: manufacturer lookup crashes on VMs and Virtual Device Contexts
The manufacturer check (hasattr(self.assigned_object.device_type, 'manufacturer')) sits outside the device_type guard. Since assigned_object can be a VirtualMachine or Virtual Device Context, neither of which has a device_type attribute: this line will raise an AttributeError. It needs to be nested inside the if hasattr(..., 'device_type') block. Both model files have this issue.

Unrelated commits in the PR
The branch includes the v1.0.4 release commit and version bump (5a4beca, 0ca97e0) which are unrelated to this feature. Could you rebase onto development so the PR only contains your feature commit (656e07d)? This keeps the history clean and makes review easier.

device alias assumes Device objects only
Hardcoding 'device': self.assigned_object means the context will have a device key even when the assigned object is a VM or Virtual Device Context. This could be confusing for users. Consider either omitting the alias, or making it conditional on the actual object type.

Documentation
The new template variables (site, tenant, role, device_type, manufacturer) need to be documented so users know they're available. A brief addition to the relevant docs section covering the available context variables and a usage example would be sufficient.

The feature itself is solid and something users have been asking for. Once these items are addressed we should be good to go.

Happy to help if you have questions on any of these points.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants