def get_all_virtual_machines(self): """ Function to get all virtual machines and related configurations information """ virtual_machines = get_all_objs(self.content, [vim.VirtualMachine]) _virtual_machines = {} for vm in virtual_machines: _ip_address = "" summary = vm.summary if summary.guest is not None: _ip_address = summary.guest.ipAddress if _ip_address is None: _ip_address = "" _mac_address = [] all_devices = _get_vm_prop(vm, ('config', 'hardware', 'device')) if all_devices: for dev in all_devices: if isinstance(dev, vim.vm.device.VirtualEthernetCard): _mac_address.append(dev.macAddress) net_dict = {} vmnet = _get_vm_prop(vm, ('guest', 'net')) if vmnet: for device in vmnet: net_dict[device.macAddress] = dict() net_dict[device.macAddress]['ipv4'] = [] net_dict[device.macAddress]['ipv6'] = [] for ip_addr in device.ipAddress: if "::" in ip_addr: net_dict[device.macAddress]['ipv6'].append(ip_addr) else: net_dict[device.macAddress]['ipv4'].append(ip_addr) esxi_hostname = None if summary.runtime.host: esxi_hostname = summary.runtime.host.summary.config.name virtual_machine = { summary.config.name: { "guest_fullname": summary.config.guestFullName, "power_state": summary.runtime.powerState, "ip_address": _ip_address, # Kept for backward compatibility "mac_address": _mac_address, # Kept for backward compatibility "uuid": summary.config.uuid, "vm_network": net_dict, "esxi_hostname": esxi_hostname, } } vm_type = self.module.params.get('vm_type') if vm_type == 'vm' and vm.config.template is False: _virtual_machines.update(virtual_machine) elif vm_type == 'template' and vm.config.template: _virtual_machines.update(virtual_machine) elif vm_type == 'all': _virtual_machines.update(virtual_machine) return _virtual_machines
def get_all_virtual_machines(self): """ Function to get all virtual machines and related configurations information """ virtual_machines = get_all_objs(self.content, [vim.VirtualMachine]) _virtual_machines = {} for vm in virtual_machines: _ip_address = "" summary = vm.summary if summary.guest is not None: _ip_address = summary.guest.ipAddress if _ip_address is None: _ip_address = "" _mac_address = [] all_devices = _get_vm_prop(vm, ('config', 'hardware', 'device')) if all_devices: for dev in all_devices: if isinstance(dev, vim.vm.device.VirtualEthernetCard): _mac_address.append(dev.macAddress) net_dict = {} vmnet = _get_vm_prop(vm, ('guest', 'net')) if vmnet: for device in vmnet: net_dict[device.macAddress] = dict() net_dict[device.macAddress]['ipv4'] = [] net_dict[device.macAddress]['ipv6'] = [] for ip_addr in device.ipAddress: if "::" in ip_addr: net_dict[device.macAddress]['ipv6'].append(ip_addr) else: net_dict[device.macAddress]['ipv4'].append(ip_addr) esxi_hostname = None if summary.runtime.host: esxi_hostname = summary.runtime.host.summary.config.name virtual_machine = { summary.config.name: { "guest_fullname": summary.config.guestFullName, "power_state": summary.runtime.powerState, "ip_address": _ip_address, # Kept for backward compatibility "mac_address": _mac_address, # Kept for backward compatibility "uuid": summary.config.uuid, "vm_network": net_dict, "esxi_hostname": esxi_hostname, } } vm_type = self.module.params.get('vm_type') if vm_type == 'vm' and vm.config.template is False: _virtual_machines.update(virtual_machine) elif vm_type == 'template' and vm.config.template: _virtual_machines.update(virtual_machine) elif vm_type == 'all': _virtual_machines.update(virtual_machine) return _virtual_machines
def get_all_virtual_machines(content): virtual_machines = get_all_objs(content, [vim.VirtualMachine]) _virtual_machines = {} for vm in virtual_machines: _ip_address = "" summary = vm.summary if summary.guest is not None: _ip_address = summary.guest.ipAddress if _ip_address is None: _ip_address = "" _mac_address = [] all_devices = _get_vm_prop(vm, ('config', 'hardware', 'device')) if all_devices: for dev in all_devices: if isinstance(dev, vim.vm.device.VirtualEthernetCard): _mac_address.append(dev.macAddress) virtual_machine = { summary.config.name: { "guest_fullname": summary.config.guestFullName, "power_state": summary.runtime.powerState, "ip_address": _ip_address, "mac_address": _mac_address, "uuid": summary.config.uuid } } _virtual_machines.update(virtual_machine) return _virtual_machines
def get_all_virtual_machines(self): """ Get all virtual machines and related configurations information """ folder = self.params.get('folder') folder_obj = None if folder: folder_obj = self.content.searchIndex.FindByInventoryPath(folder) if not folder_obj: self.module.fail_json( msg="Failed to find folder specified by %(folder)s" % self.params) virtual_machines = get_all_objs(self.content, [vim.VirtualMachine], folder=folder_obj) _virtual_machines = [] for vm in virtual_machines: _ip_address = "" summary = vm.summary if summary.guest is not None: _ip_address = summary.guest.ipAddress if _ip_address is None: _ip_address = "" _mac_address = [] all_devices = _get_vm_prop(vm, ('config', 'hardware', 'device')) if all_devices: for dev in all_devices: if isinstance(dev, vim.vm.device.VirtualEthernetCard): _mac_address.append(dev.macAddress) net_dict = {} vmnet = _get_vm_prop(vm, ('guest', 'net')) if vmnet: for device in vmnet: net_dict[device.macAddress] = dict() net_dict[device.macAddress]['ipv4'] = [] net_dict[device.macAddress]['ipv6'] = [] for ip_addr in device.ipAddress: if "::" in ip_addr: net_dict[device.macAddress]['ipv6'].append(ip_addr) else: net_dict[device.macAddress]['ipv4'].append(ip_addr) esxi_hostname = None esxi_parent = None if summary.runtime.host: esxi_hostname = summary.runtime.host.summary.config.name esxi_parent = summary.runtime.host.parent cluster_name = None if esxi_parent and isinstance(esxi_parent, vim.ClusterComputeResource): cluster_name = summary.runtime.host.parent.name vm_attributes = dict() if self.module.params.get('show_attribute'): vm_attributes = self.get_vm_attributes(vm) vm_folder = "" vm_parent = vm.parent datacenter = None while isinstance(vm_parent, vim.Folder): vm_folder += "/{0}".format(vm_parent.name) vm_parent = vm_parent.parent if isinstance(vm_parent, vim.Datacenter): datacenter = vm_parent.name vm_folder = "/{0}{1}".format(vm_parent.name, vm_folder) vm_tags = list() if self.module.params.get('show_tag'): vm_tags = self.get_tag_info(vm) virtual_machine = { "guest_name": summary.config.name, "guest_fullname": summary.config.guestFullName, "power_state": summary.runtime.powerState, "ip_address": _ip_address, # Kept for backward compatibility "mac_address": _mac_address, # Kept for backward compatibility "uuid": summary.config.uuid, "vm_network": net_dict, "esxi_hostname": esxi_hostname, "datacenter": datacenter, "cluster": cluster_name, "attributes": vm_attributes, "tags": vm_tags, "folder": vm_folder, } vm_type = self.module.params.get('vm_type') is_template = _get_vm_prop(vm, ('config', 'template')) if vm_type == 'vm' and not is_template: _virtual_machines.append(virtual_machine) elif vm_type == 'template' and is_template: _virtual_machines.append(virtual_machine) elif vm_type == 'all': _virtual_machines.append(virtual_machine) return _virtual_machines