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:
parent
b7724fdf85
commit
0a9489ef66
1 changed files with 60 additions and 47 deletions
|
@ -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 = {
|
||||||
'status': ['preview'],
|
'metadata_version': '1.1',
|
||||||
'supported_by': 'community'}
|
'status': ['preview'],
|
||||||
|
'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,61 +58,71 @@ 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)
|
||||||
_virtual_machines = {}
|
|
||||||
|
|
||||||
for vm in virtual_machines:
|
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
|
||||||
_ip_address = ""
|
def get_all_virtual_machines(self):
|
||||||
summary = vm.summary
|
"""
|
||||||
if summary.guest is not None:
|
Function to get all virtual machines and related configurations information
|
||||||
_ip_address = summary.guest.ipAddress
|
"""
|
||||||
if _ip_address is None:
|
virtual_machines = get_all_objs(self.content, [vim.VirtualMachine])
|
||||||
_ip_address = ""
|
_virtual_machines = {}
|
||||||
_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 = {
|
for vm in virtual_machines:
|
||||||
summary.config.name: {
|
_ip_address = ""
|
||||||
"guest_fullname": summary.config.guestFullName,
|
summary = vm.summary
|
||||||
"power_state": summary.runtime.powerState,
|
if summary.guest is not None:
|
||||||
"ip_address": _ip_address,
|
_ip_address = summary.guest.ipAddress
|
||||||
"mac_address": _mac_address,
|
if _ip_address is None:
|
||||||
"uuid": summary.config.uuid
|
_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)
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_virtual_machines.update(virtual_machine)
|
_virtual_machines.update(virtual_machine)
|
||||||
return _virtual_machines
|
return _virtual_machines
|
||||||
|
|
||||||
|
|
||||||
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:
|
module.exit_json(changed=False, virtual_machines=_virtual_machines)
|
||||||
content = connect_to_api(module)
|
|
||||||
_virtual_machines = get_all_virtual_machines(content)
|
|
||||||
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__':
|
||||||
|
|
Loading…
Reference in a new issue