Skip to content

Feature: Add tag-based host exclusion via exclude_tag setting#113

Open
berlikm wants to merge 5 commits into
OpensourceICTSolutions:developmentfrom
berlikm:feature/exclude-tag
Open

Feature: Add tag-based host exclusion via exclude_tag setting#113
berlikm wants to merge 5 commits into
OpensourceICTSolutions:developmentfrom
berlikm:feature/exclude-tag

Conversation

@berlikm

@berlikm berlikm commented Jul 13, 2026

Copy link
Copy Markdown

Summary

Adds a new exclude_tag plugin setting that allows excluding hosts from Zabbix sync entirely via a ZabbixTag assigned to any object in the inheritance chain (Role, Platform, Site, SiteGroup, Manufacturer, or directly on a Device/VM).

How it works

  1. Create a ZabbixTag with tag='do_not_monitor' (or any name) and any value
  2. Assign it to a DeviceRole (e.g. 'Desktop PC'), Platform, Site, or individual Device/VirtualMachine
  3. Set 'exclude_tag': 'do_not_monitor' in PLUGINS_CONFIG
  4. All hosts inheriting that tag are excluded from sync — existing Zabbix hosts are deleted

The tag is filtered before Jinja2 rendering in get_tag_attributes(), so it never reaches Zabbix as a host tag.

Dependencies

Stacks on PR #115. Merge after #115.

Testing

7 new tests covering: exclusion disabled (default), tag present/absent, skip sync when excluded, delete on exclude, and no-delete when already decommissioned. All 1076 existing tests pass.

Part of #121.

@berlikm
berlikm force-pushed the feature/exclude-tag branch 2 times, most recently from 2d95ca4 to e9b4ef1 Compare July 14, 2026 13:24
berlikm added 3 commits July 14, 2026 14:15
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
berlikm force-pushed the feature/exclude-tag branch 2 times, most recently from 91a5918 to 1bd513c Compare July 14, 2026 14:48
berlikm added 2 commits July 14, 2026 14:54
Fixes inherited Site/SiteGroup/Region assignments reading custom fields from
the intermediate object instead of the actual Device/VM.
Adds a new exclude_tag plugin setting that allows excluding hosts from
Zabbix sync entirely via a ZabbixTag assigned to any object in the
inheritance chain (Role, Platform, Site, SiteGroup, Manufacturer, or
directly on a Device/VM).

When a ZabbixTagAssignment with a tag matching exclude_tag is present
in the resolved assignments, SyncHostJob skips sync entirely. If the
host was previously synced, it is deleted from Zabbix.

The tag itself is filtered before Jinja2 rendering in
get_tag_attributes(), so it never reaches Zabbix as a host tag.

The setting defaults to '' (disabled), so existing deployments are
unaffected. This follows the same pattern as no_alerting_tag — a
signal tag that controls sync behavior without being pushed to Zabbix.

Tested: 7 new tests covering exclusion enabled/disabled, tag present
absent, and delete-on-exclude behavior. All 1092 existing tests pass.
@berlikm

berlikm commented Jul 14, 2026

Copy link
Copy Markdown
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
…stinterfaces

resolve_inherited_zabbix_assignments() returns OrderedDict for all
collections (tags, macros, templates, hostinterfaces, hostgroups).
Iterating an OrderedDict yields keys (integers), not values.
This caused tags to be skipped entirely and templates to never attach.

Add _get_all_objects() helper that handles both dict and list by checking
for .values() attribute. Also pass object=sync_target to
assigned_tag.render() so Jinja2 templates render against the actual
device/VM, not the Role/Site.

This fix belongs in PR OpensourceICTSolutions#115 because it changes resolve_inherited_zabbix_
assignments() to return OrderedDicts. PR OpensourceICTSolutions#113 (exclude_tag) and OpensourceICTSolutions#110
(device context) layer on top and inherit the fix.
berlikm added a commit to berlikm/nbxsync that referenced this pull request Jul 15, 2026
# Conflicts:
#	nbxsync/utils/sync/hostsync.py
@berlikm
berlikm force-pushed the feature/exclude-tag branch from 578ea5b to 334ec6a Compare July 16, 2026 07:33
@berlikm

berlikm commented Jul 16, 2026

Copy link
Copy Markdown
Author

Conflict resolution note for rebase

When rebasing this PR after #115 lands, there will be a conflict in nbxsync/utils/sync/hostsync.py on the tag loop:

Correct resolution: Combine both — keep the exclude_tag filtering from this PR, but use #115's or [] normalization on the for loop:

exclude_tag = self.pluginsettings.exclude_tag
for assigned_tag in (self.context.get('all_objects', {}).get('tags', []) or []):
    if exclude_tag and assigned_tag.zabbixtag.tag == exclude_tag:
        continue

Verified in the integration-test branch: 1135 tests pass, E2E confirmed.

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.

1 participant