Python list with Vcenter vm
Question: I found a Python script to list all Vcenter VM attributes, but now I need to
register some of attributes into a Python list (or array, dict... ).

But it doesn't works.

My getVminfos.py :

EDIT : the right file :

    
    
    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()
    

Prints works like a charm, but always my lists (`Ansible_Hosts`, ...) are
empty...


Answer: The lists initialization statements (Ansible_Hosts = [] etc.) should go to
main()

