Skip to content

Commit 7ed1756

Browse files
authored
vmware_dvs_host: Defaults for vmnics and lag_uplinks (#1904)
vmware_dvs_host: Defaults for vmnics and lag_uplinks Fixes: #1516 SUMMARY Remove defaults for vmnics and lag_uplinks. ISSUE TYPE Feature Pull Request COMPONENT NAME vmware_dvs_host ADDITIONAL INFORMATION
1 parent cfea1ae commit 7ed1756

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
breaking_changes:
2+
- vmware_dvs_host - removed defaults for `vmnics` and `lag_uplinks` (https://github.com/ansible-collections/community.vmware/issues/1516).

plugins/modules/vmware_dvs_host.py

+39-18
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@
3636
vmnics:
3737
description:
3838
- The ESXi hosts vmnics to use with the Distributed vSwitch.
39+
- If unset, the current non-LAG uplinks will be kept.
40+
- To remove all non-LAG uplinks, use the empty list V([]).
3941
required: false
4042
type: list
41-
default: []
4243
elements: str
4344
lag_uplinks:
4445
required: false
4546
type: list
46-
default: []
4747
elements: dict
4848
description:
4949
- The ESXi hosts vmnics to use with specific LAGs.
50+
- If unset, the current LAG uplinks will be kept.
51+
- If set, LAG uplinks will be set to I(exactly) this list. So you always have to define the complete LAG uplink configuration;
52+
if you don't, you might loose LAG uplinks.
53+
- To remove all LAG uplinks, use the empty list V([]).
5054
suboptions:
5155
lag:
5256
description:
@@ -156,7 +160,6 @@
156160
class VMwareDvsHost(PyVmomi):
157161
def __init__(self, module):
158162
super(VMwareDvsHost, self).__init__(module)
159-
self.uplink_portgroup = None
160163
self.host = None
161164
self.dv_switch = None
162165
self.desired_state = {}
@@ -174,13 +177,16 @@ def __init__(self, module):
174177
self.module.fail_json(msg="A distributed virtual switch %s "
175178
"does not exist" % self.switch_name)
176179

180+
self.uplink_portgroup = self.find_dvs_uplink_pg()
181+
177182
self.lags = {}
178183
for lag in self.dv_switch.config.lacpGroupConfig:
179184
self.lags[lag.name] = lag
180185

181-
for lag_uplink in self.lag_uplinks:
182-
if lag_uplink['lag'] not in self.lags:
183-
self.module.fail_json(msg="LAG %s not found" % lag_uplink['lag'])
186+
if self.lag_uplinks is not None:
187+
for lag_uplink in self.lag_uplinks:
188+
if lag_uplink['lag'] not in self.lags:
189+
self.module.fail_json(msg="LAG %s not found" % lag_uplink['lag'])
184190

185191
def process_state(self):
186192
dvs_host_states = {
@@ -284,6 +290,10 @@ def set_desired_state(self):
284290
lag_uplinks = []
285291
switch_uplink_ports = {'non_lag': []}
286292

293+
for dvs_host_member in self.dv_switch.config.host:
294+
if dvs_host_member.config.host.name == self.esxi_hostname:
295+
break
296+
287297
portCriteria = vim.dvs.PortCriteria()
288298
portCriteria.host = [self.host]
289299
portCriteria.portgroupKey = self.uplink_portgroup.key
@@ -302,16 +312,30 @@ def set_desired_state(self):
302312
if port.key in self.uplink_portgroup.portKeys and port.key not in lag_uplinks:
303313
switch_uplink_ports['non_lag'].append(port.key)
304314

305-
count = 0
306-
for vmnic in self.vmnics:
307-
self.desired_state[vmnic] = switch_uplink_ports['non_lag'][count]
308-
count += 1
309-
310-
for lag in self.lag_uplinks:
315+
# If defined, use vmnics as non-LAG uplinks
316+
if self.vmnics is not None:
311317
count = 0
312-
for vmnic in lag['vmnics']:
313-
self.desired_state[vmnic] = switch_uplink_ports[lag['lag']][count]
318+
for vmnic in self.vmnics:
319+
self.desired_state[vmnic] = switch_uplink_ports['non_lag'][count]
314320
count += 1
321+
# Otherwise keep current non-LAG uplinks
322+
else:
323+
for pnicSpec in dvs_host_member.config.backing.pnicSpec:
324+
if pnicSpec.uplinkPortKey not in lag_uplinks:
325+
self.desired_state[pnicSpec.pnicDevice] = pnicSpec.uplinkPortKey
326+
327+
# If defined, use lag_uplinks as LAG uplinks
328+
if self.lag_uplinks is not None:
329+
for lag in self.lag_uplinks:
330+
count = 0
331+
for vmnic in lag['vmnics']:
332+
self.desired_state[vmnic] = switch_uplink_ports[lag['lag']][count]
333+
count += 1
334+
# Otherwise keep current LAG uplinks
335+
else:
336+
for pnicSpec in dvs_host_member.config.backing.pnicSpec:
337+
if pnicSpec.uplinkPortKey in lag_uplinks:
338+
self.desired_state[pnicSpec.pnicDevice] = pnicSpec.uplinkPortKey
315339

316340
def check_uplinks(self):
317341
pnic_device = []
@@ -336,8 +360,6 @@ def check_uplinks(self):
336360
return True
337361

338362
def check_dvs_host_state(self):
339-
self.uplink_portgroup = self.find_dvs_uplink_pg()
340-
341363
if self.uplink_portgroup is None:
342364
self.module.fail_json(msg="An uplink portgroup does not exist on"
343365
" the distributed virtual switch %s" % self.switch_name)
@@ -368,7 +390,7 @@ def main():
368390
dict(
369391
esxi_hostname=dict(required=True, type='str'),
370392
switch_name=dict(required=True, type='str'),
371-
vmnics=dict(required=False, type='list', default=[], elements='str'),
393+
vmnics=dict(required=False, type='list', elements='str'),
372394
state=dict(default='present', choices=['present', 'absent'], type='str'),
373395
vendor_specific_config=dict(
374396
type='list',
@@ -381,7 +403,6 @@ def main():
381403
),
382404
lag_uplinks=dict(
383405
type='list',
384-
default=[],
385406
required=False,
386407
elements='dict',
387408
options=dict(

tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ commands =
1616
antsibull-changelog lint
1717
{toxinidir}/tools/check-ignores-order
1818
yamllint -c .yamllint.yaml tests
19+
allowlist_externals =
20+
{toxinidir}/tools/check-ignores-order
1921

2022
[testenv:add_docs]
2123
deps = git+https://github.com/ansible-network/collection_prep

0 commit comments

Comments
 (0)