@ -27,6 +27,7 @@ author:
- Abhijeet Kasurde ( @Akasurde )
notes :
- Tested on vSphere 5.5 and vSphere 6.5
- From 2.8 and onwards , facts are returned as list of dict instead of dict .
requirements :
- python > = 2.6
- PyVmomi
@ -78,16 +79,32 @@ EXAMPLES = r'''
- debug :
var : vm_facts . virtual_machines
- name : Get UUID from given VM Name
vmware_vm_facts :
hostname : ' {{ vcenter_hostname }} '
username : ' {{ vcenter_username }} '
password : ' {{ vcenter_password }} '
vm_type : vm
delegate_to : localhost
register : vm_facts
- debug :
msg : " {{ item.uuid }} "
with_items :
- " {{ vm_facts.virtual_machines | json_query(query) }} "
vars :
query : " [?guest_name== ' DC0_H0_VM0 ' ] "
'''
RETURN = r '''
virtual_machines :
description : dictionary of virtual machines and their facts
description : list of dictionary of virtual machines and their facts
returned : success
type : dict
sample :
type : lis t
sample : [
{
" ubuntu_t " : {
" guest_name " : " ubuntu_t " ,
" cluster " : null ,
" esxi_hostname " : " 10.76.33.226 " ,
" guest_fullname " : " Ubuntu Linux (64-bit) " ,
@ -98,8 +115,8 @@ virtual_machines:
" power_state " : " poweredOff " ,
" uuid " : " 4207072c-edd8-3bd5-64dc-903fd3a0db04 " ,
" vm_network " : { }
}
}
]
'''
try :
@ -118,10 +135,10 @@ class VmwareVmFacts(PyVmomi):
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
def get_all_virtual_machines ( self ) :
"""
Function to g et all virtual machines and related configurations information
G et all virtual machines and related configurations information
"""
virtual_machines = get_all_objs ( self . content , [ vim . VirtualMachine ] )
_virtual_machines = {}
_virtual_machines = []
for vm in virtual_machines :
_ip_address = " "
@ -161,26 +178,25 @@ class VmwareVmFacts(PyVmomi):
cluster_name = summary . runtime . host . parent . 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 ,
" cluster " : cluster_name ,
}
" 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 ,
" cluster " : cluster_name ,
}
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 . update ( virtual_machine )
_virtual_machines . append ( virtual_machine )
elif vm_type == ' template ' and is_template :
_virtual_machines . update ( virtual_machine )
_virtual_machines . append ( virtual_machine )
elif vm_type == ' all ' :
_virtual_machines . update ( virtual_machine )
_virtual_machines . append ( virtual_machine )
return _virtual_machines
@ -190,7 +206,7 @@ def main():
vm_type = dict ( type = ' str ' , choices = [ ' vm ' , ' all ' , ' template ' ] , default = ' all ' ) ,
)
module = AnsibleModule ( argument_spec = argument_spec ,
supports_check_mode = Fals e)
supports_check_mode = Tru e)
vmware_vm_facts = VmwareVmFacts ( module )
_virtual_machines = vmware_vm_facts . get_all_virtual_machines ( )