1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
# Copyright: (c) 2018, Christian Kotte <christian.kotte@gmx.de>
# 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
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['deprecated'],
'supported_by': 'community'
}
DOCUMENTATION = r'''
---
module: vmware_portgroup_facts
deprecated:
removed_in: '2.13'
why: Deprecated in favour of C(_info) module.
alternative: Use M(vmware_portgroup_info) instead.
short_description: Gathers facts about an ESXi host's Port Group configuration
description:
- This module can be used to gather facts about an ESXi host's Port Group configuration when ESXi hostname or Cluster name is given.
version_added: '2.6'
author:
- Abhijeet Kasurde (@Akasurde)
- Christian Kotte (@ckotte)
notes:
- Tested on vSphere 6.5
- The C(vswitch_name) property is deprecated starting from Ansible v2.12
requirements:
- python >= 2.6
- PyVmomi
options:
policies:
description:
- Gather facts 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
version_added: 2.8
cluster_name:
description:
- Name of the cluster.
- Facts will be returned for all hostsystem belonging to this cluster name.
- If C(esxi_hostname) is not given, this parameter is required.
type: str
esxi_hostname:
description:
- ESXi hostname to gather facts from.
- If C(cluster_name) is not given, this parameter is required.
type: str
extends_documentation_fragment: vmware.documentation
'''
EXAMPLES = r'''
- name: Gather portgroup facts about all ESXi Host in given Cluster
vmware_portgroup_facts:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
cluster_name: '{{ cluster_name }}'
delegate_to: localhost
- name: Gather portgroup facts about ESXi Host system
vmware_portgroup_facts:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: '{{ esxi_hostname }}'
delegate_to: localhost
'''
RETURN = r'''
hosts_portgroup_facts:
description: metadata about host's portgroup configuration
returned: on success
type: dict
sample: {
"esx01": [
{
"failback": true,
"failover_active": ["vmnic0", "vmnic1"],
"failover_standby": [],
"failure_detection": "link_status_only",
"lb": "loadbalance_srcid",
"notify": true,
"portgroup": "Management Network",
"security": [false, false, false],
"ts": "No override",
"vlan_id": 0,
"vswitch": "vSwitch0",
"vswitch_name": "vSwitch0"
},
{
"failback": true,
"failover_active": ["vmnic2"],
"failover_standby": ["vmnic3"],
"failure_detection": "No override",
"lb": "No override",
"notify": true,
"portgroup": "vMotion",
"security": [false, false, false],
"ts": "No override",
"vlan_id": 33,
"vswitch": "vSwitch1",
"vswitch_name": "vSwitch1"
}
]
}
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import vmware_argument_spec, PyVmomi
class PortgroupFactsManager(PyVmomi):
"""Class to manage Port Group facts"""
def __init__(self, module):
super(PortgroupFactsManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
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 normalize_pg_info(portgroup_obj, policy_facts):
"""Create Port Group information"""
pg_info_dict = dict()
spec = portgroup_obj.spec
pg_info_dict['portgroup'] = spec.name
pg_info_dict['vlan_id'] = spec.vlanId
# NOTE: the property vswitch_name is deprecated starting from Ansible v2.12
pg_info_dict['vswitch_name'] = spec.vswitchName
pg_info_dict['vswitch'] = spec.vswitchName
if policy_facts:
# Security facts
if spec.policy.security:
promiscuous_mode = spec.policy.security.allowPromiscuous
mac_changes = spec.policy.security.macChanges
forged_transmits = spec.policy.security.forgedTransmits
pg_info_dict['security'] = (
["No override" if promiscuous_mode is None else promiscuous_mode,
"No override" if mac_changes is None else mac_changes,
"No override" if forged_transmits is None else forged_transmits]
)
else:
pg_info_dict['security'] = ["No override", "No override", "No override"]
# Traffic Shaping facts
if spec.policy.shapingPolicy and spec.policy.shapingPolicy.enabled is not None:
pg_info_dict['ts'] = portgroup_obj.spec.policy.shapingPolicy.enabled
else:
pg_info_dict['ts'] = "No override"
# Teaming and failover facts
if spec.policy.nicTeaming:
if spec.policy.nicTeaming.policy is None:
pg_info_dict['lb'] = "No override"
else:
pg_info_dict['lb'] = spec.policy.nicTeaming.policy
if spec.policy.nicTeaming.notifySwitches is None:
pg_info_dict['notify'] = "No override"
else:
pg_info_dict['notify'] = spec.policy.nicTeaming.notifySwitches
if spec.policy.nicTeaming.rollingOrder is None:
pg_info_dict['failback'] = "No override"
else:
pg_info_dict['failback'] = not spec.policy.nicTeaming.rollingOrder
if spec.policy.nicTeaming.nicOrder is None:
pg_info_dict['failover_active'] = "No override"
pg_info_dict['failover_standby'] = "No override"
else:
pg_info_dict['failover_active'] = spec.policy.nicTeaming.nicOrder.activeNic
pg_info_dict['failover_standby'] = spec.policy.nicTeaming.nicOrder.standbyNic
if spec.policy.nicTeaming.failureCriteria and spec.policy.nicTeaming.failureCriteria.checkBeacon is None:
pg_info_dict['failure_detection'] = "No override"
else:
if spec.policy.nicTeaming.failureCriteria.checkBeacon:
pg_info_dict['failure_detection'] = "beacon_probing"
else:
pg_info_dict['failure_detection'] = "link_status_only"
else:
pg_info_dict['lb'] = "No override"
pg_info_dict['notify'] = "No override"
pg_info_dict['failback'] = "No override"
pg_info_dict['failover_active'] = "No override"
pg_info_dict['failover_standby'] = "No override"
pg_info_dict['failure_detection'] = "No override"
return pg_info_dict
def gather_host_portgroup_facts(self):
"""Gather Port Group facts per ESXi host"""
hosts_pg_facts = dict()
for host in self.hosts:
pgs = host.config.network.portgroup
hosts_pg_facts[host.name] = []
for portgroup in pgs:
hosts_pg_facts[host.name].append(
self.normalize_pg_info(portgroup_obj=portgroup, policy_facts=self.policies)
)
return hosts_pg_facts
def main():
"""Main"""
argument_spec = vmware_argument_spec()
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(
argument_spec=argument_spec,
required_one_of=[
['cluster_name', 'esxi_hostname'],
],
supports_check_mode=True
)
host_pg_mgr = PortgroupFactsManager(module)
module.exit_json(changed=False, hosts_portgroup_facts=host_pg_mgr.gather_host_portgroup_facts())
if __name__ == "__main__":
main()
|