36
36
vmnics:
37
37
description:
38
38
- 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([]).
39
41
required: false
40
42
type: list
41
- default: []
42
43
elements: str
43
44
lag_uplinks:
44
45
required: false
45
46
type: list
46
- default: []
47
47
elements: dict
48
48
description:
49
49
- 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([]).
50
54
suboptions:
51
55
lag:
52
56
description:
156
160
class VMwareDvsHost (PyVmomi ):
157
161
def __init__ (self , module ):
158
162
super (VMwareDvsHost , self ).__init__ (module )
159
- self .uplink_portgroup = None
160
163
self .host = None
161
164
self .dv_switch = None
162
165
self .desired_state = {}
@@ -174,13 +177,16 @@ def __init__(self, module):
174
177
self .module .fail_json (msg = "A distributed virtual switch %s "
175
178
"does not exist" % self .switch_name )
176
179
180
+ self .uplink_portgroup = self .find_dvs_uplink_pg ()
181
+
177
182
self .lags = {}
178
183
for lag in self .dv_switch .config .lacpGroupConfig :
179
184
self .lags [lag .name ] = lag
180
185
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' ])
184
190
185
191
def process_state (self ):
186
192
dvs_host_states = {
@@ -284,6 +290,10 @@ def set_desired_state(self):
284
290
lag_uplinks = []
285
291
switch_uplink_ports = {'non_lag' : []}
286
292
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
+
287
297
portCriteria = vim .dvs .PortCriteria ()
288
298
portCriteria .host = [self .host ]
289
299
portCriteria .portgroupKey = self .uplink_portgroup .key
@@ -302,16 +312,30 @@ def set_desired_state(self):
302
312
if port .key in self .uplink_portgroup .portKeys and port .key not in lag_uplinks :
303
313
switch_uplink_ports ['non_lag' ].append (port .key )
304
314
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 :
311
317
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 ]
314
320
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
315
339
316
340
def check_uplinks (self ):
317
341
pnic_device = []
@@ -336,8 +360,6 @@ def check_uplinks(self):
336
360
return True
337
361
338
362
def check_dvs_host_state (self ):
339
- self .uplink_portgroup = self .find_dvs_uplink_pg ()
340
-
341
363
if self .uplink_portgroup is None :
342
364
self .module .fail_json (msg = "An uplink portgroup does not exist on"
343
365
" the distributed virtual switch %s" % self .switch_name )
@@ -368,7 +390,7 @@ def main():
368
390
dict (
369
391
esxi_hostname = dict (required = True , type = 'str' ),
370
392
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' ),
372
394
state = dict (default = 'present' , choices = ['present' , 'absent' ], type = 'str' ),
373
395
vendor_specific_config = dict (
374
396
type = 'list' ,
@@ -381,7 +403,6 @@ def main():
381
403
),
382
404
lag_uplinks = dict (
383
405
type = 'list' ,
384
- default = [],
385
406
required = False ,
386
407
elements = 'dict' ,
387
408
options = dict (
0 commit comments