def main(): argument_spec = vmware_argument_spec() argument_spec.update( name=dict(type='str'), name_match=dict(type='str', choices=['first', 'last'], default='first'), uuid=dict(type='str'), use_instance_uuid=dict(type='bool', default=False), folder=dict(type='str'), datacenter=dict(type='str', required=True), tags=dict(type='bool', default=False), schema=dict(type='str', choices=['summary', 'vsphere'], default='summary'), properties=dict(type='list') ) module = AnsibleModule(argument_spec=argument_spec, required_one_of=[['name', 'uuid']], supports_check_mode=True) if module.params.get('folder'): # FindByInventoryPath() does not require an absolute path # so we should leave the input folder path unmodified module.params['folder'] = module.params['folder'].rstrip('/') if module.params['schema'] != 'vsphere' and module.params.get('properties'): module.fail_json(msg="The option 'properties' is only valid when the schema is 'vsphere'") pyv = PyVmomi(module) # Check if the VM exists before continuing vm = pyv.get_vm() # VM already exists if vm: try: if module.params['schema'] == 'summary': instance = pyv.gather_facts(vm) else: instance = pyv.to_json(vm, module.params['properties']) if module.params.get('tags'): if not HAS_VCLOUD: module.fail_json(msg="Unable to find 'vCloud Suite SDK' Python library which is required." " Please refer this URL for installation steps" " - https://code.vmware.com/web/sdk/60/vcloudsuite-python") vm_rest_client = VmwareTag(module) instance.update( tags=vm_rest_client.get_vm_tags(vm_rest_client.tag_service, vm_rest_client.tag_association_svc, vm_mid=vm._moId) ) module.exit_json(instance=instance) except Exception as exc: module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc)) else: module.fail_json(msg="Unable to gather facts for non-existing VM %s" % (module.params.get('uuid') or module.params.get('name')))
def vmware_guest_facts(VcenterConfig: VcenterConfig, show_attribute=False, datacenter='Datacenter', uuid='', schema='summary', name='') -> dict: """ :param VcenterConfig: :param show_attribute: :param datacenter: :param uuid: :param schema: :param name: :return: { "annotation": "", "current_snapshot": null, "customvalues": {}, "guest_consolidation_needed": false, "guest_question": null, "guest_tools_status": "guestToolsNotRunning", "guest_tools_version": "10247", "hw_cores_per_socket": 1, "hw_datastores": [ "ds_226_3" ], "hw_esxi_host": "10.76.33.226", "hw_eth0": { "addresstype": "assigned", "ipaddresses": null, "label": "Network adapter 1", "macaddress": "00:50:56:87:a5:9a", "macaddress_dash": "00-50-56-87-a5-9a", "portgroup_key": null, "portgroup_portkey": null, "summary": "VM Network" }, "hw_files": [ "[ds_226_3] ubuntu_t/ubuntu_t.vmx", "[ds_226_3] ubuntu_t/ubuntu_t.nvram", "[ds_226_3] ubuntu_t/ubuntu_t.vmsd", "[ds_226_3] ubuntu_t/vmware.log", "[ds_226_3] u0001/u0001.vmdk" ], "hw_folder": "/DC0/vm/Discovered virtual machine", "hw_guest_full_name": null, "hw_guest_ha_state": null, "hw_guest_id": null, "hw_interfaces": [ "eth0" ], "hw_is_template": false, "hw_memtotal_mb": 1024, "hw_name": "ubuntu_t", "hw_power_status": "poweredOff", "hw_processor_count": 1, "hw_product_uuid": "4207072c-edd8-3bd5-64dc-903fd3a0db04", "hw_version": "vmx-13", "instance_uuid": "5007769d-add3-1e12-f1fe-225ae2a07caf", "ipv4": null, "ipv6": null, "module_hw": true, "snapshots": [], "tags": [ "backup" ], "vnc": {} } """ argument_spec = { 'show_attribute': show_attribute, 'datacenter': datacenter, # 'uuid': uuid, 'schema': schema, # 'name': name } if uuid: argument_spec['uuid'] = uuid if name: argument_spec['name'] = name argument_spec.update(**VcenterConfig.as_dict()) # print(argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) pyv = PyVmomi(module) # Check if the VM exists before continuing vm = pyv.get_vm() # VM already exists if vm: try: if module.params['schema'] == 'summary': instance = pyv.gather_facts(vm) else: instance = pyv.to_json(vm, module.params['properties']) if module.params.get('tags'): vm_rest_client = VmwareTag(module) instance.update(tags=vm_rest_client.get_vm_tags( vm_rest_client.tag_service, vm_rest_client.tag_association_svc, vm_mid=vm._moId)) return instance # module.exit_json(instance=instance) except Exception as exc: module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc)) else: module.fail_json( msg="Unable to gather facts for non-existing VM %s" % (module.params.get('uuid') or module.params.get('name')))