Feature: Extend ZabbixTemplateRule with optional hostgroup and tag assignment#119
Open
berlikm wants to merge 8 commits into
Open
Conversation
This was referenced Jul 13, 2026
berlikm
force-pushed
the
feature/templaterule-hostgroup-tag
branch
from
July 14, 2026 13:04
02826e6 to
137dc3b
Compare
Adds Site, SiteGroup, and Region as valid assignment targets for all ZabbixServerAssignment, ZabbixHostgroupAssignment, ZabbixTemplateAssignment, ZabbixTagAssignment, ZabbixMacroAssignment, and ZabbixHostInventory objects. This allows assignments made at the Site/SiteGroup/Region level to be inherited by all devices and VMs at that site or below, via the existing inheritance_chain mechanism. Changes: - Constants: Site/SiteGroup/Region added to ASSIGNMENT_MODELS, ASSIGNMENT_TYPE_TO_FIELD, PATH_LABELS - Forms: All 7 assignment forms get Site/SiteGroup/Region DynamicModelChoiceField + FieldSet - Settings: inheritance_chain extended with Site/SiteGroup/Region resolution paths - Jobs: SyncHostJob._prepare_assignment() creates detached copies for inherited assignments - HostSync: _get_sync_target() resolves actual Device/VM for Jinja2 rendering - HostSync: get_tag_attributes() deduplicates tags by (tag, value) to prevent Zabbix 7 rejecting duplicate pairs inherited from multiple sources - HostInterfaceSync: get_create_params() falls back to the device's primary IP when the interface has no IP assigned (e.g. when inherited from SiteGroup) - HostInterfaceSync: errors are caught per-interface so one failing interface does not prevent the remaining interfaces and templates from being synced - Inheritance: ConfigGroup interface expansion removed; interfaces are now resolved naturally via the inheritance chain with primary IP fallback - SyncBase: _is_inherited_copy guard prevents save()/update_sync_info() on inherited copies - Views: ZabbixSiteTabView, ZabbixSiteGroupTabView, ZabbixRegionTabView added - Tests: 9 new tests for inheritance resolution Tested: all 1076 existing tests pass. Verified end-to-end on dev NetBox.
- Filter inherited/direct ZabbixServerAssignment and ZabbixHostInterface by zabbixserver - Resolve ConfigGroup interface IP from primary_ip4/primary_ip6 - Add seen-set cycle guard to resolve_path
- Fixes silent dropping of Site/SiteGroup assignments
berlikm
force-pushed
the
feature/templaterule-hostgroup-tag
branch
from
July 14, 2026 14:34
137dc3b to
2f37075
Compare
Fixes inherited Site/SiteGroup/Region assignments reading custom fields from the intermediate object instead of the actual Device/VM.
Adds a new ZabbixTemplateRule model that matches platform names against regex patterns to automatically assign Zabbix templates. This solves the scaling problem where vCenter/netbox-sync creates dozens of platform variants (e.g. 'Windows Server 2022 (Build 20348.5400)') that all need the same template. - New model: ZabbixTemplateRule (name, pattern, zabbixtemplate, enabled, priority) - Resolved in get_assigned_zabbixobjects() after direct + inherited templates - Direct assignments always take priority over regex rules - Case-insensitive matching - Full CRUD: model, form, filterset, table, view, URLs, navigation menu - Migration 0013: creates ZabbixTemplateRule table - 8 tests covering: matching, non-matching, multi-rule, direct override, disabled rules, case-insensitive, no-platform, priority ordering - All 15 tests pass (8 new + 7 existing)
Replace the unbounded re.search with a signal.alarm-based timeout that prevents catastrophic backtracking patterns from hanging the sync job. The timeout uses signal.SIGALRM which works in the main thread (where Django views and RQ worker jobs run). If called from a sub-thread where signals are unavailable, it gracefully falls back to an unbounded re.search — acceptable because platform names are short (<100 chars) and patterns are validated at model save time via re.compile().
Creates an unsaved ZabbixTemplateAssignment wrapper object (pk=None, _is_inherited_copy=True) when a regex rule matches, so downstream code (table rendering, sync) sees the same interface as real assignments.
Adds optional zabbixhostgroup and zabbixtag FKs to ZabbixTemplateRule, enabling OS-family grouping (e.g. Windows/Linux hostgroups + os_family tags) via regex rules without requiring separate assignments. - models/zabbixtemplaterule.py: added nullable SET_NULL FKs - tables/zabbixtemplaterule.py: added hostgroup and tag columns - migrations/0014_zabbixtemplaterule_hostgroup_tag.py: adds FKs - inheritance.py: when a rule matches, creates unsaved wrapper assignments for template, hostgroup, and tag (deduplicated by FK id)
berlikm
force-pushed
the
feature/templaterule-hostgroup-tag
branch
from
July 14, 2026 15:01
2f37075 to
a172983
Compare
Author
|
This PR is part of the zero-touch provisioning feature set described in #121. Please see that issue for the full architecture and motivation. |
berlikm
added a commit
to berlikm/nbxsync
that referenced
this pull request
Jul 15, 2026
berlikm
force-pushed
the
feature/templaterule-hostgroup-tag
branch
from
July 16, 2026 07:34
de5ca37 to
a172983
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends
ZabbixTemplateRulewith optionalzabbixhostgroupandzabbixtagforeign keys, enabling OS-family grouping (e.g. Windows/Linux hostgroups + os_family tags) via regex rules.Changes
models/zabbixtemplaterule.py— AddedzabbixhostgroupandzabbixtagFKs (both nullable,SET_NULL)tables/andforms/— Added hostgroup and tag columns/fieldsinheritance.py— When a rule matches, creates unsavedZabbixHostgroupAssignmentandZabbixTagAssignmentwrappers (same pattern as template wrappers), deduplicated by FK idMigration
Dependencies
Stacks on PR #118. Merge after #118.
Testing
All 1076 existing tests pass. Verified on dev NetBox — Windows regex rule correctly assigns template + hostgroup + tag in one rule.
Part of #121.