mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
VMware: Refactor vmware_vmkernel (#33664)
This fix adds following: * Documentation update * Add support for DHCP * Refactor code to use PyVmomi class Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
dd2ea8ae1d
commit
ff525ab3d1
1 changed files with 149 additions and 106 deletions
|
@ -1,195 +1,239 @@
|
||||||
#!/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) 2017, Ansible Project
|
||||||
|
# Copyright: (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
|
||||||
# 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 = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: vmware_vmkernel
|
module: vmware_vmkernel
|
||||||
short_description: Create a VMware VMkernel Interface
|
short_description: Create a VMware VMkernel Interface.
|
||||||
description:
|
description:
|
||||||
- Create a VMware VMkernel Interface
|
- Create a VMware VMkernel Interface.
|
||||||
version_added: 2.0
|
version_added: 2.0
|
||||||
author: "Joseph Callen (@jcpowermac), Russell Teague (@mtnbikenc)"
|
author:
|
||||||
|
- Joseph Callen (@jcpowermac)
|
||||||
|
- Russell Teague (@mtnbikenc)
|
||||||
|
- Abhijeet Kasurde (@akasurde) <akasurde@redhat.com>
|
||||||
notes:
|
notes:
|
||||||
- Tested on vSphere 5.5
|
- Tested on vSphere 5.5, 6.5
|
||||||
requirements:
|
requirements:
|
||||||
- "python >= 2.6"
|
- "python >= 2.6"
|
||||||
- PyVmomi
|
- PyVmomi
|
||||||
options:
|
options:
|
||||||
vswitch_name:
|
vswitch_name:
|
||||||
description:
|
description:
|
||||||
- The name of the vswitch where to add the VMK interface
|
- The name of the vswitch where to add the VMK interface.
|
||||||
required: True
|
required: True
|
||||||
portgroup_name:
|
portgroup_name:
|
||||||
description:
|
description:
|
||||||
- The name of the portgroup for the VMK interface
|
- The name of the portgroup for the VMK interface.
|
||||||
required: True
|
required: True
|
||||||
|
network:
|
||||||
|
description:
|
||||||
|
- A dictionary of network details.
|
||||||
|
- 'Following parameter is required:'
|
||||||
|
- ' - C(type) (string): Type of IP assignment (either C(dhcp) or C(static)).'
|
||||||
|
- 'Following parameters are required in case of C(type) is set to C(static)'
|
||||||
|
- ' - C(ip_address) (string): Static IP address (implies C(type: static)).'
|
||||||
|
- ' - C(netmask) (string): Static netmask required for C(ip).'
|
||||||
|
version_added: 2.5
|
||||||
ip_address:
|
ip_address:
|
||||||
description:
|
description:
|
||||||
- The IP Address for the VMK interface
|
- The IP Address for the VMK interface.
|
||||||
required: True
|
- Use C(network) parameter with C(ip_address) instead.
|
||||||
|
- Deprecated option, will be removed in version 2.9.
|
||||||
subnet_mask:
|
subnet_mask:
|
||||||
description:
|
description:
|
||||||
- The Subnet Mask for the VMK interface
|
- The Subnet Mask for the VMK interface.
|
||||||
required: True
|
- Use C(network) parameter with C(subnet_mask) instead.
|
||||||
|
- Deprecated option, will be removed in version 2.9.
|
||||||
vland_id:
|
vland_id:
|
||||||
description:
|
description:
|
||||||
- The VLAN ID for the VMK interface
|
- The VLAN ID for the VMK interface.
|
||||||
required: True
|
required: True
|
||||||
mtu:
|
mtu:
|
||||||
description:
|
description:
|
||||||
- The MTU for the VMK interface
|
- The MTU for the VMK interface.
|
||||||
required: False
|
required: False
|
||||||
enable_vsan:
|
enable_vsan:
|
||||||
description:
|
description:
|
||||||
- Enable the VMK interface for VSAN traffic
|
- Enable the VMK interface for VSAN traffic.
|
||||||
required: False
|
required: False
|
||||||
enable_vmotion:
|
enable_vmotion:
|
||||||
description:
|
description:
|
||||||
- Enable the VMK interface for vMotion traffic
|
- Enable the VMK interface for vMotion traffic.
|
||||||
required: False
|
required: False
|
||||||
enable_mgmt:
|
enable_mgmt:
|
||||||
description:
|
description:
|
||||||
- Enable the VMK interface for Management traffic
|
- Enable the VMK interface for Management traffic.
|
||||||
required: False
|
required: False
|
||||||
enable_ft:
|
enable_ft:
|
||||||
description:
|
description:
|
||||||
- Enable the VMK interface for Fault Tolerance traffic
|
- Enable the VMK interface for Fault Tolerance traffic.
|
||||||
required: False
|
required: False
|
||||||
extends_documentation_fragment: vmware.documentation
|
extends_documentation_fragment: vmware.documentation
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
# Example command from Ansible Playbook
|
- name: Add Management vmkernel port using static network type
|
||||||
|
vmware_vmkernel:
|
||||||
- name: Add Management vmkernel port (vmk1)
|
hostname: 192.168.127.9
|
||||||
local_action:
|
username: admin
|
||||||
module: vmware_vmkernel
|
password: supersecret123
|
||||||
hostname: esxi_hostname
|
vswitch_name: vSwitch0
|
||||||
username: esxi_username
|
portgroup_name: PG_0001
|
||||||
password: esxi_password
|
|
||||||
vswitch_name: vswitch_name
|
|
||||||
portgroup_name: portgroup_name
|
|
||||||
vlan_id: vlan_id
|
vlan_id: vlan_id
|
||||||
ip_address: ip_address
|
network:
|
||||||
subnet_mask: subnet_mask
|
type: 'static'
|
||||||
|
ip_address: 192.168.127.10
|
||||||
|
subnet_mask: 255.255.255.0
|
||||||
|
enable_mgmt: True
|
||||||
|
|
||||||
|
- name: Add Management vmkernel port using DHCP network type
|
||||||
|
vmware_vmkernel:
|
||||||
|
hostname: 192.168.127.9
|
||||||
|
username: admin
|
||||||
|
password: supersecret123
|
||||||
|
vswitch_name: vSwitch0
|
||||||
|
portgroup_name: PG_0002
|
||||||
|
vlan_id: vlan_id
|
||||||
|
network:
|
||||||
|
type: 'dhcp'
|
||||||
enable_mgmt: True
|
enable_mgmt: True
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from pyVmomi import vim, vmodl
|
from pyVmomi import vim, vmodl
|
||||||
HAS_PYVMOMI = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_PYVMOMI = False
|
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, vmware_argument_spec
|
from ansible.module_utils.vmware import get_all_objs, PyVmomi, vmware_argument_spec
|
||||||
|
|
||||||
|
|
||||||
def create_vmkernel_adapter(host_system, port_group_name,
|
class PyVmomiHelper(PyVmomi):
|
||||||
vlan_id, vswitch_name,
|
def __init__(self, module):
|
||||||
ip_address, subnet_mask,
|
super(PyVmomiHelper, self).__init__(module)
|
||||||
mtu, enable_vsan, enable_vmotion, enable_mgmt, enable_ft):
|
self.port_group_name = self.params['portgroup_name']
|
||||||
|
self.ip_address = self.params['network'].get('ip_address', None)
|
||||||
|
self.subnet_mask = self.params['network'].get('subnet_mask', None)
|
||||||
|
self.network_type = self.params['network']['type']
|
||||||
|
self.mtu = self.params['mtu']
|
||||||
|
self.enable_vsan = self.params['enable_vsan']
|
||||||
|
self.enable_vmotion = self.params['enable_vmotion']
|
||||||
|
self.enable_mgmt = self.params['enable_mgmt']
|
||||||
|
self.enable_ft = self.params['enable_ft']
|
||||||
|
self.vswitch_name = self.params['vswitch_name']
|
||||||
|
self.vlan_id = self.params['vlan_id']
|
||||||
|
|
||||||
host_config_manager = host_system.configManager
|
# TODO: Add logic to select different hostsystem
|
||||||
host_network_system = host_config_manager.networkSystem
|
host = get_all_objs(self.content, [vim.HostSystem])
|
||||||
host_virtual_vic_manager = host_config_manager.virtualNicManager
|
if not host:
|
||||||
config = vim.host.NetworkConfig()
|
self.module.fail_json(msg="Unable to locate Physical Host.")
|
||||||
|
self.host_system = host.keys()[0]
|
||||||
|
|
||||||
config.portgroup = [vim.host.PortGroup.Config()]
|
def create_vmkernel_adapter(self):
|
||||||
config.portgroup[0].changeOperation = "add"
|
host_config_manager = self.host_system.configManager
|
||||||
config.portgroup[0].spec = vim.host.PortGroup.Specification()
|
host_network_system = host_config_manager.networkSystem
|
||||||
config.portgroup[0].spec.name = port_group_name
|
host_virtual_vic_manager = host_config_manager.virtualNicManager
|
||||||
config.portgroup[0].spec.vlanId = vlan_id
|
config = vim.host.NetworkConfig()
|
||||||
config.portgroup[0].spec.vswitchName = vswitch_name
|
|
||||||
config.portgroup[0].spec.policy = vim.host.NetworkPolicy()
|
|
||||||
|
|
||||||
config.vnic = [vim.host.VirtualNic.Config()]
|
config.portgroup = [vim.host.PortGroup.Config()]
|
||||||
config.vnic[0].changeOperation = "add"
|
config.portgroup[0].changeOperation = "add"
|
||||||
config.vnic[0].portgroup = port_group_name
|
config.portgroup[0].spec = vim.host.PortGroup.Specification()
|
||||||
config.vnic[0].spec = vim.host.VirtualNic.Specification()
|
config.portgroup[0].spec.name = self.port_group_name
|
||||||
config.vnic[0].spec.ip = vim.host.IpConfig()
|
config.portgroup[0].spec.vlanId = self.vlan_id
|
||||||
config.vnic[0].spec.ip.dhcp = False
|
config.portgroup[0].spec.vswitchName = self.vswitch_name
|
||||||
config.vnic[0].spec.ip.ipAddress = ip_address
|
config.portgroup[0].spec.policy = vim.host.NetworkPolicy()
|
||||||
config.vnic[0].spec.ip.subnetMask = subnet_mask
|
|
||||||
if mtu:
|
|
||||||
config.vnic[0].spec.mtu = mtu
|
|
||||||
|
|
||||||
host_network_config_result = host_network_system.UpdateNetworkConfig(config, "modify")
|
config.vnic = [vim.host.VirtualNic.Config()]
|
||||||
|
config.vnic[0].changeOperation = "add"
|
||||||
|
config.vnic[0].portgroup = self.port_group_name
|
||||||
|
config.vnic[0].spec = vim.host.VirtualNic.Specification()
|
||||||
|
config.vnic[0].spec.ip = vim.host.IpConfig()
|
||||||
|
|
||||||
for vnic_device in host_network_config_result.vnicDevice:
|
if self.network_type == 'dhcp':
|
||||||
if enable_vsan:
|
config.vnic[0].spec.ip.dhcp = True
|
||||||
vsan_system = host_config_manager.vsanSystem
|
else:
|
||||||
vsan_config = vim.vsan.host.ConfigInfo()
|
config.vnic[0].spec.ip.dhcp = False
|
||||||
vsan_config.networkInfo = vim.vsan.host.ConfigInfo.NetworkInfo()
|
|
||||||
|
|
||||||
vsan_config.networkInfo.port = [vim.vsan.host.ConfigInfo.NetworkInfo.PortConfig()]
|
if self.ip_address is None:
|
||||||
|
self.module.fail_json(msg="network.ip_address is required parameter in "
|
||||||
|
"case of 'static' network_type.")
|
||||||
|
if self.subnet_mask is None:
|
||||||
|
self.module.fail_json(msg="network.subnet_mask is required parameter in "
|
||||||
|
"case of 'static' network_type.")
|
||||||
|
|
||||||
vsan_config.networkInfo.port[0].device = vnic_device
|
config.vnic[0].spec.ip.ipAddress = self.ip_address
|
||||||
vsan_system.UpdateVsan_Task(vsan_config)
|
config.vnic[0].spec.ip.subnetMask = self.subnet_mask
|
||||||
|
|
||||||
if enable_vmotion:
|
if self.mtu:
|
||||||
host_virtual_vic_manager.SelectVnicForNicType("vmotion", vnic_device)
|
config.vnic[0].spec.mtu = self.mtu
|
||||||
|
|
||||||
if enable_mgmt:
|
host_network_config_result = host_network_system.UpdateNetworkConfig(config, "modify")
|
||||||
host_virtual_vic_manager.SelectVnicForNicType("management", vnic_device)
|
|
||||||
|
|
||||||
if enable_ft:
|
for vnic_device in host_network_config_result.vnicDevice:
|
||||||
host_virtual_vic_manager.SelectVnicForNicType("faultToleranceLogging", vnic_device)
|
if self.enable_vsan:
|
||||||
return True
|
vsan_system = host_config_manager.vsanSystem
|
||||||
|
vsan_config = vim.vsan.host.ConfigInfo()
|
||||||
|
vsan_config.networkInfo = vim.vsan.host.ConfigInfo.NetworkInfo()
|
||||||
|
|
||||||
|
vsan_config.networkInfo.port = [vim.vsan.host.ConfigInfo.NetworkInfo.PortConfig()]
|
||||||
|
|
||||||
|
vsan_config.networkInfo.port[0].device = vnic_device
|
||||||
|
vsan_system.UpdateVsan_Task(vsan_config)
|
||||||
|
|
||||||
|
if self.enable_vmotion:
|
||||||
|
host_virtual_vic_manager.SelectVnicForNicType("vmotion", vnic_device)
|
||||||
|
|
||||||
|
if self.enable_mgmt:
|
||||||
|
host_virtual_vic_manager.SelectVnicForNicType("management", vnic_device)
|
||||||
|
|
||||||
|
if self.enable_ft:
|
||||||
|
host_virtual_vic_manager.SelectVnicForNicType("faultToleranceLogging", vnic_device)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
argument_spec = vmware_argument_spec()
|
argument_spec = vmware_argument_spec()
|
||||||
argument_spec.update(dict(portgroup_name=dict(required=True, type='str'),
|
argument_spec.update(dict(portgroup_name=dict(required=True, type='str'),
|
||||||
ip_address=dict(required=True, type='str'),
|
ip_address=dict(removed_in_version=2.9, type='str'),
|
||||||
subnet_mask=dict(required=True, type='str'),
|
subnet_mask=dict(removed_in_version=2.9, type='str'),
|
||||||
mtu=dict(required=False, type='int'),
|
mtu=dict(required=False, type='int'),
|
||||||
enable_vsan=dict(required=False, type='bool'),
|
enable_vsan=dict(required=False, type='bool'),
|
||||||
enable_vmotion=dict(required=False, type='bool'),
|
enable_vmotion=dict(required=False, type='bool'),
|
||||||
enable_mgmt=dict(required=False, type='bool'),
|
enable_mgmt=dict(required=False, type='bool'),
|
||||||
enable_ft=dict(required=False, type='bool'),
|
enable_ft=dict(required=False, type='bool'),
|
||||||
vswitch_name=dict(required=True, type='str'),
|
vswitch_name=dict(required=True, type='str'),
|
||||||
vlan_id=dict(required=True, type='int')))
|
vlan_id=dict(required=True, type='int'),
|
||||||
|
network=dict(required=True,
|
||||||
|
type='dict',
|
||||||
|
options=dict(
|
||||||
|
type=dict(type='str', choices=['static', 'dhcp']),
|
||||||
|
ip_address=dict(type='str'),
|
||||||
|
subnet_mask=dict(type='str'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
|
||||||
|
pyv = PyVmomiHelper(module)
|
||||||
if not HAS_PYVMOMI:
|
|
||||||
module.fail_json(msg='pyvmomi is required for this module')
|
|
||||||
|
|
||||||
port_group_name = module.params['portgroup_name']
|
|
||||||
ip_address = module.params['ip_address']
|
|
||||||
subnet_mask = module.params['subnet_mask']
|
|
||||||
mtu = module.params['mtu']
|
|
||||||
enable_vsan = module.params['enable_vsan']
|
|
||||||
enable_vmotion = module.params['enable_vmotion']
|
|
||||||
enable_mgmt = module.params['enable_mgmt']
|
|
||||||
enable_ft = module.params['enable_ft']
|
|
||||||
vswitch_name = module.params['vswitch_name']
|
|
||||||
vlan_id = module.params['vlan_id']
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
content = connect_to_api(module)
|
changed = pyv.create_vmkernel_adapter()
|
||||||
host = get_all_objs(content, [vim.HostSystem])
|
|
||||||
if not host:
|
|
||||||
module.fail_json(msg="Unable to locate Physical Host.")
|
|
||||||
host_system = host.keys()[0]
|
|
||||||
changed = create_vmkernel_adapter(host_system, port_group_name,
|
|
||||||
vlan_id, vswitch_name,
|
|
||||||
ip_address, subnet_mask,
|
|
||||||
mtu, enable_vsan, enable_vmotion, enable_mgmt, enable_ft)
|
|
||||||
module.exit_json(changed=changed)
|
module.exit_json(changed=changed)
|
||||||
except vmodl.RuntimeFault as runtime_fault:
|
except vmodl.RuntimeFault as runtime_fault:
|
||||||
module.fail_json(msg=runtime_fault.msg)
|
module.fail_json(msg=runtime_fault.msg)
|
||||||
|
@ -198,6 +242,5 @@ def main():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue