forked from ansible-collections/community.vmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmware_vmkernel.py
1148 lines (1027 loc) · 46.9 KB
/
vmware_vmkernel.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
# Copyright: (c) 2017-18, Ansible Project
# Copyright: (c) 2017-18, Abhijeet Kasurde <akasurde@redhat.com>
# Copyright: (c) 2018, Christian Kotte <christian.kotte@gmx.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r'''
---
module: vmware_vmkernel
short_description: Manages a VMware VMkernel Adapter of an ESXi host.
description:
- This module can be used to manage the VMKernel adapters / VMKernel network interfaces of an ESXi host.
- The module assumes that the host is already configured with the Port Group in case of a vSphere Standard Switch (vSS).
- The module assumes that the host is already configured with the Distributed Port Group in case of a vSphere Distributed Switch (vDS).
- The module automatically migrates the VMKernel adapter from vSS to vDS or vice versa if present.
author:
- Joseph Callen (@jcpowermac)
- Russell Teague (@mtnbikenc)
- Abhijeet Kasurde (@Akasurde)
- Christian Kotte (@ckotte)
- Nina Loser (@Nina2244)
notes:
- The option O(device) need to be used with DHCP because otherwise it's not possible to check if a VMkernel device is already present
- You can only change from DHCP to static, and vSS to vDS, or vice versa, in one step, without creating a new device, with O(device) specified.
- You can only create the VMKernel adapter on a vDS if authenticated to vCenter and not if authenticated to ESXi.
options:
vswitch_name:
description:
- The name of the vSwitch where to add the VMKernel interface.
- Required parameter only if O(state=present).
- Optional parameter from version 2.5 and onwards.
type: str
aliases: ['vswitch']
dvswitch_name:
description:
- The name of the vSphere Distributed Switch (vDS) where to add the VMKernel interface.
- Required parameter only if O(state=present).
- Optional parameter from version 2.8 and onwards.
type: str
aliases: ['dvswitch']
portgroup_name:
description:
- The name of the port group for the VMKernel interface.
required: true
aliases: ['portgroup']
type: str
network:
description:
- A dictionary of network details.
suboptions:
type:
type: str
description:
- Type of IP assignment.
choices: [ 'static', 'dhcp' ]
default: 'static'
ip_address:
type: str
description:
- Static IP address.
- Required if O(network.type=static).
subnet_mask:
type: str
description:
- Static netmask required.
- Required if O(network.type=static).
default_gateway:
type: str
description: Default gateway (Override default gateway for this adapter).
tcpip_stack:
type: str
description:
- The TCP/IP stack for the VMKernel interface.
choices: [ 'default', 'provisioning', 'vmotion', 'vxlan' ]
default: 'default'
type: dict
default: {
type: 'static',
tcpip_stack: 'default',
}
mtu:
description:
- The MTU for the VMKernel interface.
- The default value of 1500 is valid from version 2.5 and onwards.
default: 1500
type: int
device:
description:
- Search VMkernel adapter by device name.
- The parameter is required only in case of O(network.type=dhcp).
type: str
enable_vsan:
description:
- Enable VSAN traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
enable_vmotion:
description:
- Enable vMotion traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
- You cannot enable vMotion on an additional adapter if you already have an adapter with the vMotion TCP/IP stack configured.
type: bool
default: false
enable_mgmt:
description:
- Enable Management traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
enable_ft:
description:
- Enable Fault Tolerance traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
enable_provisioning:
description:
- Enable Provisioning traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
enable_replication:
description:
- Enable vSphere Replication traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
enable_replication_nfc:
description:
- Enable vSphere Replication NFC traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
enable_backup_nfc:
description:
- Enable vSphere Backup NFC traffic on the VMKernel adapter.
- This option is only allowed if the default TCP/IP stack is used.
type: bool
default: false
state:
description:
- If set to V(present), the VMKernel adapter will be created with the given specifications.
- If set to V(absent), the VMKernel adapter will be removed.
- If set to V(present) and VMKernel adapter exists, the configurations will be updated.
choices: [ present, absent ]
default: present
type: str
esxi_hostname:
description:
- Name of ESXi host to which VMKernel is to be managed.
required: true
type: str
extends_documentation_fragment:
- community.vmware.vmware.documentation
'''
EXAMPLES = r'''
- name: Add Management vmkernel port using static network type
community.vmware.vmware_vmkernel:
hostname: '{{ esxi_hostname }}'
username: '{{ esxi_username }}'
password: '{{ esxi_password }}'
esxi_hostname: '{{ esxi_hostname }}'
vswitch_name: vSwitch0
portgroup_name: PG_0001
network:
type: 'static'
ip_address: 192.168.127.10
subnet_mask: 255.255.255.0
state: present
enable_mgmt: true
delegate_to: localhost
- name: Add Management vmkernel port using DHCP network type
community.vmware.vmware_vmkernel:
hostname: '{{ esxi_hostname }}'
username: '{{ esxi_username }}'
password: '{{ esxi_password }}'
esxi_hostname: '{{ esxi_hostname }}'
vswitch_name: vSwitch0
portgroup_name: PG_0002
state: present
network:
type: 'dhcp'
enable_mgmt: true
delegate_to: localhost
- name: Change IP allocation from static to dhcp
community.vmware.vmware_vmkernel:
hostname: '{{ esxi_hostname }}'
username: '{{ esxi_username }}'
password: '{{ esxi_password }}'
esxi_hostname: '{{ esxi_hostname }}'
vswitch_name: vSwitch0
portgroup_name: PG_0002
state: present
device: vmk1
network:
type: 'dhcp'
enable_mgmt: true
delegate_to: localhost
- name: Delete VMkernel port
community.vmware.vmware_vmkernel:
hostname: '{{ esxi_hostname }}'
username: '{{ esxi_username }}'
password: '{{ esxi_password }}'
esxi_hostname: '{{ esxi_hostname }}'
vswitch_name: vSwitch0
portgroup_name: PG_0002
state: absent
delegate_to: localhost
- name: Add Management vmkernel port to Distributed Switch
community.vmware.vmware_vmkernel:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: '{{ esxi_hostname }}'
dvswitch_name: dvSwitch1
portgroup_name: dvPG_0001
network:
type: 'static'
ip_address: 192.168.127.10
subnet_mask: 255.255.255.0
state: present
enable_mgmt: true
delegate_to: localhost
- name: Add vMotion vmkernel port with vMotion TCP/IP stack
community.vmware.vmware_vmkernel:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: '{{ esxi_hostname }}'
dvswitch_name: dvSwitch1
portgroup_name: dvPG_0001
network:
type: 'static'
ip_address: 192.168.127.10
subnet_mask: 255.255.255.0
tcpip_stack: vmotion
state: present
delegate_to: localhost
'''
RETURN = r'''
result:
description: metadata about VMKernel name
returned: always
type: dict
sample: {
"changed": false,
"msg": "VMkernel Adapter already configured properly",
"device": "vmk1",
"ipv4": "static",
"ipv4_gw": "No override",
"ipv4_ip": "192.168.1.15",
"ipv4_sm": "255.255.255.0",
"mtu": 9000,
"services": "vMotion",
"switch": "vDS"
}
'''
try:
from pyVmomi import vim, vmodl
except ImportError:
pass
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.vmware.plugins.module_utils.vmware import (
PyVmomi, TaskError, vmware_argument_spec, wait_for_task,
find_dvspg_by_name, find_dvs_by_name, get_all_objs
)
from ansible.module_utils._text import to_native
class PyVmomiHelper(PyVmomi):
"""Class to manage VMkernel configuration of an ESXi host system"""
def __init__(self, module):
super(PyVmomiHelper, self).__init__(module)
if self.params['network']:
self.network_type = self.params['network'].get('type')
self.ip_address = self.params['network'].get('ip_address', None)
self.subnet_mask = self.params['network'].get('subnet_mask', None)
self.default_gateway = self.params['network'].get('default_gateway', None)
self.tcpip_stack = self.params['network'].get('tcpip_stack')
self.device = self.params['device']
if self.network_type == 'dhcp' and not self.device:
module.fail_json(msg="device is a required parameter when network type is set to 'dhcp'")
self.mtu = self.params['mtu']
self.enable_vsan = self.params['enable_vsan']
self.enable_vmotion = self.params['enable_vmotion']
self.enable_mgmt = self.params['enable_mgmt']
self.enable_ft = self.params['enable_ft']
self.enable_provisioning = self.params['enable_provisioning']
self.enable_replication = self.params['enable_replication']
self.enable_replication_nfc = self.params['enable_replication_nfc']
self.enable_backup_nfc = self.params['enable_backup_nfc']
self.vswitch_name = self.params['vswitch_name']
self.vds_name = self.params['dvswitch_name']
self.port_group_name = self.params['portgroup_name']
self.esxi_host_name = self.params['esxi_hostname']
hosts = self.get_all_host_objs(esxi_host_name=self.esxi_host_name)
if hosts:
self.esxi_host_obj = hosts[0]
else:
self.module.fail_json(
msg="Failed to get details of ESXi server. Please specify esxi_hostname."
)
if self.network_type == 'static':
if self.module.params['state'] == 'absent':
pass
elif not self.ip_address:
module.fail_json(msg="ip_address is a required parameter when network type is set to 'static'")
elif not self.subnet_mask:
module.fail_json(msg="subnet_mask is a required parameter when network type is set to 'static'")
# find Port Group
if self.vswitch_name:
self.port_group_obj = self.get_port_group_by_name(
host_system=self.esxi_host_obj,
portgroup_name=self.port_group_name,
vswitch_name=self.vswitch_name
)
if not self.port_group_obj:
module.fail_json(msg="Portgroup '%s' not found on vSS '%s'" % (self.port_group_name, self.vswitch_name))
elif self.vds_name:
self.dv_switch_obj = find_dvs_by_name(self.content, self.vds_name)
if not self.dv_switch_obj:
module.fail_json(msg="vDS '%s' not found" % self.vds_name)
self.port_group_obj = find_dvspg_by_name(self.dv_switch_obj, self.port_group_name)
if not self.port_group_obj:
module.fail_json(msg="Portgroup '%s' not found on vDS '%s'" % (self.port_group_name, self.vds_name))
# find VMkernel Adapter
if self.device:
self.vnic = self.get_vmkernel_by_device(device_name=self.device)
else:
# config change (e.g. DHCP to static, or vice versa); doesn't work with virtual port change
self.vnic = self.get_vmkernel_by_portgroup_new(port_group_name=self.port_group_name)
if not self.vnic and self.network_type == 'static':
# vDS to vSS or vSS to vSS (static IP)
self.vnic = self.get_vmkernel_by_ip(ip_address=self.ip_address)
def get_port_group_by_name(self, host_system, portgroup_name, vswitch_name):
"""
Get specific port group by given name
Args:
host_system: Name of Host System
portgroup_name: Name of Port Group
vswitch_name: Name of the vSwitch
Returns: List of port groups by given specifications
"""
portgroups = self.get_all_port_groups_by_host(host_system=host_system)
for portgroup in portgroups:
if portgroup.spec.vswitchName == vswitch_name and portgroup.spec.name == portgroup_name:
return portgroup
return None
def ensure(self):
"""
Manage internal VMKernel management
Returns: NA
"""
host_vmk_states = {
'absent': {
'present': self.host_vmk_delete,
'absent': self.host_vmk_unchange,
},
'present': {
'present': self.host_vmk_update,
'absent': self.host_vmk_create,
}
}
try:
host_vmk_states[self.module.params['state']][self.check_state()]()
except vmodl.RuntimeFault as runtime_fault:
self.module.fail_json(msg=to_native(runtime_fault.msg))
except vmodl.MethodFault as method_fault:
self.module.fail_json(msg=to_native(method_fault.msg))
def get_vmkernel_by_portgroup_new(self, port_group_name=None):
"""
Check if vmkernel available or not
Args:
port_group_name: name of port group
Returns: vmkernel managed object if vmkernel found, false if not
"""
for vnic in self.esxi_host_obj.config.network.vnic:
# check if it's a vSS Port Group
if vnic.spec.portgroup == port_group_name:
return vnic
# check if it's a vDS Port Group
try:
if vnic.spec.distributedVirtualPort.portgroupKey == self.port_group_obj.key:
return vnic
except AttributeError:
pass
return False
def get_vmkernel_by_ip(self, ip_address):
"""
Check if vmkernel available or not
Args:
ip_address: IP address of vmkernel device
Returns: vmkernel managed object if vmkernel found, false if not
"""
for vnic in self.esxi_host_obj.config.network.vnic:
if vnic.spec.ip.ipAddress == ip_address:
return vnic
return None
def get_vmkernel_by_device(self, device_name):
"""
Check if vmkernel available or not
Args:
device_name: name of vmkernel device
Returns: vmkernel managed object if vmkernel found, false if not
"""
for vnic in self.esxi_host_obj.config.network.vnic:
if vnic.device == device_name:
return vnic
return None
def check_state(self):
"""
Check internal state management
Returns: Present if found and absent if not found
"""
return 'present' if self.vnic else 'absent'
def host_vmk_delete(self):
"""
Delete VMKernel
Returns: NA
"""
results = dict(changed=False, msg='')
vmk_device = self.vnic.device
try:
if self.module.check_mode:
results['msg'] = "VMkernel Adapter would be deleted"
else:
self.esxi_host_obj.configManager.networkSystem.RemoveVirtualNic(vmk_device)
results['msg'] = "VMkernel Adapter deleted"
results['changed'] = True
results['device'] = vmk_device
except vim.fault.NotFound as not_found:
self.module.fail_json(
msg="Failed to find vmk to delete due to %s" %
to_native(not_found.msg)
)
except vim.fault.HostConfigFault as host_config_fault:
self.module.fail_json(
msg="Failed to delete vmk due host config issues : %s" %
to_native(host_config_fault.msg)
)
self.module.exit_json(**results)
def host_vmk_unchange(self):
"""
Denote no change in VMKernel
Returns: NA
"""
self.module.exit_json(changed=False)
def host_vmk_update(self):
"""
Update VMKernel with given parameters
Returns: NA
"""
changed = changed_settings = changed_vds = changed_services = \
changed_service_vmotion = changed_service_mgmt = changed_service_ft = \
changed_service_vsan = changed_service_prov = changed_service_rep = changed_service_rep_nfc = changed_service_backup_nfc = False
changed_list = []
results = dict(changed=False, msg='')
results['tcpip_stack'] = self.tcpip_stack
net_stack_instance_key = self.get_api_net_stack_instance(self.tcpip_stack)
if self.vnic.spec.netStackInstanceKey != net_stack_instance_key:
self.module.fail_json(msg="The TCP/IP stack cannot be changed on an existing VMkernel adapter!")
# Check MTU
results['mtu'] = self.mtu
if self.vnic.spec.mtu != self.mtu:
changed_settings = True
changed_list.append("MTU")
results['mtu_previous'] = self.vnic.spec.mtu
# Check IPv4 settings
results['ipv4'] = self.network_type
results['ipv4_ip'] = self.ip_address
results['ipv4_sm'] = self.subnet_mask
if self.default_gateway:
results['ipv4_gw'] = self.default_gateway
else:
results['ipv4_gw'] = "No override"
if self.vnic.spec.ip.dhcp:
if self.network_type == 'static':
changed_settings = True
changed_list.append("IPv4 settings")
results['ipv4_previous'] = "DHCP"
if not self.vnic.spec.ip.dhcp:
if self.network_type == 'dhcp':
changed_settings = True
changed_list.append("IPv4 settings")
results['ipv4_previous'] = "static"
elif self.network_type == 'static':
if self.ip_address != self.vnic.spec.ip.ipAddress:
changed_settings = True
changed_list.append("IP")
results['ipv4_ip_previous'] = self.vnic.spec.ip.ipAddress
if self.subnet_mask != self.vnic.spec.ip.subnetMask:
changed_settings = True
changed_list.append("SM")
results['ipv4_sm_previous'] = self.vnic.spec.ip.subnetMask
if self.default_gateway:
try:
if self.default_gateway != self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway:
changed_settings = True
changed_list.append("GW override")
results['ipv4_gw_previous'] = self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway
except AttributeError:
changed_settings = True
changed_list.append("GW override")
results['ipv4_gw_previous'] = "No override"
else:
try:
if self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway:
changed_settings = True
changed_list.append("GW override")
results['ipv4_gw_previous'] = self.vnic.spec.ipRouteSpec.ipRouteConfig.defaultGateway
except AttributeError:
pass
# Check virtual port (vSS or vDS)
results['portgroup'] = self.port_group_name
dvs_uuid = None
if self.vswitch_name:
results['switch'] = self.vswitch_name
try:
if self.vnic.spec.distributedVirtualPort.switchUuid:
changed_vds = True
changed_list.append("Virtual Port")
dvs_uuid = self.vnic.spec.distributedVirtualPort.switchUuid
except AttributeError:
pass
if changed_vds:
results['switch_previous'] = self.find_dvs_by_uuid(dvs_uuid)
self.dv_switch_obj = find_dvs_by_name(self.content, results['switch_previous'])
results['portgroup_previous'] = self.find_dvspg_by_key(
self.dv_switch_obj, self.vnic.spec.distributedVirtualPort.portgroupKey
)
elif self.vds_name:
results['switch'] = self.vds_name
try:
if self.vnic.spec.distributedVirtualPort.switchUuid != self.dv_switch_obj.uuid:
changed_vds = True
changed_list.append("Virtual Port")
dvs_uuid = self.vnic.spec.distributedVirtualPort.switchUuid
except AttributeError:
changed_vds = True
changed_list.append("Virtual Port")
if changed_vds:
results['switch_previous'] = self.find_dvs_by_uuid(dvs_uuid)
results['portgroup_previous'] = self.vnic.spec.portgroup
portgroups = self.get_all_port_groups_by_host(host_system=self.esxi_host_obj)
for portgroup in portgroups:
if portgroup.spec.name == self.vnic.spec.portgroup:
results['switch_previous'] = portgroup.spec.vswitchName
results['services'] = self.create_enabled_services_string()
# Check configuration of service types (only if default TCP/IP stack is used)
if self.vnic.spec.netStackInstanceKey == 'defaultTcpipStack':
service_type_vmks = self.get_all_vmks_by_service_type()
if (self.enable_vmotion and self.vnic.device not in service_type_vmks['vmotion']) or \
(not self.enable_vmotion and self.vnic.device in service_type_vmks['vmotion']):
changed_services = changed_service_vmotion = True
if (self.enable_mgmt and self.vnic.device not in service_type_vmks['management']) or \
(not self.enable_mgmt and self.vnic.device in service_type_vmks['management']):
changed_services = changed_service_mgmt = True
if (self.enable_ft and self.vnic.device not in service_type_vmks['faultToleranceLogging']) or \
(not self.enable_ft and self.vnic.device in service_type_vmks['faultToleranceLogging']):
changed_services = changed_service_ft = True
if (self.enable_vsan and self.vnic.device not in service_type_vmks['vsan']) or \
(not self.enable_vsan and self.vnic.device in service_type_vmks['vsan']):
changed_services = changed_service_vsan = True
if (self.enable_provisioning and self.vnic.device not in service_type_vmks['vSphereProvisioning']) or \
(not self.enable_provisioning and self.vnic.device in service_type_vmks['vSphereProvisioning']):
changed_services = changed_service_prov = True
if (self.enable_replication and self.vnic.device not in service_type_vmks['vSphereReplication']) or \
(not self.enable_replication and self.vnic.device in service_type_vmks['vSphereReplication']):
changed_services = changed_service_rep = True
if (self.enable_replication_nfc and self.vnic.device not in service_type_vmks['vSphereReplicationNFC']) or \
(not self.enable_replication_nfc and self.vnic.device in service_type_vmks['vSphereReplicationNFC']):
changed_services = changed_service_rep_nfc = True
if (self.enable_backup_nfc and self.vnic.device not in service_type_vmks['vSphereBackupNFC']) or \
(not self.enable_backup_nfc and self.vnic.device in service_type_vmks['vSphereBackupNFC']):
changed_services = changed_service_backup_nfc = True
if changed_services:
changed_list.append("services")
if changed_settings or changed_vds or changed_services:
changed = True
if self.module.check_mode:
changed_suffix = ' would be updated'
else:
changed_suffix = ' updated'
if len(changed_list) > 2:
message = ', '.join(changed_list[:-1]) + ', and ' + str(changed_list[-1])
elif len(changed_list) == 2:
message = ' and '.join(changed_list)
elif len(changed_list) == 1:
message = changed_list[0]
message = "VMkernel Adapter " + message + changed_suffix
if changed_settings or changed_vds:
vnic_config = vim.host.VirtualNic.Specification()
ip_spec = vim.host.IpConfig()
if self.network_type == 'dhcp':
ip_spec.dhcp = True
else:
ip_spec.dhcp = False
ip_spec.ipAddress = self.ip_address
ip_spec.subnetMask = self.subnet_mask
if self.default_gateway:
vnic_config.ipRouteSpec = vim.host.VirtualNic.IpRouteSpec()
vnic_config.ipRouteSpec.ipRouteConfig = vim.host.IpRouteConfig()
vnic_config.ipRouteSpec.ipRouteConfig.defaultGateway = self.default_gateway
else:
vnic_config.ipRouteSpec = vim.host.VirtualNic.IpRouteSpec()
vnic_config.ipRouteSpec.ipRouteConfig = vim.host.IpRouteConfig()
vnic_config.ip = ip_spec
vnic_config.mtu = self.mtu
if changed_vds:
if self.vswitch_name:
vnic_config.portgroup = self.port_group_name
elif self.vds_name:
vnic_config.distributedVirtualPort = vim.dvs.PortConnection()
vnic_config.distributedVirtualPort.switchUuid = self.dv_switch_obj.uuid
vnic_config.distributedVirtualPort.portgroupKey = self.port_group_obj.key
try:
if not self.module.check_mode:
self.esxi_host_obj.configManager.networkSystem.UpdateVirtualNic(self.vnic.device, vnic_config)
except vim.fault.NotFound as not_found:
self.module.fail_json(
msg="Failed to update vmk as virtual network adapter cannot be found %s" %
to_native(not_found.msg)
)
except vim.fault.HostConfigFault as host_config_fault:
self.module.fail_json(
msg="Failed to update vmk due to host config issues : %s" %
to_native(host_config_fault.msg)
)
except vim.fault.InvalidState as invalid_state:
self.module.fail_json(
msg="Failed to update vmk as ipv6 address is specified in an ipv4 only system : %s" %
to_native(invalid_state.msg)
)
except vmodl.fault.InvalidArgument as invalid_arg:
self.module.fail_json(
msg="Failed to update vmk as IP address or Subnet Mask in the IP configuration"
"are invalid or PortGroup does not exist : %s" % to_native(invalid_arg.msg)
)
if changed_services:
changed_list.append("Services")
services_previous = []
vnic_manager = self.esxi_host_obj.configManager.virtualNicManager
if changed_service_mgmt:
if self.vnic.device in service_type_vmks['management']:
services_previous.append('Mgmt')
operation = 'select' if self.enable_mgmt else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='management', operation=operation
)
if changed_service_vmotion:
if self.vnic.device in service_type_vmks['vmotion']:
services_previous.append('vMotion')
operation = 'select' if self.enable_vmotion else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vmotion', operation=operation
)
if changed_service_ft:
if self.vnic.device in service_type_vmks['faultToleranceLogging']:
services_previous.append('FT')
operation = 'select' if self.enable_ft else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='faultToleranceLogging', operation=operation
)
if changed_service_prov:
if self.vnic.device in service_type_vmks['vSphereProvisioning']:
services_previous.append('Prov')
operation = 'select' if self.enable_provisioning else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereProvisioning', operation=operation
)
if changed_service_rep:
if self.vnic.device in service_type_vmks['vSphereReplication']:
services_previous.append('Repl')
operation = 'select' if self.enable_replication else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereReplication', operation=operation
)
if changed_service_rep_nfc:
if self.vnic.device in service_type_vmks['vSphereReplicationNFC']:
services_previous.append('Repl_NFC')
operation = 'select' if self.enable_replication_nfc else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereReplicationNFC', operation=operation
)
if changed_service_backup_nfc:
if self.vnic.device in service_type_vmks['vSphereBackupNFC']:
services_previous.append('Backup_NFC')
operation = 'select' if self.enable_backup_nfc else 'deselect'
self.set_service_type(
vnic_manager=vnic_manager, vmk=self.vnic, service_type='vSphereBackupNFC', operation=operation
)
if changed_service_vsan:
if self.vnic.device in service_type_vmks['vsan']:
services_previous.append('VSAN')
results['vsan'] = self.set_vsan_service_type(self.enable_vsan)
results['services_previous'] = ', '.join(services_previous)
else:
message = "VMkernel Adapter already configured properly"
results['changed'] = changed
results['msg'] = message
results['device'] = self.vnic.device
self.module.exit_json(**results)
def find_dvs_by_uuid(self, uuid):
"""
Find DVS by UUID
Returns: DVS name
"""
dvs_list = get_all_objs(self.content, [vim.DistributedVirtualSwitch])
for dvs in dvs_list:
if dvs.uuid == uuid:
return dvs.summary.name
return None
def find_dvspg_by_key(self, dv_switch, portgroup_key):
"""
Find dvPortgroup by key
Returns: dvPortgroup name
"""
portgroups = dv_switch.portgroup
for portgroup in portgroups:
if portgroup.key == portgroup_key:
return portgroup.name
return None
def set_vsan_service_type(self, enable_vsan):
"""
Set VSAN service type
Returns: result of UpdateVsan_Task
"""
result = None
vsan_system = self.esxi_host_obj.configManager.vsanSystem
vsan_system_config = vsan_system.config
vsan_config = vim.vsan.host.ConfigInfo()
vsan_config.networkInfo = vsan_system_config.networkInfo
current_vsan_vnics = [portConfig.device for portConfig in vsan_system_config.networkInfo.port]
changed = False
result = "%s NIC %s (currently enabled NICs: %s) : " % ("Enable" if enable_vsan else "Disable", self.vnic.device, current_vsan_vnics)
if not enable_vsan:
if self.vnic.device in current_vsan_vnics:
vsan_config.networkInfo.port = list(filter(lambda portConfig: portConfig.device != self.vnic.device, vsan_config.networkInfo.port))
changed = True
else:
if self.vnic.device not in current_vsan_vnics:
vsan_port_config = vim.vsan.host.ConfigInfo.NetworkInfo.PortConfig()
vsan_port_config.device = self.vnic.device
if vsan_config.networkInfo is None:
vsan_config.networkInfo = vim.vsan.host.ConfigInfo.NetworkInfo()
vsan_config.networkInfo.port = [vsan_port_config]
else:
vsan_config.networkInfo.port.append(vsan_port_config)
changed = True
if not self.module.check_mode and changed:
try:
vsan_task = vsan_system.UpdateVsan_Task(vsan_config)
task_result = wait_for_task(vsan_task)
if task_result[0]:
result += "Success"
else:
result += "Failed"
except TaskError as task_err:
self.module.fail_json(
msg="Failed to set service type to vsan for %s : %s" % (self.vnic.device, to_native(task_err))
)
if self.module.check_mode:
result += "Dry-run"
return result
def host_vmk_create(self):
"""
Create VMKernel
Returns: NA
"""
results = dict(changed=False, message='')
if self.vswitch_name:
results['switch'] = self.vswitch_name
elif self.vds_name:
results['switch'] = self.vds_name
results['portgroup'] = self.port_group_name
vnic_config = vim.host.VirtualNic.Specification()
ip_spec = vim.host.IpConfig()
results['ipv4'] = self.network_type
if self.network_type == 'dhcp':
ip_spec.dhcp = True
else:
ip_spec.dhcp = False
results['ipv4_ip'] = self.ip_address
results['ipv4_sm'] = self.subnet_mask
ip_spec.ipAddress = self.ip_address
ip_spec.subnetMask = self.subnet_mask
if self.default_gateway:
vnic_config.ipRouteSpec = vim.host.VirtualNic.IpRouteSpec()
vnic_config.ipRouteSpec.ipRouteConfig = vim.host.IpRouteConfig()
vnic_config.ipRouteSpec.ipRouteConfig.defaultGateway = self.default_gateway
vnic_config.ip = ip_spec
results['mtu'] = self.mtu
vnic_config.mtu = self.mtu
results['tcpip_stack'] = self.tcpip_stack
vnic_config.netStackInstanceKey = self.get_api_net_stack_instance(self.tcpip_stack)
vmk_device = None
try:
if self.module.check_mode:
results['msg'] = "VMkernel Adapter would be created"
else:
if self.vswitch_name:
vmk_device = self.esxi_host_obj.configManager.networkSystem.AddVirtualNic(
self.port_group_name,
vnic_config
)
elif self.vds_name:
vnic_config.distributedVirtualPort = vim.dvs.PortConnection()
vnic_config.distributedVirtualPort.switchUuid = self.dv_switch_obj.uuid
vnic_config.distributedVirtualPort.portgroupKey = self.port_group_obj.key
vmk_device = self.esxi_host_obj.configManager.networkSystem.AddVirtualNic(portgroup="", nic=vnic_config)
results['msg'] = "VMkernel Adapter created"
results['changed'] = True
results['device'] = vmk_device
if self.network_type != 'dhcp':
if self.default_gateway:
results['ipv4_gw'] = self.default_gateway
else:
results['ipv4_gw'] = "No override"
results['services'] = self.create_enabled_services_string()
except vim.fault.AlreadyExists as already_exists:
self.module.fail_json(
msg="Failed to add vmk as portgroup already has a virtual network adapter %s" %
to_native(already_exists.msg)
)
except vim.fault.HostConfigFault as host_config_fault:
self.module.fail_json(
msg="Failed to add vmk due to host config issues : %s" %
to_native(host_config_fault.msg)
)
except vim.fault.InvalidState as invalid_state:
self.module.fail_json(
msg="Failed to add vmk as ipv6 address is specified in an ipv4 only system : %s" %
to_native(invalid_state.msg)
)
except vmodl.fault.InvalidArgument as invalid_arg:
self.module.fail_json(
msg="Failed to add vmk as IP address or Subnet Mask in the IP configuration "
"are invalid or PortGroup does not exist : %s" % to_native(invalid_arg.msg)
)
# do service type configuration
if self.tcpip_stack == 'default' and not all(
option is False for option in [self.enable_vsan, self.enable_vmotion,
self.enable_mgmt, self.enable_ft,
self.enable_provisioning, self.enable_replication,
self.enable_replication_nfc, self.enable_backup_nfc]):
self.vnic = self.get_vmkernel_by_device(device_name=vmk_device)
# VSAN
if self.enable_vsan:
results['vsan'] = self.set_vsan_service_type(self.enable_vsan)
# Other service type
host_vnic_manager = self.esxi_host_obj.configManager.virtualNicManager
if self.enable_vmotion:
self.set_service_type(host_vnic_manager, self.vnic, 'vmotion')
if self.enable_mgmt:
self.set_service_type(host_vnic_manager, self.vnic, 'management')
if self.enable_ft:
self.set_service_type(host_vnic_manager, self.vnic, 'faultToleranceLogging')
if self.enable_provisioning:
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereProvisioning')
if self.enable_replication:
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereReplication')
if self.enable_replication_nfc:
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereReplicationNFC')
if self.enable_backup_nfc:
self.set_service_type(host_vnic_manager, self.vnic, 'vSphereBackupNFC')
self.module.exit_json(**results)
def set_service_type(self, vnic_manager, vmk, service_type, operation='select'):
"""
Set service type to given VMKernel
Args:
vnic_manager: Virtual NIC manager object
vmk: VMkernel managed object
service_type: Name of service type
operation: Select to select service type, deselect to deselect service type
"""
try:
if operation == 'select':
if not self.module.check_mode:
vnic_manager.SelectVnicForNicType(service_type, vmk.device)
elif operation == 'deselect':
if not self.module.check_mode:
vnic_manager.DeselectVnicForNicType(service_type, vmk.device)
except vmodl.fault.InvalidArgument as invalid_arg:
self.module.fail_json(
msg="Failed to %s VMK service type '%s' on '%s' due to : %s" %
(operation, service_type, vmk.device, to_native(invalid_arg.msg))
)
def get_all_vmks_by_service_type(self):
"""
Return information about service types and VMKernel
Returns: Dictionary of service type as key and VMKernel list as value