Skip to content

Commit d7ca5d3

Browse files
committed
adds config option 'vm_exclude_by_tag_filter' #393
1 parent c47e4ae commit d7ca5d3

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

module/sources/vmware/config.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ def __init__(self):
108108
str, description="simply include/exclude VMs"),
109109
ConfigOption("vm_include_filter", str)
110110
]),
111+
ConfigOption("vm_exclude_by_tag_filter",
112+
str,
113+
description="""defines a comma separated list of vCenter tags which (if assigned to a VM)
114+
will exclude this VM from being synced to NetBox. The config option 'vm_tag_source'
115+
determines which tags are collected for VMs.
116+
""",
117+
config_example="tag-a, tag-b"
118+
),
119+
111120
ConfigOptionGroup(title="relations",
112121
options=[
113122
ConfigOption("cluster_site_relation",
@@ -439,7 +448,7 @@ def validate_options(self):
439448
if option.value is None:
440449
continue
441450

442-
if "filter" in option.key:
451+
if "filter" in option.key and "vm_exclude_by_tag_filter" not in option.key:
443452

444453
re_compiled = None
445454
try:
@@ -452,6 +461,12 @@ def validate_options(self):
452461

453462
continue
454463

464+
if option.key == "vm_exclude_by_tag_filter":
465+
466+
option.set_value(quoted_split(option.value))
467+
468+
continue
469+
455470
if "relation" in option.key and "vlan_group_relation" not in option.key:
456471

457472
relation_data = list()

module/sources/vmware/connection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,16 @@ def add_virtual_machine(self, obj):
21872187
vm_tags = self.get_object_relation(name, "vm_tag_relation")
21882188

21892189
# get vCenter tags
2190-
vm_tags.extend(self.collect_object_tags(obj))
2190+
vcenter_tags = self.collect_object_tags(obj)
2191+
2192+
# check if VM tag excludes VM from being synced to NetBox
2193+
for sync_exclude_tag in self.settings.vm_exclude_by_tag_filter or list():
2194+
if sync_exclude_tag in vcenter_tags:
2195+
log.debug(f"Virtual machine vCenter tag '{sync_exclude_tag}' in matches 'vm_exclude_by_tag_filter'. "
2196+
f"Skipping")
2197+
return
2198+
2199+
vm_tags.extend(vcenter_tags)
21912200

21922201
# vm memory depending on setting
21932202
vm_memory = grab(obj, "config.hardware.memoryMB", fallback=0)

settings-example.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ password = super-secret
172172
;vm_exclude_filter =
173173
;vm_include_filter =
174174

175+
; defines a comma separated list of vCenter tags which (if assigned to a VM) will exclude
176+
; this VM from being synced to NetBox. The config option 'vm_tag_source' determines which
177+
; tags are collected for VMs.
178+
;vm_exclude_by_tag_filter = tag-a, tag-b
179+
175180
; relations options
176181

177182
; This option defines which vCenter cluster is part of a NetBox site.

0 commit comments

Comments
 (0)