Open zhashuyu opened 8 years ago
Get the datacenter managed object reference first and then " datacenter_mor.hostFolder.childEntity " will give you list of the clusters in that datacenter.
The above will also list standalone hosts and possibly other folders. You could manually traverse the entities: isinstance(obj, vim.ClusterComputeResource) (if isinstance(obj, vim.Folder): look under obj.childEntity as well)
You could use: https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/tools/pchelper.py view = pchelper.get_container_view(si, [vim.ClusterComputeResource]) view.view will have all your clusters. (don't forget view.Destroy() when you are done as ViewContainers use resources on the server)
You could also probably use the PropertyCollector similar to the code inside pchelper without a container, but it is probably the most work of the options.
hi, I can get cluster in a datacenter with datacenter.hostFolder.childEntity. but i cann‘t get esxi host info in a cluster, can you tell me how to do it?
Under those childEntity (it is a list), there is a list of host, e.g. datacenter.hostFolder.childEntity[0].host[0]
That will give you the HostSystem. http://pubs.vmware.com/vsphere-60/index.jsp#com.vmware.wssdk.apiref.doc/vim.HostSystem.html
Places of interest in there is: config, hardware, runtime, summary (a lot of configuration that can be changed under the services/managers in configManager).
Hi, Here is the script to list all clusters in a Data Center. If a Cluster name is provided to the script. It will list the hosts in the cluster.
#!/usr/bin/env python
"""
List Of Hosts in a Cluster
"""
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import atexit
import argparse
import getpass
import ssl
import requests
def get_args():
""" Get arguments from CLI """
parser = argparse.ArgumentParser(
description='Arguments for talking to vCenter')
parser.add_argument('-s', '--host',
required=True,
action='store',
help='vSpehre service to connect to')
parser.add_argument('-o', '--port',
type=int,
default=443,
action='store',
help='Port to connect on')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='Username to use')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use')
parser.add_argument('--cluster-name',
required=False,
action='store',
default=None,
help='Name of the cluster you wish to list the hosts')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass(
prompt='Enter password')
return args
def get_obj(content, vimtype, name = None):
return [item for item in content.viewManager.CreateContainerView(
content.rootFolder, [vimtype], recursive=True).view]
def main():
args = get_args()
# Disabling urllib3 ssl warnings
requests.packages.urllib3.disable_warnings()
# Disabling SSL certificate verification
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
# connect this thing
si = SmartConnect(
host=args.host,
user=args.user,
pwd=args.password,
port=args.port,
sslContext=context)
# disconnect this thing
atexit.register(Disconnect, si)
content = si.RetrieveContent()
for cluster_obj in get_obj(content, vim.ComputeResource,
args.cluster_name):
if args.cluster_name:
if cluster_obj.name == args.cluster_name:
for host in cluster_obj.host:
print host.name
else:
print cluster_obj.name
# start this thing
if __name__ == "__main__":
main()
Hi, please tell me how to display all license name from data center.
This code looks great, thanks! Do you need to destroyview on the on the containerview?
This was mentioned earlier in the thread and I'm not sure why it is needed or where to put it in the code. Will the resources on the server be return when the program exits? If so, then it is probably not big deal.
Do you need to destroyview on the on the containerview? It is a best practice to do that. If the session is properly logged out (I believe that is what Disconnect function does), then it should clean it up.
def get_obj(content, vimtype, name = None):
cv = content.viewManager.CreateContainerView(
content.rootFolder, [vimtype], recursive=True)
try:
return [item for item in cv.view] # or just return cv.view
finally:
cv.Destroy()
I want list all cluster in datacenter and I still have no idea to do that after days search. Please help!