Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmware_vswitch_info: Add support to return information about policies #1309

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- vmware_vswitch_info - Add support to return security, teaming and traffic shaping policies on vSwitches.
(https://github.com/ansible-collections/community.vmware/pull/1309).
78 changes: 67 additions & 11 deletions plugins/modules/vmware_vswitch_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
- python >= 2.6
- PyVmomi
options:
policies:
version_added: '2.4.0'
description:
- Gather information about Security, Traffic Shaping, as well as Teaming and failover.
- The property C(ts) stands for Traffic Shaping and C(lb) for Load Balancing.
type: bool
default: false
cluster_name:
description:
- Name of the cluster.
Expand Down Expand Up @@ -72,15 +79,31 @@
"num_ports": 128,
"pnics": [
"vmnic0"
]
],
"failback": true,
"failover_active": ["vmnic0"],
"failover_standby": [],
"failure_detection": "link_status_only",
"lb": "loadbalance_srcid",
"notify": true,
"security": [false, false, false],
"ts": false
},
"vSwitch_0011": {
"mtu": 1500,
"num_ports": 128,
"pnics": [
"vmnic2",
"vmnic1"
]
],
"failback": true,
"failover_active": ["vmnic1"],
"failover_standby": ["vmnic2"],
"failure_detection": "link_status_only",
"lb": "loadbalance_srcid",
"notify": true,
"security": [false, false, false],
"ts": false,
},
},
}
Expand All @@ -100,6 +123,7 @@ def __init__(self, module):
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
if not self.hosts:
self.module.fail_json(msg="Failed to find host system.")
self.policies = self.params.get('policies')

@staticmethod
def serialize_pnics(vswitch_obj):
Expand All @@ -110,22 +134,53 @@ def serialize_pnics(vswitch_obj):
pnics.append(pnic.split("-", 3)[-1])
return pnics

@staticmethod
def normalize_vswitch_info(vswitch_obj, policy_info):
"""Create vSwitch information"""
vswitch_info_dict = dict()
spec = vswitch_obj.spec
vswitch_info_dict['pnics'] = VswitchInfoManager.serialize_pnics(vswitch_obj)
vswitch_info_dict['mtu'] = vswitch_obj.mtu
vswitch_info_dict['num_ports'] = spec.numPorts

if policy_info:
# Security info
if spec.policy.security:
vswitch_info_dict['security'] = (
[
spec.policy.security.allowPromiscuous,
spec.policy.security.macChanges,
spec.policy.security.forgedTransmits
]
)

# Traffic Shaping info
if spec.policy.shapingPolicy:
vswitch_info_dict['ts'] = spec.policy.shapingPolicy.enabled

# Teaming and failover info
if spec.policy.nicTeaming:
vswitch_info_dict['lb'] = spec.policy.nicTeaming.policy
vswitch_info_dict['notify'] = spec.policy.nicTeaming.notifySwitches
vswitch_info_dict['failback'] = not spec.policy.nicTeaming.rollingOrder
vswitch_info_dict['failover_active'] = spec.policy.nicTeaming.nicOrder.activeNic
vswitch_info_dict['failover_standby'] = spec.policy.nicTeaming.nicOrder.standbyNic
if spec.policy.nicTeaming.failureCriteria.checkBeacon:
vswitch_info_dict['failure_detection'] = "beacon_probing"
else:
vswitch_info_dict['failure_detection'] = "link_status_only"

return vswitch_info_dict

def gather_vswitch_info(self):
"""Gather vSwitch info"""
hosts_vswitch_info = dict()
for host in self.hosts:
network_manager = host.configManager.networkSystem
if network_manager:
temp_switch_dict = dict()
for available_vswitch in network_manager.networkInfo.vswitch:
temp_switch_dict[available_vswitch.name] = dict(
pnics=self.serialize_pnics(available_vswitch),
mtu=available_vswitch.mtu,
# we need to use the spec to get the ports
# otherwise, the output might be different compared to the vswitch config module
# (e.g. 5632 ports instead of 128)
num_ports=available_vswitch.spec.numPorts
)
for vswitch in network_manager.networkInfo.vswitch:
temp_switch_dict[vswitch.name] = self.normalize_vswitch_info(vswitch_obj=vswitch, policy_info=self.policies)
hosts_vswitch_info[host.name] = temp_switch_dict
return hosts_vswitch_info

Expand All @@ -136,6 +191,7 @@ def main():
argument_spec.update(
cluster_name=dict(type='str', required=False),
esxi_hostname=dict(type='str', required=False),
policies=dict(type='bool', required=False, default=False),
)

module = AnsibleModule(
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/targets/vmware_vswitch_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@
- assert:
that:
- switch_info_check_mode.hosts_vswitch_info is defined

- name: Gather vswitch policies info about all hosts in given cluster
vmware_vswitch_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: '{{ esxi1 }}'
validate_certs: false
policies: true
register: switch_policies_info

- debug: var=switch_policies_info

- assert:
that:
- switch_policies_info.hosts_vswitch_info is defined
- switch_policies_info.hosts_vswitch_info[esxi1] is defined
- switch_policies_info.hosts_vswitch_info[esxi1]['vSwitch0'] is defined
- switch_policies_info.hosts_vswitch_info[esxi1]['vSwitch0'].security is defined
- switch_policies_info.hosts_vswitch_info[esxi1]['vSwitch0'].ts is defined
- switch_policies_info.hosts_vswitch_info[esxi1]['vSwitch0'].lb is defined