Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_vswitch_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r'''
13 ---
14 module: vmware_vswitch_info
15 short_description: Gathers info about an ESXi host's vswitch configurations
16 description:
17 - This module can be used to gather information about an ESXi host's vswitch configurations when ESXi hostname or Cluster name is given.
18 - The vSphere Client shows the value for the number of ports as elastic from vSphere 5.5 and above.
19 - Other tools like esxcli might show the number of ports as 1536 or 5632.
20 - See U(https://kb.vmware.com/s/article/2064511) for more details.
21 author:
22 - Abhijeet Kasurde (@Akasurde)
23 options:
24 policies:
25 description:
26 - Gather information about Security, Traffic Shaping, as well as Teaming and failover.
27 - The property C(ts) stands for Traffic Shaping and C(lb) for Load Balancing.
28 type: bool
29 default: false
30 cluster_name:
31 description:
32 - Name of the cluster.
33 - Info about vswitch belonging to every ESXi host systems under this cluster will be returned.
34 - If O(esxi_hostname) is not given, this parameter is required.
35 type: str
36 esxi_hostname:
37 description:
38 - ESXi hostname to gather information from.
39 - If O(cluster_name) is not given, this parameter is required.
40 type: str
41 extends_documentation_fragment:
42 - vmware.vmware.base_options
43
44 '''
45
46 EXAMPLES = r'''
47 - name: Gather vswitch info about all ESXi Host in given Cluster
48 community.vmware.vmware_vswitch_info:
49 hostname: '{{ vcenter_hostname }}'
50 username: '{{ vcenter_username }}'
51 password: '{{ vcenter_password }}'
52 cluster_name: '{{ cluster_name }}'
53 delegate_to: localhost
54 register: all_hosts_vswitch_info
55
56 - name: Gather vswitch info about ESXi Host
57 community.vmware.vmware_vswitch_info:
58 hostname: '{{ vcenter_hostname }}'
59 username: '{{ vcenter_username }}'
60 password: '{{ vcenter_password }}'
61 esxi_hostname: '{{ esxi_hostname }}'
62 delegate_to: localhost
63 register: all_vswitch_info
64 '''
65
66 RETURN = r'''
67 hosts_vswitch_info:
68 description: metadata about host's vswitch configuration
69 returned: on success
70 type: dict
71 sample: {
72 "10.76.33.218": {
73 "vSwitch0": {
74 "mtu": 1500,
75 "num_ports": 128,
76 "pnics": [
77 "vmnic0"
78 ],
79 "failback": true,
80 "failover_active": ["vmnic0"],
81 "failover_standby": [],
82 "failure_detection": "link_status_only",
83 "lb": "loadbalance_srcid",
84 "notify": true,
85 "security": [false, false, false],
86 "ts": false
87 },
88 "vSwitch_0011": {
89 "mtu": 1500,
90 "num_ports": 128,
91 "pnics": [
92 "vmnic2",
93 "vmnic1"
94 ],
95 "failback": true,
96 "failover_active": ["vmnic1"],
97 "failover_standby": ["vmnic2"],
98 "failure_detection": "link_status_only",
99 "lb": "loadbalance_srcid",
100 "notify": true,
101 "security": [false, false, false],
102 "ts": false,
103 },
104 },
105 }
106 '''
107
108 from ansible.module_utils.basic import AnsibleModule
109 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
110 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
111
112
113 class VswitchInfoManager(PyVmomi):
114 """Class to gather vSwitch info"""
115
116 def __init__(self, module):
117 super(VswitchInfoManager, self).__init__(module)
118 cluster_name = self.params.get('cluster_name', None)
119 esxi_host_name = self.params.get('esxi_hostname', None)
120 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
121 if not self.hosts:
122 self.module.fail_json(msg="Failed to find host system.")
123 self.policies = self.params.get('policies')
124
125 @staticmethod
126 def serialize_pnics(vswitch_obj):
127 """Get pnic names"""
128 pnics = []
129 for pnic in vswitch_obj.pnic:
130 # vSwitch contains all PNICs as string in format of 'key-vim.host.PhysicalNic-vmnic0'
131 pnics.append(pnic.split("-", 3)[-1])
132 return pnics
133
134 @staticmethod
135 def normalize_vswitch_info(vswitch_obj, policy_info):
136 """Create vSwitch information"""
137 vswitch_info_dict = dict()
138 spec = vswitch_obj.spec
139 vswitch_info_dict['pnics'] = VswitchInfoManager.serialize_pnics(vswitch_obj)
140 vswitch_info_dict['mtu'] = vswitch_obj.mtu
141 vswitch_info_dict['num_ports'] = spec.numPorts
142
143 if policy_info:
144 # Security info
145 if spec.policy.security:
146 vswitch_info_dict['security'] = (
147 [
148 spec.policy.security.allowPromiscuous,
149 spec.policy.security.macChanges,
150 spec.policy.security.forgedTransmits
151 ]
152 )
153
154 # Traffic Shaping info
155 if spec.policy.shapingPolicy:
156 vswitch_info_dict['ts'] = spec.policy.shapingPolicy.enabled
157
158 # Teaming and failover info
159 if spec.policy.nicTeaming:
160 vswitch_info_dict['lb'] = spec.policy.nicTeaming.policy
161 vswitch_info_dict['notify'] = spec.policy.nicTeaming.notifySwitches
162 vswitch_info_dict['failback'] = not spec.policy.nicTeaming.rollingOrder
163 vswitch_info_dict['failover_active'] = spec.policy.nicTeaming.nicOrder.activeNic
164 vswitch_info_dict['failover_standby'] = spec.policy.nicTeaming.nicOrder.standbyNic
165 if spec.policy.nicTeaming.failureCriteria.checkBeacon:
166 vswitch_info_dict['failure_detection'] = "beacon_probing"
167 else:
168 vswitch_info_dict['failure_detection'] = "link_status_only"
169
170 return vswitch_info_dict
171
172 def gather_vswitch_info(self):
173 """Gather vSwitch info"""
174 hosts_vswitch_info = dict()
175 for host in self.hosts:
176 network_manager = host.configManager.networkSystem
177 if network_manager:
178 temp_switch_dict = dict()
179 for vswitch in network_manager.networkInfo.vswitch:
180 temp_switch_dict[vswitch.name] = self.normalize_vswitch_info(vswitch_obj=vswitch, policy_info=self.policies)
181 hosts_vswitch_info[host.name] = temp_switch_dict
182 return hosts_vswitch_info
183
184
185 def main():
186 """Main"""
187 argument_spec = base_argument_spec()
188 argument_spec.update(
189 cluster_name=dict(type='str', required=False),
190 esxi_hostname=dict(type='str', required=False),
191 policies=dict(type='bool', required=False, default=False),
192 )
193
194 module = AnsibleModule(
195 argument_spec=argument_spec,
196 required_one_of=[
197 ['cluster_name', 'esxi_hostname'],
198 ],
199 supports_check_mode=True
200 )
201
202 vmware_vswitch_mgr = VswitchInfoManager(module)
203 module.exit_json(changed=False, hosts_vswitch_info=vmware_vswitch_mgr.gather_vswitch_info())
204
205
206 if __name__ == "__main__":
207 main()