使用Vcenter vm的Python列表

时间:2016-02-15 09:18:22

标签: python list vcenter

我找到了一个Python脚本来列出所有Vcenter VM属性,但现在我需要将一些属性注册到Python列表(或数组,dict ......)中。

但它不起作用。

我的getVminfos.py:

编辑:正确的文件:

icacls "%LogFileFolder%" /grant Everyone:(OI)(CI)F

Prints就像魅力一样,但我的列表(import argparse import atexit import itertools import unicodedata import pyVmomi from pyVmomi import vmodl from pyVmomi import vim from pyVim.connect import SmartConnect, Disconnect def GetArgs(): parser = argparse.ArgumentParser(description='Process args for retrieving all the Virtual Machines') parser.add_argument('-s', '--host', required=True, action='store',help='Remote host 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='User name to use when connecting to host') parser.add_argument('-p', '--password', required=False, action='store',help='Password to use when connecting to host') args = parser.parse_args() return args def print_vm_info(virtual_machine): """ Print information for a particular virtual machine or recurse into a folder with depth protection """ Ansible_Hosts = [] Ansible_Groups = [] Ansible_Names = [] summary = virtual_machine.summary print("Name : ", summary.config.name) print("Template : ", summary.config.template) #print("Path : ", summary.config.vmPathName) print"Guest : ", str(unicodedata.normalize('NFKD', summary.config.guestFullName)) #print("Instance UUID : ", summary.config.instanceUuid) #print("Bios UUID : ", summary.config.uuid) print"State : ", summary.runtime.powerState if summary.guest is not None: ip_address = summary.guest.ipAddress if ip_address: Ansible_Hosts.append([ip_address]) print "Ansible_Hosts[1:15]", Ansible_Hosts[1:15] def main(): args = GetArgs() try: si = SmartConnect(host=args.host,user=args.user,pwd=args.password,port=int(args.port)) if not si: print("Could not connect to the specified host using specified " "username and password") return -1 atexit.register(Disconnect, si) content = si.RetrieveContent() # get root folder container = content.rootFolder # starting point to look into viewType = [vim.VirtualMachine] # object types to look for recursive = True # whether we should look into it recursively containerView = content.viewManager.CreateContainerView( container, viewType, recursive) children = containerView.view for child in children: print_vm_info(child) except vmodl.MethodFault as error: print("Caught vmodl fault : " + error.msg) return -1 return 0 # Start program if __name__ == "__main__": main() ,...)总是空的......

1 个答案:

答案 0 :(得分:-1)

列表初始化语句(Ansible_Hosts = []等)应该转到main()