|
|
|
@ -2,8 +2,8 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
|
|
|
|
|
# Copyright: (c) 2017, Ansible Project
|
|
|
|
|
# Copyright: (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
|
|
|
|
|
# Copyright: (c) 2017-18, Ansible Project
|
|
|
|
|
# Copyright: (c) 2017-18, Abhijeet Kasurde <akasurde@redhat.com>
|
|
|
|
|
# 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
|
|
|
|
@ -62,12 +62,13 @@ options:
|
|
|
|
|
description:
|
|
|
|
|
- Name of cluster name for host membership.
|
|
|
|
|
- Portgroup will be created on all hosts of the given cluster.
|
|
|
|
|
- This option is required if hosts is not specified.
|
|
|
|
|
- This option is required if C(hosts) is not specified.
|
|
|
|
|
version_added: "2.5"
|
|
|
|
|
hosts:
|
|
|
|
|
description:
|
|
|
|
|
- List of name of host or hosts on which portgroup needs to be added.
|
|
|
|
|
- This option is required if cluster_name is not specified.
|
|
|
|
|
- This option is required if C(cluster_name) is not specified.
|
|
|
|
|
aliases: [ esxi_hostname ]
|
|
|
|
|
version_added: "2.5"
|
|
|
|
|
state:
|
|
|
|
|
description:
|
|
|
|
@ -131,6 +132,21 @@ EXAMPLES = r'''
|
|
|
|
|
state: absent
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
|
result:
|
|
|
|
|
description: metadata about the portgroup
|
|
|
|
|
returned: always
|
|
|
|
|
type: dict
|
|
|
|
|
sample: {
|
|
|
|
|
"esxi01.example.com": {
|
|
|
|
|
"portgroup_name": "pg0010",
|
|
|
|
|
"switch_name": "vswitch_0001",
|
|
|
|
|
"vlan_id": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from pyVmomi import vim, vmodl
|
|
|
|
|
except ImportError:
|
|
|
|
@ -138,14 +154,15 @@ except ImportError:
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
|
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PyVmomiHelper(PyVmomi):
|
|
|
|
|
def __init__(self, module):
|
|
|
|
|
super(PyVmomiHelper, self).__init__(module)
|
|
|
|
|
|
|
|
|
|
self.hosts = self.params['hosts']
|
|
|
|
|
self.cluster = self.params['cluster_name']
|
|
|
|
|
hosts = self.params['hosts']
|
|
|
|
|
cluster = self.params['cluster_name']
|
|
|
|
|
self.portgroup_name = self.params['portgroup_name']
|
|
|
|
|
self.switch_name = self.params['switch_name']
|
|
|
|
|
self.vlan_id = self.params['vlan_id']
|
|
|
|
@ -153,17 +170,27 @@ class PyVmomiHelper(PyVmomi):
|
|
|
|
|
self.forged_transmits = self.params['network_policy'].get('forged_transmits')
|
|
|
|
|
self.mac_changes = self.params['network_policy'].get('mac_changes')
|
|
|
|
|
self.network_policy = self.create_network_policy()
|
|
|
|
|
self.changed = False
|
|
|
|
|
self.state = self.params['state']
|
|
|
|
|
|
|
|
|
|
self.host_obj_list = []
|
|
|
|
|
if cluster and self.find_cluster_by_name(cluster_name=cluster):
|
|
|
|
|
self.host_obj_list = self.get_all_hosts_by_cluster(cluster_name=cluster)
|
|
|
|
|
elif hosts:
|
|
|
|
|
for host in hosts:
|
|
|
|
|
host_system = self.find_hostsystem_by_name(host_name=host)
|
|
|
|
|
if host_system:
|
|
|
|
|
self.host_obj_list.append(host_system)
|
|
|
|
|
else:
|
|
|
|
|
self.module.fail_json(msg="Failed to find ESXi %s" % host)
|
|
|
|
|
|
|
|
|
|
def process_state(self):
|
|
|
|
|
"""
|
|
|
|
|
Function to manage state
|
|
|
|
|
"""
|
|
|
|
|
if self.state == 'present':
|
|
|
|
|
self.add_portgroup()
|
|
|
|
|
self.add_hosts_port_group()
|
|
|
|
|
elif self.state == 'absent':
|
|
|
|
|
self.remove_portgroup()
|
|
|
|
|
self.remove_hosts_port_group()
|
|
|
|
|
|
|
|
|
|
# Get
|
|
|
|
|
def get_port_group_by_name(self, host_system, portgroup_name, vswitch_name):
|
|
|
|
@ -184,34 +211,41 @@ class PyVmomiHelper(PyVmomi):
|
|
|
|
|
desired_pgs.append(pg)
|
|
|
|
|
return desired_pgs
|
|
|
|
|
|
|
|
|
|
def check_network_policy_diff(self, current_policy, desired_policy):
|
|
|
|
|
@staticmethod
|
|
|
|
|
def check_network_policy_diff(current_policy, desired_policy):
|
|
|
|
|
"""
|
|
|
|
|
Function to find difference between existing network policy and user given network policy
|
|
|
|
|
Args:
|
|
|
|
|
current_policy: Current network policy
|
|
|
|
|
desired_policy: User defined network policy
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Returns: True if difference found, False if not.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
ret = False
|
|
|
|
|
if current_policy.security.allowPromiscuous != desired_policy.security.allowPromiscuous:
|
|
|
|
|
ret = True
|
|
|
|
|
if current_policy.security.forgedTransmits != desired_policy.security.forgedTransmits:
|
|
|
|
|
ret = True
|
|
|
|
|
if current_policy.security.macChanges != desired_policy.security.macChanges:
|
|
|
|
|
if (current_policy.security.allowPromiscuous != desired_policy.security.allowPromiscuous) or \
|
|
|
|
|
(current_policy.security.forgedTransmits != desired_policy.security.forgedTransmits) or \
|
|
|
|
|
(current_policy.security.macChanges != desired_policy.security.macChanges):
|
|
|
|
|
ret = True
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
# Add
|
|
|
|
|
def add_hosts_port_group(self, hosts):
|
|
|
|
|
def add_hosts_port_group(self):
|
|
|
|
|
"""
|
|
|
|
|
Function to add port group to given hosts
|
|
|
|
|
Args:
|
|
|
|
|
hosts: List of Host System
|
|
|
|
|
"""
|
|
|
|
|
for host in hosts:
|
|
|
|
|
self.create_host_port_group(host, self.portgroup_name, self.vlan_id, self.switch_name, self.network_policy)
|
|
|
|
|
results = dict(changed=False, result=dict())
|
|
|
|
|
host_change_list = []
|
|
|
|
|
for host in self.host_obj_list:
|
|
|
|
|
change = False
|
|
|
|
|
results['result'][host.name] = dict(portgroup_name=self.portgroup_name,
|
|
|
|
|
vlan_id=self.vlan_id,
|
|
|
|
|
switch_name=self.switch_name)
|
|
|
|
|
change = self.create_host_port_group(host, self.portgroup_name, self.vlan_id, self.switch_name, self.network_policy)
|
|
|
|
|
host_change_list.append(change)
|
|
|
|
|
if any(host_change_list):
|
|
|
|
|
results['changed'] = True
|
|
|
|
|
self.module.exit_json(**results)
|
|
|
|
|
|
|
|
|
|
def create_host_port_group(self, host_system, portgroup_name, vlan_id, vswitch_name, network_policy):
|
|
|
|
|
"""
|
|
|
|
@ -241,14 +275,23 @@ class PyVmomiHelper(PyVmomi):
|
|
|
|
|
host_system.configManager.networkSystem.AddPortGroup(portgrp=port_group.spec)
|
|
|
|
|
self.changed = True
|
|
|
|
|
except vim.fault.AlreadyExists as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup as it already exists: %s" % e.msg)
|
|
|
|
|
except vim.fault.NotFound as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup as vSwitch was not found: %s" % e.msg)
|
|
|
|
|
except vim.fault.HostConfigFault as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup due to host system configuration failure : %s" % e.msg)
|
|
|
|
|
except vmodl.fault.InvalidArgument as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup as VLAN id was not correct as per specifications: %s" % e.msg)
|
|
|
|
|
# To match with other vmware_* modules if portgroup
|
|
|
|
|
# exists, we proceed, as user may want idempotency
|
|
|
|
|
self.changed = False
|
|
|
|
|
except vim.fault.NotFound as not_found:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup as vSwitch"
|
|
|
|
|
" was not found: %s" % to_native(not_found.msg))
|
|
|
|
|
except vim.fault.HostConfigFault as host_config_fault:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup due to host system"
|
|
|
|
|
" configuration failure : %s" % to_native(host_config_fault.msg))
|
|
|
|
|
except vmodl.fault.InvalidArgument as invalid_argument:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup as VLAN id was not"
|
|
|
|
|
" correct as per specifications: %s" % to_native(invalid_argument.msg))
|
|
|
|
|
except Exception as generic_exception:
|
|
|
|
|
self.module.fail_json(msg="Failed to add Portgroup due to generic"
|
|
|
|
|
" exception : %s" % to_native(generic_exception))
|
|
|
|
|
else:
|
|
|
|
|
self.changed = False
|
|
|
|
|
# Change portgroup
|
|
|
|
|
if desired_pgs[0].spec.vlanId != vlan_id:
|
|
|
|
|
port_group.spec.vlanId = vlan_id
|
|
|
|
@ -259,29 +302,26 @@ class PyVmomiHelper(PyVmomi):
|
|
|
|
|
|
|
|
|
|
if self.changed:
|
|
|
|
|
try:
|
|
|
|
|
host_system.configManager.networkSystem.UpdatePortGroup(pgName=self.portgroup_name, portgrp=port_group.spec)
|
|
|
|
|
host_system.configManager.networkSystem.UpdatePortGroup(pgName=self.portgroup_name,
|
|
|
|
|
portgrp=port_group.spec)
|
|
|
|
|
self.changed = True
|
|
|
|
|
except vim.fault.AlreadyExists as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup as it conflicts with already existing Portgroup: %s" % e.msg)
|
|
|
|
|
except vim.fault.NotFound as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup as vSwitch was not found: %s" % e.msg)
|
|
|
|
|
except vim.fault.HostConfigFault as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup due to host system configuration failure : %s" % e.msg)
|
|
|
|
|
except vmodl.fault.InvalidArgument as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup as VLAN id was not correct as per specifications: %s" % e.msg)
|
|
|
|
|
self.changed = False
|
|
|
|
|
|
|
|
|
|
def add_portgroup(self):
|
|
|
|
|
"""
|
|
|
|
|
Function to add portgroup to given cluster or given hostsystem
|
|
|
|
|
"""
|
|
|
|
|
if self.cluster and self.find_cluster_by_name(cluster_name=self.cluster):
|
|
|
|
|
hosts = self.get_all_hosts_by_cluster(cluster_name=self.cluster)
|
|
|
|
|
self.add_hosts_port_group(hosts=hosts)
|
|
|
|
|
elif self.hosts:
|
|
|
|
|
for host in self.hosts:
|
|
|
|
|
host_system = self.find_hostsystem_by_name(host_name=host)
|
|
|
|
|
if host_system:
|
|
|
|
|
self.add_hosts_port_group([host_system])
|
|
|
|
|
# To match with other vmware_* modules if portgroup
|
|
|
|
|
# exists, we proceed, as user may want idempotency
|
|
|
|
|
self.changed = False
|
|
|
|
|
except vim.fault.NotFound as not_found:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup as"
|
|
|
|
|
" vSwitch was not found: %s" % to_native(not_found.msg))
|
|
|
|
|
except vim.fault.HostConfigFault as host_config_fault:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup due to host"
|
|
|
|
|
" system configuration failure : %s" % to_native(host_config_fault.msg))
|
|
|
|
|
except vmodl.fault.InvalidArgument as invalid_argument:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup as VLAN id was not"
|
|
|
|
|
" correct as per specifications: %s" % to_native(invalid_argument.msg))
|
|
|
|
|
except Exception as generic_exception:
|
|
|
|
|
self.module.fail_json(msg="Failed to update Portgroup due to generic"
|
|
|
|
|
" exception : %s" % to_native(generic_exception))
|
|
|
|
|
return self.changed
|
|
|
|
|
|
|
|
|
|
def create_network_policy(self):
|
|
|
|
|
"""
|
|
|
|
@ -298,28 +338,24 @@ class PyVmomiHelper(PyVmomi):
|
|
|
|
|
network_policy = vim.host.NetworkPolicy(security=security_policy)
|
|
|
|
|
return network_policy
|
|
|
|
|
|
|
|
|
|
# Remove
|
|
|
|
|
def remove_portgroup(self):
|
|
|
|
|
"""
|
|
|
|
|
Function to remove portgroup depending upon hostsystem and vswitch name
|
|
|
|
|
"""
|
|
|
|
|
if self.cluster and self.find_cluster_by_name(cluster_name=self.cluster):
|
|
|
|
|
hosts = self.get_all_hosts_by_cluster(cluster_name=self.cluster)
|
|
|
|
|
self.remove_hosts_port_group(hosts=hosts)
|
|
|
|
|
elif self.hosts:
|
|
|
|
|
for host in self.hosts:
|
|
|
|
|
host_system = self.find_hostsystem_by_name(host_name=host)
|
|
|
|
|
if host_system:
|
|
|
|
|
self.remove_hosts_port_group(hosts=[host_system])
|
|
|
|
|
|
|
|
|
|
def remove_hosts_port_group(self, hosts):
|
|
|
|
|
def remove_hosts_port_group(self):
|
|
|
|
|
"""
|
|
|
|
|
Function to remove port group from given host
|
|
|
|
|
Args:
|
|
|
|
|
hosts: List of host system
|
|
|
|
|
"""
|
|
|
|
|
for host in hosts:
|
|
|
|
|
self.remove_host_port_group(host_system=host, portgroup_name=self.portgroup_name, vswitch_name=self.switch_name)
|
|
|
|
|
results = dict(changed=False, result=dict())
|
|
|
|
|
host_change_list = []
|
|
|
|
|
for host in self.host_obj_list:
|
|
|
|
|
change = False
|
|
|
|
|
results['result'][host.name] = dict(portgroup_name=self.portgroup_name,
|
|
|
|
|
vlan_id=self.vlan_id,
|
|
|
|
|
switch_name=self.switch_name)
|
|
|
|
|
change = self.remove_host_port_group(host_system=host,
|
|
|
|
|
portgroup_name=self.portgroup_name,
|
|
|
|
|
vswitch_name=self.switch_name)
|
|
|
|
|
host_change_list.append(change)
|
|
|
|
|
if any(host_change_list):
|
|
|
|
|
results['changed'] = True
|
|
|
|
|
self.module.exit_json(**results)
|
|
|
|
|
|
|
|
|
|
def remove_host_port_group(self, host_system, portgroup_name, vswitch_name):
|
|
|
|
|
"""
|
|
|
|
@ -330,19 +366,27 @@ class PyVmomiHelper(PyVmomi):
|
|
|
|
|
vswitch_name: Name of vSwitch
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
desired_pgs = self.get_port_group_by_name(host_system=host_system, portgroup_name=portgroup_name, vswitch_name=vswitch_name)
|
|
|
|
|
changed = False
|
|
|
|
|
desired_pgs = self.get_port_group_by_name(host_system=host_system,
|
|
|
|
|
portgroup_name=portgroup_name,
|
|
|
|
|
vswitch_name=vswitch_name)
|
|
|
|
|
if desired_pgs:
|
|
|
|
|
try:
|
|
|
|
|
host_system.configManager.networkSystem.RemovePortGroup(pgName=self.portgroup_name)
|
|
|
|
|
self.changed = True
|
|
|
|
|
except vim.fault.NotFound as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup as it was not found: %s" % e.msg)
|
|
|
|
|
except vim.fault.ResourceInUse as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup as it is in use: %s" % e.msg)
|
|
|
|
|
except vim.fault.HostConfigFault as e:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup due to configuration failures: %s" % e.msg)
|
|
|
|
|
else:
|
|
|
|
|
self.changed = False
|
|
|
|
|
changed = True
|
|
|
|
|
except vim.fault.NotFound as not_found:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup as it was"
|
|
|
|
|
" not found: %s" % to_native(not_found.msg))
|
|
|
|
|
except vim.fault.ResourceInUse as resource_in_use:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup as it is"
|
|
|
|
|
" in use: %s" % to_native(resource_in_use.msg))
|
|
|
|
|
except vim.fault.HostConfigFault as host_config_fault:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup due to configuration"
|
|
|
|
|
" failures: %s" % to_native(host_config_fault.msg))
|
|
|
|
|
except Exception as generic_exception:
|
|
|
|
|
self.module.fail_json(msg="Failed to remove Portgroup due to generic"
|
|
|
|
|
" exception : %s" % to_native(generic_exception))
|
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
@ -351,7 +395,7 @@ def main():
|
|
|
|
|
portgroup_name=dict(required=True, type='str'),
|
|
|
|
|
switch_name=dict(required=True, type='str'),
|
|
|
|
|
vlan_id=dict(required=True, type='int'),
|
|
|
|
|
hosts=dict(type='list'),
|
|
|
|
|
hosts=dict(type='list', aliases=['esxi_hostname']),
|
|
|
|
|
cluster_name=dict(type='str'),
|
|
|
|
|
state=dict(type='str', choices=['present', 'absent'], default='present'),
|
|
|
|
|
network_policy=dict(type='dict',
|
|
|
|
@ -378,18 +422,13 @@ def main():
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
pyv = PyVmomiHelper(module)
|
|
|
|
|
|
|
|
|
|
pyv.process_state()
|
|
|
|
|
|
|
|
|
|
changed = pyv.changed
|
|
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
except vmodl.RuntimeFault as runtime_fault:
|
|
|
|
|
module.fail_json(msg=runtime_fault.msg)
|
|
|
|
|
module.fail_json(msg=to_native(runtime_fault.msg))
|
|
|
|
|
except vmodl.MethodFault as method_fault:
|
|
|
|
|
module.fail_json(msg=method_fault.msg)
|
|
|
|
|
module.fail_json(msg=to_native(method_fault.msg))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
module.fail_json(msg=to_native(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|