Repository navigation
vmware: derive the NetBox VM name and description with vm_name_regex - #590
Merged
Merged
Conversation
The new option vm_name_regex is applied to the vCenter VM name before any other name handling. Its named group "name" becomes the NetBox VM name and an optional named group "description" fills the VM's description. A name the regex does not match is used unchanged, and so is a name whose "name" group takes no part in the match or matches only whitespace; strip_vm_domain_name still runs afterwards. The regex is compiled when the config is loaded and must contain the "name" group. The resulting name is what the VM filters and relations see and what NetBox VMs are matched by, so it has to stay unique per cluster; the warning about a duplicate VM name says so when the option is in use. A name or description longer than the NetBox field is reported before it is cut. The description field is added to the VM data model and is only sent to NetBox 3.4 and later, which is where the field appeared. Fixes #465
semx
force-pushed
the
feature/vm-name-regex
branch
from
October 10, 2026 01:53
659e126 to
b17d3f5
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.
vm_name_regexfor #465: a regex applied to the vCenter VM name before any other name handling. Its named groupnamebecomes the NetBox VM name; an optional named groupdescriptionfills the VM's description. A name the regex does not match is used unchanged (one debug2 line says so), andstrip_vm_domain_namestill runs afterwards. The option is unset by default, so nothing changes without it.The example from the issue,
^(?P<name>[^ (]+)(?: \((?P<description>.*)\))?$, turnsweb01 (production)into the VMweb01with descriptionproduction. A regex without anamegroup, or one that does not compile, is rejected when the config is loaded. Anamegroup that takes no part in the match (an optional group) or matches only whitespace leaves the name unchanged with a warning, so a permissive pattern cannot produce a VM without a name. An emptydescriptiongroup sets nothing; a description already in NetBox is kept in that case, the way comments are. A result longer than the NetBox field (64 for the name, 200 for the description) is reported before NetBox cuts it. The description is only sent to NetBox 3.4 and later, where the field exists.Renaming matters for matching: a VM whose NetBox name changes is no longer found by name + cluster and falls back to the MAC, IP and serial matching that already exists. The resulting name is also what
vm_include_filter,vm_exclude_filterand thevm_*_relationoptions see, and it has to stay unique per cluster: two VMs that rewrite to the same name are reported as duplicates (the warning names the option) and the second is skipped, as before. On a live NetBox 4.7.2 with the vc001 inventory and^(?P<name>vm\d+)_(?P<description>.*)$, the 18 VMs the regex matched were renamed in place (18 PATCH, no POST, 34 VMs before and after) and got their description; the second run wrote nothing. Users who enable this on an existing NetBox should expect that one-off rename.Tests:
tests/test_vmware_vm_name_regex.py(16): match with and without the description group, a non-matching name, the optional and the blanknamegroup, the emptydescriptiongroup, anchoring at the start of the name, the length warnings, thestrip_vm_domain_nameorder, the unset default, load-time rejection of a regex without thenamegroup and of one that does not compile, and one vcsim run asserting the description lands on the VM object. All 15 test runs fail ondevelopmentwithout this change (the vcsim one is skipped on the second capture). Full suite: 295 passed, 1 skipped (development: 280). With the option unset the handler produces the same inventory asdevelopmenton vc001: 324 objects, byte-identical canonical dump.