Automating vSphere Metadata Management with pyvmomi

Listen to this Post

Featured Image
The vCenter Inventory Instance Hierarchy is a diagram that shows how different parts of your virtual infrastructure like Virtual Machines, Hosts, Datastores, and Networks are organized inside vCenter. Understanding this structure is crucial when using the vSphere API for automation and management.

Among the key metadata fields available for vSphere objects like Virtual Machines are:
– Notes: Free-text descriptions for documentation.
– Tags: Predefined labels for grouping and filtering.
– Custom Attributes: Key-value pairs for structured data.

By using the vSphere ServiceInstance with the pyvmomi Python library, you can programmatically access and manage these metadata fields, enabling better integration, automation, and reporting.

👉 Full Retrieve VM Annotations with pyvmomi

You Should Know:

1. Connecting to vSphere API with pyvmomi

from pyVim.connect import SmartConnectNoSSL, Disconnect 
import ssl

Disable SSL verification (for lab environments) 
context = ssl._create_unverified_context()

Connect to vCenter 
si = SmartConnectNoSSL( 
host="vcenter.example.com", 
user="[email protected]", 
pwd="your_password", 
port=443 
)

Retrieve VM annotations 
content = si.RetrieveContent() 
vm_view = content.viewManager.CreateContainerView( 
content.rootFolder, [vim.VirtualMachine], True 
) 
vms = vm_view.view

for vm in vms: 
print(f"VM Name: {vm.name}") 
print(f"Notes: {vm.summary.config.annotation}")

Disconnect 
Disconnect(si) 

2. Managing vSphere Tags via PowerCLI (Windows Automation)

Connect-VIServer -Server "vcenter.example.com" -User "admin" -Password "your_password"

Create a new tag category 
New-TagCategory -Name "Environment" -Cardinality "Single" -EntityType "VirtualMachine"

Assign a tag to a VM 
New-Tag -Name "Production" -Category "Environment" 
Get-VM "MyVM" | New-TagAssignment -Tag "Production" 

3. Retrieving Custom Attributes via vSphere CLI

 Using govc (vSphere CLI) 
govc vm.info -json MyVM | jq '.VirtualMachines[bash].CustomValue' 

4. Automating VM Notes Updates

 Update VM notes using pyvmomi 
vm = vm_view.view[bash]  Get first VM 
vm.setCustomValue(key="MaintenanceWindow", value="2024-06-01") 
print(f"Updated custom field for {vm.name}") 

5. Bulk Tagging with Python

from pyVmomi import vim

tag_manager = si.content.taggingManager 
tag_category = tag_manager.CreateCategory( 
vim.tag.Category( 
name="Department", 
description="VM Department", 
cardinality="SINGLE", 
associableTypes=["VirtualMachine"] 
) 
)

new_tag = tag_manager.CreateTag( 
vim.tag.Tag( 
name="Finance", 
categoryId=tag_category.id 
) 
)

Assign tag to VM 
tag_association = tag_manager.AttachTag(new_tag.id, vm) 

What Undercode Say:

Managing vSphere metadata (notes, tags, custom attributes) is essential for automation, compliance, and operational efficiency. Using pyvmomi, PowerCLI, and govc, administrators can streamline VM management, enforce tagging policies, and integrate vSphere with external tools.

For advanced automation, consider:

  • Ansible vSphere Modules (vmware_guest_tag)
  • Terraform vSphere Provider for IaC deployments
  • REST API for cloud-native integrations

Expected Output:

VM Name: MyVM 
Notes: Critical DB Server 
Custom Attributes: {'MaintenanceWindow': '2024-06-01'} 

Prediction:

As hybrid cloud adoption grows, AI-driven tagging and auto-classification of VMs based on usage patterns will become standard, reducing manual metadata management efforts.

References:

Reported By: Activity 7332371807481536513 – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram