-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathvmware_guest.py
3607 lines (3249 loc) · 170 KB
/
vmware_guest.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 -*-
#
# This module is also sponsored by E.T.A.I. (www.etai.fr)
# 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
DOCUMENTATION = r'''
---
module: vmware_guest
short_description: Manages virtual machines in vCenter
description: >
This module can be used to create new virtual machines from templates or other virtual machines,
manage power state of virtual machine such as power on, power off, suspend, shutdown, reboot, restart etc.,
modify various virtual machine components like network, disk, customization etc.,
rename a virtual machine and remove a virtual machine with associated components.
author:
- Loic Blot (@nerzhul) <[email protected]>
- Philippe Dellaert (@pdellaert) <[email protected]>
- Abhijeet Kasurde (@Akasurde) <[email protected]>
requirements:
- python >= 2.6
- PyVmomi
notes:
- Please make sure that the user used for M(community.vmware.vmware_guest) has the correct level of privileges.
- For example, following is the list of minimum privileges required by users to create virtual machines.
- " DataStore > Allocate Space"
- " Virtual Machine > Configuration > Add New Disk"
- " Virtual Machine > Configuration > Add or Remove Device"
- " Virtual Machine > Inventory > Create New"
- " Network > Assign Network"
- " Resource > Assign Virtual Machine to Resource Pool"
- "Module may require additional privileges as well, which may be required for gathering facts - e.g. ESXi configurations."
- Tested on vSphere 5.5, 6.0, 6.5 and 6.7.
- Use SCSI disks instead of IDE when you want to expand online disks by specifying a SCSI controller.
- Uses SysPrep for Windows VM (depends on 'guest_id' parameter match 'win') with PyVmomi.
- In order to change the VM's parameters (e.g. number of CPUs), the VM must be powered off unless the hot-add
support is enabled and the C(state=present) must be used to apply the changes.
- "For additional information please visit Ansible VMware community wiki - U(https://github.com/ansible/community/wiki/VMware)."
options:
state:
description:
- Specify the state the virtual machine should be in.
- If C(state) is set to C(present) and virtual machine exists, ensure the virtual machine configurations conforms to task arguments.
- If C(state) is set to C(absent) and virtual machine exists, then the specified virtual machine is removed with it's associated components.
- If C(state) is set to one of the following C(poweredon), C(powered-on), C(poweredoff), C(powered-off),
C(present), C(restarted), C(suspended) and virtual machine does not exists, virtual machine is deployed with the given parameters.
- If C(state) is set to C(poweredon) or C(powered-on) and virtual machine exists with powerstate other than powered on,
then the specified virtual machine is powered on.
- If C(state) is set to C(poweredoff) or C(powered-off) and virtual machine exists with powerstate other than powered off,
then the specified virtual machine is powered off.
- If C(state) is set to C(restarted) and virtual machine exists, then the virtual machine is restarted.
- If C(state) is set to C(suspended) and virtual machine exists, then the virtual machine is set to suspended mode.
- If C(state) is set to C(shutdownguest) or C(shutdown-guest) and virtual machine exists, then the virtual machine is shutdown.
- If C(state) is set to C(rebootguest) or C(reboot-guest) and virtual machine exists, then the virtual machine is rebooted.
- Powerstate C(powered-on) and C(powered-off) is added in version 2.10.
default: present
type: str
choices: [ absent, poweredon, powered-on, poweredoff, powered-off, present, rebootguest, reboot-guest, restarted, suspended, shutdownguest, shutdown-guest]
name:
description:
- Name of the virtual machine to work with.
- Virtual machine names in vCenter are not necessarily unique, which may be problematic, see C(name_match).
- If multiple virtual machines with same name exists, then C(folder) is required parameter to
identify uniqueness of the virtual machine.
- This parameter is required, if C(state) is set to C(poweredon), C(powered-on), C(poweredoff), C(powered-off),
C(present), C(restarted), C(suspended) and virtual machine does not exists.
- This parameter is case sensitive.
type: str
name_match:
description:
- If multiple virtual machines matching the name, use the first or last found.
default: 'first'
choices: [ 'first', 'last' ]
type: str
uuid:
description:
- UUID of the virtual machine to manage if known, this is VMware's unique identifier.
- This is required if C(name) is not supplied.
- If virtual machine does not exists, then this parameter is ignored.
- Please note that a supplied UUID will be ignored on virtual machine creation, as VMware creates the UUID internally.
type: str
use_instance_uuid:
description:
- Whether to use the VMware instance UUID rather than the BIOS UUID.
default: false
type: bool
template:
description:
- Template or existing virtual machine used to create new virtual machine.
- If this value is not set, virtual machine is created without using a template.
- If the virtual machine already exists, this parameter will be ignored.
- This parameter is case sensitive.
- From version 2.8 onwards, absolute path to virtual machine or template can be used.
aliases: [ 'template_src' ]
type: str
is_template:
description:
- Flag the instance as a template.
- This will mark the given virtual machine as template.
- Note, this may need to be done in a dedicated task invocation that is not making
any other changes. For example, user cannot change the state from powered-on to
powered-off AND save as template in the same task.
- See M(community.vmware.vmware_guest) source for more details.
default: false
type: bool
folder:
description:
- Destination folder, absolute path to find an existing guest or create the new guest.
- "The folder should include the datacenter. ESXi's datacenter is ha-datacenter."
- This parameter is case sensitive.
- 'If multiple machines are found with same name, this parameter is used to identify'
- 'Examples:'
- ' folder: /ha-datacenter/vm'
- ' folder: ha-datacenter/vm'
- ' folder: /datacenter1/vm'
- ' folder: datacenter1/vm'
- ' folder: /datacenter1/vm/folder1'
- ' folder: datacenter1/vm/folder1'
- ' folder: /folder1/datacenter1/vm'
- ' folder: folder1/datacenter1/vm'
- ' folder: /folder1/datacenter1/vm/folder2'
type: str
hardware:
type: dict
description:
- "Manage virtual machine's hardware attributes."
- All parameters case sensitive.
suboptions:
hotadd_cpu:
type: bool
description: Allow virtual CPUs to be added while the virtual machine is running.
hotremove_cpu:
type: bool
description: Allow virtual CPUs to be removed while the virtual machine is running.
hotadd_memory:
type: bool
description: Allow memory to be added while the virtual machine is running.
memory_mb:
type: int
description: Amount of memory in MB.
num_cpus:
type: int
description:
- Number of CPUs.
- C(num_cpus) must be a multiple of C(num_cpu_cores_per_socket).
- For example, to create a VM with 2 sockets of 4 cores, specify C(num_cpus) as 8 and C(num_cpu_cores_per_socket) as 4.
num_cpu_cores_per_socket:
type: int
description: Number of Cores Per Socket.
scsi:
type: str
description:
- Valid values are C(buslogic), C(lsilogic), C(lsilogicsas) and C(paravirtual).
- C(paravirtual) is default.
choices: [ 'buslogic', 'lsilogic', 'lsilogicsas', 'paravirtual' ]
secure_boot:
type: bool
description: Whether to enable or disable (U)EFI secure boot.
version_added: '1.11.0'
memory_reservation_lock:
type: bool
description:
- If set C(true), memory resource reservation for the virtual machine.
max_connections:
type: int
description:
- Maximum number of active remote display connections for the virtual machines.
mem_limit:
type: int
description:
- The memory utilization of a virtual machine will not exceed this limit.
- Unit is MB.
mem_reservation:
type: int
description: The amount of memory resource that is guaranteed available to the virtual machine.
aliases: [ 'memory_reservation' ]
cpu_limit:
type: int
description:
- The CPU utilization of a virtual machine will not exceed this limit.
- Unit is MHz.
cpu_reservation:
type: int
description: The amount of CPU resource that is guaranteed available to the virtual machine.
version:
type: str
description:
- The Virtual machine hardware versions.
- Default is 10 (ESXi 5.5 and onwards).
- If set to C(latest), the specified virtual machine will be upgraded to the most current hardware version supported on the host.
- C(latest) is added in Ansible 2.10.
- Please check VMware documentation for correct virtual machine hardware version.
- Incorrect hardware version may lead to failure in deployment. If hardware version is already equal to the given.
boot_firmware:
type: str
description: Choose which firmware should be used to boot the virtual machine.
choices: [ 'bios', 'efi' ]
nested_virt:
type: bool
description:
- Enable nested virtualization.
virt_based_security:
type: bool
description:
- Enable Virtualization Based Security feature for Windows on ESXi 6.7 and later, from hardware version 14.
- Supported Guest OS are Windows 10 64 bit, Windows Server 2016, Windows Server 2019 and later.
- The firmware of virtual machine must be EFI and secure boot must be enabled.
- Virtualization Based Security depends on nested virtualization and Intel Virtualization Technology for Directed I/O.
- Deploy on unsupported ESXi, hardware version or firmware may lead to failure or deployed VM with unexpected configurations.
iommu:
type: bool
description: Flag to specify if I/O MMU is enabled for this virtual machine.
version_added: '1.11.0'
guest_id:
type: str
description:
- Set the guest ID.
- This parameter is case sensitive.
- C(rhel7_64Guest) for virtual machine with RHEL7 64 bit.
- C(centos64Guest) for virtual machine with CentOS 64 bit.
- C(ubuntu64Guest) for virtual machine with Ubuntu 64 bit.
- This field is required when creating a virtual machine, not required when creating from the template.
- >
Valid values are referenced here:
U(https://code.vmware.com/apis/358/doc/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html)
disk:
description:
- A list of disks to add.
- This parameter is case sensitive.
- Shrinking disks is not supported.
- Removing existing disks of the virtual machine is not supported.
- 'Attributes C(controller_type), C(controller_number), C(unit_number) are used to configure multiple types of disk
controllers and disks for creating or reconfiguring virtual machine. Added in Ansible 2.10.'
type: list
elements: dict
suboptions:
size:
description:
- Disk storage size.
- Please specify storage unit like [kb, mb, gb, tb].
type: str
size_kb:
description: Disk storage size in kb.
type: int
size_mb:
description: Disk storage size in mb.
type: int
size_gb:
description: Disk storage size in gb.
type: int
size_tb:
description: Disk storage size in tb.
type: int
type:
description:
- Type of disk.
- If C(thin) specified, disk type is set to thin disk.
- If C(eagerzeroedthick) specified, disk type is set to eagerzeroedthick disk. Added Ansible 2.5.
- If not specified, disk type is inherited from the source VM or template when cloned and thick disk, no eagerzero otherwise.
type: str
choices: [ 'thin', 'thick', 'eagerzeroedthick' ]
datastore:
type: str
description:
- The name of datastore which will be used for the disk.
- If C(autoselect_datastore) is set to True, will select the less used datastore whose name contains this "disk.datastore" string.
filename:
type: str
description:
- Existing disk image to be used.
- Filename must already exist on the datastore.
- Specify filename string in C([datastore_name] path/to/file.vmdk) format. Added in Ansible 2.8.
autoselect_datastore:
type: bool
description:
- Select the less used datastore.
- C(disk.datastore) and C(disk.autoselect_datastore) will not be used if C(datastore) is specified outside this C(disk) configuration.
disk_mode:
type: str
choices: ['persistent', 'independent_persistent', 'independent_nonpersistent']
description:
- Type of disk mode.
- Added in Ansible 2.6.
- If C(persistent) specified, changes are immediately and permanently written to the virtual disk. This is default.
- If C(independent_persistent) specified, same as persistent, but not affected by snapshots.
- If C(independent_nonpersistent) specified, changes to virtual disk are made to a redo log and discarded at power off,
but not affected by snapshots.
controller_type:
type: str
choices: ['buslogic', 'lsilogic', 'lsilogicsas', 'paravirtual', 'sata', 'nvme']
description:
- Type of disk controller.
- C(nvme) controller type support starts on ESXi 6.5 with VM hardware version C(version) 13.
Set this type on not supported ESXi or VM hardware version will lead to failure in deployment.
- When set to C(sata), please make sure C(unit_number) is correct and not used by SATA CDROMs.
- If set to C(sata) type, please make sure C(controller_number) and C(unit_number) are set correctly when C(cdrom) also set to C(sata) type.
controller_number:
type: int
choices: [0, 1, 2, 3]
description:
- Disk controller bus number.
- The maximum number of same type controller is 4 per VM.
unit_number:
type: int
description:
- Disk Unit Number.
- Valid value range from 0 to 15 for SCSI controller, except 7.
- Valid value range from 0 to 14 for NVME controller.
- Valid value range from 0 to 29 for SATA controller.
- C(controller_type), C(controller_number) and C(unit_number) are required when creating or reconfiguring VMs
with multiple types of disk controllers and disks.
- When creating new VM, the first configured disk in the C(disk) list will be "Hard Disk 1".
cdrom:
description:
- A list of CD-ROM configurations for the virtual machine. Added in version 2.9.
- Providing CD-ROM configuration as dict is deprecated and will be removed VMware collection 4.0.0.
Please use a list instead.
- 'Parameters C(controller_type), C(controller_number), C(unit_number), C(state) are added for a list of CD-ROMs
configuration support.'
- For C(ide) controller, hot-add or hot-remove CD-ROM is not supported.
type: raw
suboptions:
type:
type: str
description:
- The type of CD-ROM, valid options are C(none), C(client) or C(iso).
- With C(none) the CD-ROM will be disconnected but present.
- The default value is C(client).
iso_path:
type: str
description:
- The datastore path to the ISO file to use, in the form of C([datastore1] path/to/file.iso).
- Required if type is set C(iso).
controller_type:
type: str
description:
- Valid options are C(ide) and C(sata).
- Default value is C(ide).
- When set to C(sata), please make sure C(unit_number) is correct and not used by SATA disks.
controller_number:
type: int
description:
- For C(ide) controller, valid value is 0 or 1.
- For C(sata) controller, valid value is 0 to 3.
unit_number:
type: int
description:
- For CD-ROM device attach to C(ide) controller, valid value is 0 or 1.
- For CD-ROM device attach to C(sata) controller, valid value is 0 to 29.
- C(controller_number) and C(unit_number) are mandatory attributes.
state:
type: str
description:
- Valid value is C(present) or C(absent).
- Default is C(present).
- If set to C(absent), then the specified CD-ROM will be removed.
resource_pool:
description:
- Use the given resource pool for virtual machine operation.
- This parameter is case sensitive.
- Resource pool should be child of the selected host parent.
type: str
wait_for_ip_address:
description:
- Wait until vCenter detects an IP address for the virtual machine.
- This requires vmware-tools (vmtoolsd) to properly work after creation.
- "vmware-tools needs to be installed on the given virtual machine in order to work with this parameter."
default: false
type: bool
wait_for_ip_address_timeout:
description:
- Define a timeout (in seconds) for the wait_for_ip_address parameter.
default: '300'
type: int
wait_for_customization_timeout:
description:
- Define a timeout (in seconds) for the wait_for_customization parameter.
- Be careful when setting this value since the time guest customization took may differ among guest OSes.
default: '3600'
type: int
wait_for_customization:
description:
- Wait until vCenter detects all guest customizations as successfully completed.
- When enabled, the VM will automatically be powered on.
- "If vCenter does not detect guest customization start or succeed, failed events after time
C(wait_for_customization_timeout) parameter specified, warning message will be printed and task result is fail."
default: false
type: bool
state_change_timeout:
description:
- If the C(state) is set to C(shutdownguest), by default the module will return immediately after sending the shutdown signal.
- If this argument is set to a positive integer, the module will instead wait for the virtual machine to reach the poweredoff state.
- The value sets a timeout in seconds for the module to wait for the state change.
default: 0
type: int
snapshot_src:
description:
- Name of the existing snapshot to use to create a clone of a virtual machine.
- This parameter is case sensitive.
- While creating linked clone using C(linked_clone) parameter, this parameter is required.
type: str
linked_clone:
description:
- Whether to create a linked clone from the snapshot specified.
- If specified, then C(snapshot_src) is required parameter.
default: false
type: bool
force:
description:
- Ignore warnings and complete the actions.
- This parameter is useful while removing virtual machine which is powered on state.
- 'This module reflects the VMware vCenter API and UI workflow, as such, in some cases the `force` flag will
be mandatory to perform the action to ensure you are certain the action has to be taken, no matter what the consequence.
This is specifically the case for removing a powered on the virtual machine when C(state) is set to C(absent).'
default: false
type: bool
delete_from_inventory:
description:
- Whether to delete Virtual machine from inventory or delete from disk.
default: False
type: bool
datacenter:
description:
- Destination datacenter for the deploy operation.
- This parameter is case sensitive.
default: ha-datacenter
type: str
cluster:
description:
- The cluster name where the virtual machine will run.
- This is a required parameter, if C(esxi_hostname) is not set.
- C(esxi_hostname) and C(cluster) are mutually exclusive parameters.
- This parameter is case sensitive.
type: str
esxi_hostname:
description:
- The ESXi hostname where the virtual machine will run.
- This is a required parameter, if C(cluster) is not set.
- C(esxi_hostname) and C(cluster) are mutually exclusive parameters.
- This parameter is case sensitive.
type: str
advanced_settings:
description:
- Define a list of advanced settings to be added to the VMX config.
- An advanced settings object takes two fields C(key) and C(value).
- Incorrect key and values will be ignored.
elements: dict
type: list
version_added: '1.7.0'
annotation:
description:
- A note or annotation to include in the virtual machine.
type: str
aliases: [ 'notes' ]
customvalues:
description:
- Define a list of custom values to set on virtual machine.
- A custom value object takes two fields C(key) and C(value).
- Incorrect key and values will be ignored.
elements: dict
type: list
networks:
description:
- A list of networks (in the order of the NICs).
- Removing NICs is not allowed, while reconfiguring the virtual machine.
- All parameters and VMware object names are case sensitive.
type: list
elements: dict
suboptions:
name:
type: str
description:
- Name of the portgroup or distributed virtual portgroup for this interface.
- Required per entry.
- When specifying distributed virtual portgroup make sure given C(esxi_hostname) or C(cluster) is associated with it.
vlan:
type: int
description:
- VLAN number for this interface.
- Required per entry.
device_type:
type: str
description:
- Virtual network device.
- Valid value can be one of C(e1000), C(e1000e), C(pcnet32), C(vmxnet2), C(vmxnet3), C(sriov).
- C(vmxnet3) is default.
- Optional per entry.
- Used for virtual hardware.
mac:
type: str
description:
- Customize MAC address.
- Optional per entry.
- Used for virtual hardware.
dvswitch_name:
type: str
description:
- Name of the distributed vSwitch.
- Optional per entry.
- Used for virtual hardware.
type:
type: str
description:
- Type of IP assignment.
- Valid values are one of C(dhcp), C(static).
- C(dhcp) is default.
- Optional per entry.
- Used for OS customization.
ip:
type: str
description:
- Static IP address. Implies C(type=static).
- Optional per entry.
- Used for OS customization.
netmask:
type: str
description:
- Static netmask required for C(ip).
- Optional per entry.
- Used for OS customization.
gateway:
type: str
description:
- Static gateway.
- Optional per entry.
- Used for OS customization.
dns_servers:
type: str
description:
- DNS servers for this network interface (Windows).
- Optional per entry.
- Used for OS customization.
domain:
type: str
description:
- Domain name for this network interface (Windows).
- Optional per entry.
- Used for OS customization.
connected:
type: bool
description:
- Indicates whether the NIC is currently connected.
version_added: '1.5.0'
start_connected:
type: bool
description:
- Specifies whether or not to connect the device when the virtual machine starts.
customization:
description:
- Parameters for OS customization when cloning from the template or the virtual machine, or apply to the existing virtual machine directly.
- Not all operating systems are supported for customization with respective vCenter version,
please check VMware documentation for respective OS customization.
- For supported customization operating system matrix, (see U(http://partnerweb.vmware.com/programs/guestOS/guest-os-customization-matrix.pdf))
- All parameters and VMware object names are case sensitive.
- Linux based OSes requires Perl package to be installed for OS customizations.
suboptions:
existing_vm:
type: bool
description:
- If set to C(True), do OS customization on the specified virtual machine directly.
- Common for Linux and Windows customization.
dns_servers:
type: list
elements: str
description:
- List of DNS servers to configure.
- Common for Linux and Windows customization.
dns_suffix:
type: list
elements: str
description:
- List of domain suffixes, also known as DNS search path.
- Default C(domain) parameter.
- Common for Linux and Windows customization.
domain:
type: str
description:
- DNS domain name to use.
- Common for Linux and Windows customization.
hostname:
type: str
description:
- Computer hostname.
- Default is shortened C(name) parameter.
- Allowed characters are alphanumeric (uppercase and lowercase) and minus, rest of the characters are dropped as per RFC 952.
- Common for Linux and Windows customization.
timezone:
type: str
description:
- Timezone.
- See List of supported time zones for different vSphere versions in Linux/Unix.
- Common for Linux and Windows customization.
- L(Windows, https://msdn.microsoft.com/en-us/library/ms912391.aspx).
hwclockUTC:
type: bool
description:
- Specifies whether the hardware clock is in UTC or local time.
- Specific to Linux customization.
autologon:
type: bool
description:
- Auto logon after virtual machine customization.
- Specific to Windows customization.
autologoncount:
type: int
description:
- Number of autologon after reboot.
- Specific to Windows customization.
- Ignored if C(autologon) is unset or set to C(False).
- If unset, 1 will be used.
domainadmin:
type: str
description:
- User used to join in AD domain.
- Required if C(joindomain) specified.
- Specific to Windows customization.
domainadminpassword:
type: str
description:
- Password used to join in AD domain.
- Required if C(joindomain) specified.
- Specific to Windows customization.
fullname:
type: str
description:
- Server owner name.
- Specific to Windows customization.
- If unset, "Administrator" will be used as a fall-back.
joindomain:
type: str
description:
- AD domain to join.
- Not compatible with C(joinworkgroup).
- Specific to Windows customization.
joinworkgroup:
type: str
description:
- Workgroup to join.
- Not compatible with C(joindomain).
- Specific to Windows customization.
- If unset, "WORKGROUP" will be used as a fall-back.
orgname:
type: str
description:
- Organisation name.
- Specific to Windows customization.
- If unset, "ACME" will be used as a fall-back.
password:
type: str
description:
- Local administrator password.
- Specific to Windows customization.
productid:
type: str
description:
- Product ID.
- Specific to Windows customization.
runonce:
type: list
elements: str
description:
- List of commands to run at first user logon.
- Specific to Windows customization.
type: dict
vapp_properties:
description:
- A list of vApp properties.
- 'For full list of attributes and types refer to: U(https://code.vmware.com/apis/704/vsphere/vim.vApp.PropertyInfo.html)'
type: list
elements: dict
suboptions:
id:
type: str
description:
- Property ID.
- Required per entry.
value:
type: str
description:
- Property value.
type:
type: str
description:
- Value type, string type by default.
operation:
type: str
description:
- The C(remove) attribute is required only when removing properties.
customization_spec:
description:
- Unique name identifying the requested customization specification.
- This parameter is case sensitive.
- If set, then overrides C(customization) parameter values.
type: str
datastore:
description:
- Specify datastore or datastore cluster to provision virtual machine.
- This parameter takes precedence over C(disk.datastore) parameter.
- This parameter can be used to override datastore or datastore cluster setting
of the virtual machine when deployed from the template.
- Please see example for more usage.
type: str
convert:
description:
- Specify convert disk type while cloning template or virtual machine.
choices: [ 'thin', 'thick', 'eagerzeroedthick' ]
type: str
extends_documentation_fragment:
- community.vmware.vmware.documentation
'''
EXAMPLES = r'''
- name: Create a virtual machine on given ESXi hostname
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: /DC1/vm/
name: test_vm_0001
state: poweredon
guest_id: centos64Guest
# This is hostname of particular ESXi server on which user wants VM to be deployed
esxi_hostname: "{{ esxi_hostname }}"
disk:
- size_gb: 10
type: thin
datastore: datastore1
hardware:
memory_mb: 512
num_cpus: 4
scsi: paravirtual
networks:
- name: VM Network
mac: aa:bb:dd:aa:00:14
ip: 10.10.10.100
netmask: 255.255.255.0
device_type: vmxnet3
wait_for_ip_address: true
wait_for_ip_address_timeout: 600
delegate_to: localhost
register: deploy_vm
- name: Create a virtual machine from a template
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: /testvms
name: testvm_2
state: poweredon
template: template_el7
disk:
- size_gb: 10
type: thin
datastore: g73_datastore
# Add another disk from an existing VMDK
- filename: "[datastore1] testvms/testvm_2_1/testvm_2_1.vmdk"
hardware:
memory_mb: 512
num_cpus: 6
num_cpu_cores_per_socket: 3
scsi: paravirtual
memory_reservation_lock: True
mem_limit: 8096
mem_reservation: 4096
cpu_limit: 8096
cpu_reservation: 4096
max_connections: 5
hotadd_cpu: True
hotremove_cpu: True
hotadd_memory: False
version: 12 # Hardware version of virtual machine
boot_firmware: "efi"
cdrom:
- controller_number: 0
unit_number: 0
state: present
type: iso
iso_path: "[datastore1] livecd.iso"
networks:
- name: VM Network
mac: aa:bb:dd:aa:00:14
wait_for_ip_address: true
delegate_to: localhost
register: deploy
- name: Clone a virtual machine from Windows template and customize
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: datacenter1
cluster: cluster
name: testvm-2
template: template_windows
networks:
- name: VM Network
ip: 192.168.1.100
netmask: 255.255.255.0
gateway: 192.168.1.1
mac: aa:bb:dd:aa:00:14
domain: my_domain
dns_servers:
- 192.168.1.1
- 192.168.1.2
- vlan: 1234
type: dhcp
customization:
autologon: true
dns_servers:
- 192.168.1.1
- 192.168.1.2
domain: my_domain
password: new_vm_password
runonce:
- powershell.exe -ExecutionPolicy Unrestricted -File C:\Windows\Temp\ConfigureRemotingForAnsible.ps1 -ForceNewSSLCert -EnableCredSSP
delegate_to: localhost
- name: Clone a virtual machine from Linux template and customize
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter }}"
state: present
folder: /DC1/vm
template: "{{ template }}"
name: "{{ vm_name }}"
cluster: DC1_C1
networks:
- name: VM Network
ip: 192.168.10.11
netmask: 255.255.255.0
wait_for_ip_address: True
customization:
domain: "{{ guest_domain }}"
dns_servers:
- 8.9.9.9
- 7.8.8.9
dns_suffix:
- example.com
- example2.com
delegate_to: localhost
- name: Rename a virtual machine (requires the virtual machine's uuid)
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
uuid: "{{ vm_uuid }}"
name: new_name
state: present
delegate_to: localhost
- name: Remove a virtual machine by uuid
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
uuid: "{{ vm_uuid }}"
state: absent
delegate_to: localhost
- name: Remove a virtual machine from inventory
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: vm_name
delete_from_inventory: True
state: absent
delegate_to: localhost
- name: Manipulate vApp properties
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: vm_name
state: present
vapp_properties:
- id: remoteIP
category: Backup
label: Backup server IP
type: string
value: 10.10.10.1
- id: old_property
operation: remove
delegate_to: localhost
- name: Set powerstate of a virtual machine to poweroff by using UUID
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
uuid: "{{ vm_uuid }}"
state: poweredoff
delegate_to: localhost
- name: Deploy a virtual machine in a datastore different from the datastore of the template
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ vm_name }}"
state: present
template: "{{ template_name }}"
# Here datastore can be different which holds template
datastore: "{{ virtual_machine_datastore }}"
hardware:
memory_mb: 512
num_cpus: 2
scsi: paravirtual
delegate_to: localhost
- name: Create a diskless VM
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
state: poweredoff
cluster: "{{ ccr1 }}"
name: diskless_vm
folder: /Asia-Datacenter1/vm
guest_id: centos64Guest
datastore: "{{ ds1 }}"
hardware:
memory_mb: 1024
num_cpus: 2
num_cpu_cores_per_socket: 1
- name: Create a VM with multiple disks of different disk controller types
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: /DC1/vm/
name: test_vm_multi_disks
state: poweredoff
guest_id: centos64Guest
datastore: datastore1
disk:
- size_gb: 10
controller_type: 'nvme'
controller_number: 0
unit_number: 0
- size_gb: 10
controller_type: 'paravirtual'
controller_number: 0
unit_number: 1
- size_gb: 10
controller_type: 'sata'
controller_number: 0
unit_number: 2
hardware:
memory_mb: 512
num_cpus: 4
version: 14
networks:
- name: VM Network
device_type: vmxnet3
delegate_to: localhost
register: deploy_vm
'''
RETURN = r'''
instance:
description: metadata about the new virtual machine
returned: always
type: dict
sample: None
'''
import re
import time
import string
HAS_PYVMOMI = False
try:
from pyVmomi import vim, vmodl
HAS_PYVMOMI = True
except ImportError:
pass
from random import randint
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.network import is_mac
from ansible.module_utils._text import to_text, to_native
from ansible_collections.community.vmware.plugins.module_utils.vmware import (
find_obj,
gather_vm_facts,
get_all_objs,
compile_folder_path_for_object,
serialize_spec,