Skip to content

Commit 5404678

Browse files
Bugfix: nb_inventory.py group_by: tags (NB 2.9+) (#340)
1 parent 02f68ce commit 5404678

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,18 @@ def extract_primary_ip6(self, host):
586586
return
587587

588588
def extract_tags(self, host):
589-
return host["tags"]
589+
try:
590+
tag_zero = host["tags"][0]
591+
# Check the type of the first element in the "tags" array.
592+
# If a dictionary (Netbox >= 2.9), return an array of tags' slugs.
593+
if isinstance(tag_zero, dict):
594+
return list(sub["slug"] for sub in host["tags"])
595+
# If a string (Netbox <= 2.8), return the original "tags" array.
596+
elif isinstance(tag_zero, str):
597+
return host["tags"]
598+
# If tag_zero fails definition (no tags), return the empty array.
599+
except Exception:
600+
return host["tags"]
590601

591602
def extract_interfaces(self, host):
592603
try:

0 commit comments

Comments
 (0)