-
Hello there, We wish to basically dump all available information about guest VMs in vsphere which includes their Custom Attributes, and the VM notes field. We run vSphere v.7.0.3.00700 The afore mentioned fields (custom attributes, notes) don't appear to be part of the vsphere_client.vcenter.VM.get(vm.vm) object. Please note that I'm already somewhat successfully using vsphere_client.tagging.Tag.list() to get global tags/categories, but these aren't the custom ones unless Im missing something. ENV: Using Python3.10, and vSphere-Automation-SDK==1.80.0 import json
import requests
import getpass
import urllib3
import os
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
vcenters = ["vcenter1", "vcenter2"]
username = input("username:")
password = getpass.getpass("Please enter your password: ")
vms_info_general = {}
vms_info_detailed = {}
for vcenter in vcenters:
os.makedirs(f"results_json/general/{vcenter}", exist_ok=True)
os.makedirs(f"results_json/detailed/{vcenter}", exist_ok=True)
print(f"Connecting to {vcenter}")
vsphere_client = create_vsphere_client(server=vcenter, username=username, password=password, session=session)
print("Connected")
print("Getting all VMs' general info")
all_vms = vsphere_client.vcenter.VM.list()
print(f"{all_vms.__len__()} VMs on {vcenter}")
vms_info_general[vcenter] = all_vms
vms_info_detailed[vcenter] = []
#collecting category and tag information for vcentre
cat_dict = {}
for id in vsphere_client.tagging.Category.list():
cat = vsphere_client.tagging.Category.get(id)
cat_dict[cat.id] = cat.name
tag_dict = {}
for id in vsphere_client.tagging.Tag.list():
tag = vsphere_client.tagging.Tag.get(id)
tag_dict[id] = tag
for vm in all_vms:
vm_tags = {}
#vm_objs = [{'id': v.vm, 'type': 'VirtualMachine'} for v in all_vms]
vm_objs = [{'id': vm.vm, 'type': 'VirtualMachine'}]
for vm_obj in vsphere_client.tagging.TagAssociation.list_attached_tags_on_objects(vm_objs):
cat_tag_dict = {}
for ti in vm_obj.tag_ids:
cat_name = cat_dict[tag_dict[ti].category_id]
if cat_name not in cat_tag_dict:
cat_tag_dict[cat_name] = []
cat_tag_dict[cat_name].append(tag_dict[ti].name)
vm_tags[vm_obj.object_id.id] = cat_tag_dict
print(f"Getting details for {vm.name} - {vcenter}")
this_vm_details = vsphere_client.vcenter.VM.get(vm.vm)
vms_info_detailed[vcenter].append((this_vm_details, vm_tags))
for vcenter in vms_info_general:
for vm in vms_info_general[vcenter]:
with open(f"results_json/general/{vcenter}/{vm.name}.json", "w+") as f:
json.dump(vm.to_dict(), f, indent=4, sort_keys=True)
for vcenter in vms_info_detailed:
for vm, tags in vms_info_detailed[vcenter]:
with open(f"results_json/detailed/{vcenter}/{vm.name}.json", "w+") as f:
vm_dict = vm.to_dict()
vm_dict["tags"] = tags
json.dump(vm_dict, f, indent=4, sort_keys=True)
``` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @nico599 To get details of custom attributes, notes, you can use our vim APIs. The fields are part of VirtualMachine managed object. |
Beta Was this translation helpful? Give feedback.
Hi @nico599
To get details of custom attributes, notes, you can use our vim APIs.
Please refer to pyvmomi
Samples: https://github.com/vmware/pyvmomi-community-samples/tree/master/samples
The fields are part of VirtualMachine managed object.
custom attributes -> vm.summary.customValue
Notes: vm.summary.config.annotation