VMware: Add multiple IP Addresses in result (#34866)

This fix adds IPv4 and IPv6 addresses in virtual machine facts.

Fixes: #34792

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-01-16 16:31:38 +05:30 committed by GitHub
parent b7724fdf85
commit 0a9489ef66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,16 +1,18 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
# (c) 2015, Joseph Callen <jcallen () csc.com> # Copyright: (c) 2018, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'
}
DOCUMENTATION = r''' DOCUMENTATION = r'''
--- ---
@ -21,6 +23,7 @@ description:
version_added: '2.0' version_added: '2.0'
author: author:
- Joseph Callen (@jcpowermac) - Joseph Callen (@jcpowermac)
- Abhijeet Kasurde (@akasurde)
notes: notes:
- Tested on vSphere 5.5 and vSphere 6.5 - Tested on vSphere 5.5 and vSphere 6.5
requirements: requirements:
@ -55,13 +58,19 @@ except ImportError:
pass pass
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import (HAS_PYVMOMI, connect_to_api, get_all_objs, from ansible.module_utils.vmware import PyVmomi, get_all_objs, vmware_argument_spec, _get_vm_prop
vmware_argument_spec, _get_vm_prop)
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py class VmwareVmFacts(PyVmomi):
def get_all_virtual_machines(content): def __init__(self, module):
virtual_machines = get_all_objs(content, [vim.VirtualMachine]) super(VmwareVmFacts, self).__init__(module)
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
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 = {} _virtual_machines = {}
for vm in virtual_machines: for vm in virtual_machines:
@ -78,13 +87,27 @@ def get_all_virtual_machines(content):
if isinstance(dev, vim.vm.device.VirtualEthernetCard): if isinstance(dev, vim.vm.device.VirtualEthernetCard):
_mac_address.append(dev.macAddress) _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)
virtual_machine = { virtual_machine = {
summary.config.name: { summary.config.name: {
"guest_fullname": summary.config.guestFullName, "guest_fullname": summary.config.guestFullName,
"power_state": summary.runtime.powerState, "power_state": summary.runtime.powerState,
"ip_address": _ip_address, "ip_address": _ip_address, # Kept for backward compatibility
"mac_address": _mac_address, "mac_address": _mac_address, # Kept for backward compatibility
"uuid": summary.config.uuid "uuid": summary.config.uuid,
"vm_network": net_dict,
} }
} }
@ -93,23 +116,13 @@ def get_all_virtual_machines(content):
def main(): def main():
argument_spec = vmware_argument_spec() argument_spec = vmware_argument_spec()
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
if not HAS_PYVMOMI: vmware_vm_facts = VmwareVmFacts(module)
module.fail_json(msg='pyvmomi is required for this module') _virtual_machines = vmware_vm_facts.get_all_virtual_machines()
try:
content = connect_to_api(module)
_virtual_machines = get_all_virtual_machines(content)
module.exit_json(changed=False, virtual_machines=_virtual_machines) module.exit_json(changed=False, virtual_machines=_virtual_machines)
except vmodl.RuntimeFault as runtime_fault:
module.fail_json(msg=runtime_fault.msg)
except vmodl.MethodFault as method_fault:
module.fail_json(msg=method_fault.msg)
except Exception as e:
module.fail_json(msg=str(e))
if __name__ == '__main__': if __name__ == '__main__':