vmware.vmware.vm module – Create, update, or delete a virtual machine.
Note
This module is part of the vmware.vmware collection (version 2.5.0).
You might already have this collection installed if you are using the ansible package.
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install vmware.vmware.
To use it in a playbook, specify: vmware.vmware.vm.
Synopsis
This module is used to create, update, or delete virtual machines.
Although this module will attempt to validate your configuration, it is not feasible to validate all possible combinations of parameters. You may encounter errors when starting VMs with invalid configurations.
Error messages may not always be very helpful. Checking the vCenter task logs or VM edit settings page may have additional messages.
Parameters
Parameter |
Comments |
|---|---|
Whether to allow the VM to be powered off and on when required by the changes detected by the module. For example, if the module detects CPU changes and hot add is not enabled, you can enable this parameter to allow the VM to be powered off, updated, and then powered on automatically. If this is set to false, a failure will occur if the VM needs to be powered off before changes can be applied. A “hard” power off is performed when the VM is powered off. If you do not want this, you could use this module in check mode, vmware.vmware.vm_powerstate module to power off the VM if needed, and then this module again to apply the changes. Choices:
|
|
CD-ROMs to manage on the VM. The order of this list does matter, since you cannot specify the CDROM index number in vSphere. In other words, the first CDROM in this list will always manage the first CDROM on the VM. |
|
The mode of the CD-ROM when acting as a client device. One of If this is not set and the CD-ROM is being created, passthrough will be used.
Choices:
|
|
Whether to connect the CD-ROM to the VM at power on. This parameter is ignored if If this is set to true, the CD-ROM will be connected to the VM at power on. If this is set to false, the CD-ROM will remain disconnected from the VM at power on. Choices:
|
|
Specifies the controller, bus, and unit number of the CD-ROM. Only SATA and IDE controllers are supported. The format of this value should be like ‘SATA(0:0)’ or ‘IDE(0:1)’. CD-ROM controllers referenced in this attribute must be configured in the corresponding controller section. The exception to this are the two IDE controllers, which are automatically added to the VM. The device node should be unique across all other devices (CD-ROMs, disks, etc.) on the VM. |
|
The ISO media path to use for the CD-ROM. This must be a valid datastore path that is accessible to the host of the VM. One of A datastore path should be like ‘[my-datastore-name] some/folder/path/My.iso’. A content library item can be referenced by its full datastore path only. |
|
Whether to remove CD-ROMs that are not specified in the If this is set to true, any CD-ROMs that are not specified in the If this is set to false, the module will ignore CD-ROMs that are not specified in the Choices:
|
|
The cluster in which to place the VM. This is required when creating a new VM. You cannot use this module to modify the placement of a VM once it has been created. This parameter is ignored if the VM already exists. |
|
Options related to CPU resource allocation. This is required when creating a new VM. |
|
The number of CPU cores to add to the VM. This is required when creating a new VM. |
|
The number of cores per socket to use for the VM. If this is defined, |
|
Whether to enable CPU hot add. This allows you to add CPUs to the VM while it is powered on. Encryption ( Choices:
|
|
Whether to enable CPU hot remove. This allows you to remove CPUs from the VM while it is powered on. Encryption ( Choices:
|
|
Whether to enable Virtual CPU Performance Monitoring Counters (VPMC). Choices:
|
|
The maximum amount of CPU resources (Mhz) the VM can use. |
|
The amount of CPU resource (Mhz) that is guaranteed available to the virtual machine. |
|
The custom number of shares of CPU allocated to this virtual machine. You can set If this is defined, |
|
The allocation level of CPU resources for the virtual machine. If Choices:
|
|
The datacenter in which to place the VM. This is required when creating a new VM. You cannot use this module to modify the placement of a VM once it has been created. This parameter is ignored if the VM already exists. |
|
The datastore on which to place the VM. This is required when creating a new VM, or when creating new disks. You cannot use this module to modify the placement of a VM once it has been created. This parameter is ignored if the VM or disk already exists. |
|
The datastore cluster on which to place the VM. This is required when creating a new VM. This is required when creating a new VM, or when creating new disks. You cannot use this module to modify the placement of a VM once it has been created. This parameter is ignored if the VM or disk already exists. |
|
Whether to delete the VM from the datastore when removing it, or just remove the VM from the vSphere inventory. If this is set to true, the VM will be deleted from the datastore when it is removed. If this is set to false, the VM will remain on the datastore when it is removed. This parameter is only used when Choices:
|
|
Disks to manage on the VM. Reducing disk size is not supported. At least one disk is required when creating a new VM. Controllers (except IDE) referenced by the |
|
The datastore or datastore cluster where this disk should be stored. The host that the VM is running on must have access to this datastore. This can be a name or a MOID. If using a name, the first datastore (or cluster) that matches the name will be used. If this is not specified, the disk will be created on the same datastore as the VM. This is only used when creating a new disk. If it is specified for an existing disk, it is ignored. Only one of |
|
Specifies the controller, bus, and unit number of the disk. The format of this value should be like ‘SCSI(0:0)’ or ‘IDE(0:1)’. Disk controllers referenced in this attribute must be configured in the corresponding controller section. The exception to this are the two IDE controllers, which are automatically added to the VM. |
|
If true, disk sharing will be enabled. This enabled multi-writer mode, allowing multiple VMs to write to the same disk. If false, disk sharing will be disabled. This will cause the disk to be exclusive to the VM. If this is not specified, new disks will be created with sharing disabled. Sharing depends on the storage backing type. If your storage does not support sharing, enabling sharing will cause the module to fail with an incompatible device backing error. Choices:
|
|
The filename of an existing disk to attach to the VM. This file must already exist on the datastore. This is only used when creating a new disk. If it is specified for an existing disk, it is ignored. An example filename is ‘[my-datastore-name] some/folder/path/My.vmdk’. Only one of |
|
The mode of the disk. Supported values are dependent on the disk storage location.
If this is not specified, new disks will be created with If you chose a mode that is not supported by the disk storage type, the module will fail with an incompatible device backing error. Choices:
|
|
The provisioning type for the disk. This is only used when creating a new disk. If it is specified for an existing disk, it is ignored. Choices:
|
|
The size of the disk. The format of this value should be like ‘100gb’ or ‘100mb’. Supported units are ‘kb’, ‘mb’, ‘gb’, ‘tb’. Note that disk size cannot be decreased at any time. |
|
Whether to detach disks that are to be removed, or delete them entirely. If this is set to true, any disks that are removed will be detached from the VM but remain on the datastore. If this is set to false, any disks that are removed will be deleted from the datastore. This parameter only applies to individual disk removal. If the entire VM is being removed, this parameter is ignored and Choices:
|
|
Whether to remove disks that are not specified in the If this is set to true, any disks that are not specified in the If this is set to false, the module will ignore disks that are not specified in the Choices:
|
|
The ESXi host on which to place the VM. This is required when creating a new VM. You cannot use this module to modify the placement of a VM once it has been created. This parameter is ignored if the VM already exists. |
|
Destination folder, absolute or relative path to find an existing guest. Should be the full folder path, with or without the ‘datacenter/vm/’ prefix For example ‘datacenter_name/vm/path/to/folder’ or ‘path/to/folder’. You cannot use this module to modify the placement of a VM once it has been created. |
|
The guest ID of the VM. Guest IDs are pre-defined by VMware. For example see https://developer.broadcom.com/xapis/vsphere-web-services-api/latest/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html This is required when creating a new VM. |
|
Specify the hardware version for the new VM. This value should be an integer, where the actual version is ‘vmx-<value>’. This parameter is ignored if the VM already exists. Upgrading hardware version is not supported in this module. If this is not specified, the highest available hardware version will be used. Your environment must support the hardware version you specify. Specifying an unsupported hardware version will cause a vSphere error when deploying the VM. |
|
The hostname or IP address of the vSphere vCenter server. If the value is not specified in the task, the value of environment variable |
|
Options related to memory resource allocation. This is required when creating a new VM. |
|
Whether to enable memory hot add. This allows you to add memory to the VM while it is powered on. Encryption ( Choices:
|
|
The maximum amount of memory the VM can use. |
|
The amount of memory resource that is guaranteed available to the VM. Only one of This value must be less than or equal to the VMs total memory in MB. |
|
Whether to reserve (lock) all memory allocated for the VM. Only one of This will cause VMware to reserve all memory allocated for the VM, meaning that the memory will not be available to other VMs even if this VM is not actively using it. Choices:
|
|
The custom number of shares of memory allocated to this virtual machine. You can set If this is defined, |
|
The allocation level of memory resources for the virtual machine. If Choices:
|
|
The amount of memory in mb to add to the VM. Memory cannot be changed while the VM is powered on, unless memory hot add is already enabled. This parameter is required when creating a new VM. |
|
Name of the virtual machine to manage. Virtual machine names in vCenter are not necessarily unique, which may be problematic, see This is required when the VM does not exist, or if |
|
If multiple virtual machines matching the name, use the first or last found. Choices:
|
|
A list of network adapters to manage on the VM. Due to limitations in the VMware API, you cannot change the type of a network adapter once it has been created using this module. The order of this list does matter, since you cannot specify the network adapter index number in vSphere. In other words, the first network adapter in this list will always manage the network adapter assigned to index 0. The network adapter types defined in this parameter must match the types of any existing adapters on the VM, in the same order that they are specified. For example, the first adapter in this list must have the same type as the first adapter attached to the VM (if one exists). Any adapters in this list that do not exist on the VM will be created. Portgroups must already exist; this module does not create them. |
|
The type of the adapter. This is required when creating a new adapter. Note that this cannot be changed once the adapter has been created. Choices:
|
|
Specifies whether or not to connect the network adapter when the virtual machine starts. Choices:
|
|
Indicates whether the NIC is currently connected. Choices:
|
|
The maximum amount of network resources the network adapter can use. The unit is Mbps. |
|
The MAC address of the network adapter. If you want to use a generated or automatic mac address, set this to ‘automatic’. If not specified and this is a new adapter, a random MAC address will be assigned. If not specified and this is an existing adapter, the MAC address will not be changed. |
|
The name or MOID of the standard or distributed virtual portgroup for this interface. The portgroup must already exist. |
|
The amount of network resources reserved for the network adapter. The unit is Mbps. |
|
The percentage of network resources allocated to the network adapter. If setting this, it should be between 0 and 100. Only one of |
|
The pre-defined allocation level of network resources for the network adapter. Only one of Choices:
|
|
Whether to remove network adapters that are not specified in the If this is set to true, any network adapters that are not specified in the If this is set to false, the module will ignore network adapters beyond those listed in the If you do not specify Choices:
|
|
A list of non-volatile DIMMs to manage on the VM. Any NVDIMMs in this list that do not exist on the VM will be created. If any NVDIMMs are specified, the module will automatically add and manage a NVDIMM controller on the VM. If you remove all NVDIMMs, the NVDIMM controller will be removed as well. You must have a PMem datastore selected for the host that the VM is running on before adding NVDIMMs to the VM. |
|
The amount of memory to add to the VM. Memory cannot be changed while the VM is powered on, unless memory hot add is already enabled. This parameter is required when creating a new VM. |
|
Whether to remove NVDIMMs that are not specified in the If this is set to true, any NVDIMMs that are not specified in the If this is set to false, the module will ignore NVDIMMs beyond those listed in the Choices:
|
|
NVMe device controllers to manage on the VM. If a controller is not specified, it will be removed from the VM. You may only specify four NVMe controllers per VM. The order of this list does not matter, i.e. you may specify bus numbers out of order and do not need to specify all bus numbers (See Valid unit numbers for NVMe controllers are 0-63. |
|
The bus number of the NVMe controller. This is used to identify the controller and is required. Valid bus numbers are 0 to 3, inclusive. |
|
The bus sharing mode of the controller. If this is not set, noSharing will be used for new controllers. Choices:
|
|
Whether to remove NVMe controllers that are not specified in the If this is set to true, any NVMe controllers that are not specified in the If this is set to false, the module will ignore NVMe controllers that are not specified in the Choices:
|
|
The password of the vSphere vCenter server. If the value is not specified in the task, the value of environment variable |
|
The port number of the vSphere vCenter server. If the value is not specified in the task, the value of environment variable Default: |
|
The address of a proxy that will receive all HTTPS requests and relay them. The format is a hostname or a IP. If the value is not specified in the task, the value of environment variable |
|
The port of the HTTP proxy that will receive all HTTPS requests and relay them. If the value is not specified in the task, the value of environment variable |
|
The resource pool in which to place the VM. This is required when creating a new VM. You cannot use this module to modify the placement of a VM once it has been created. This parameter is ignored if the VM already exists. |
|
SATA controllers to manage on the VM. You may only specify four SATA controllers per VM. Valid unit numbers for SATA controllers are 0-29. The order of this list does not matter, i.e. you may specify bus numbers out of order and do not need to specify all bus numbers (See |
|
The bus number of the SATA controller. This is used to identify the controller and is required. Valid bus numbers are 0 to 3, inclusive. |
|
Whether to remove SATA controllers that are not specified in the If this is set to true, any SATA controllers that are not specified in the If this is set to false, the module will ignore SATA controllers that are not specified in the Choices:
|
|
SCSI device controllers to manage on the VM. If a controller is not specified, it will be removed from the VM. You may only specify four SCSI controllers per VM. The order of this list does not matter, i.e. you may specify bus numbers out of order and do not need to specify all bus numbers (See Valid unit numbers for SCSI controllers are 0-15, except for the reserved unit 7. |
|
The bus number of the SCSI controller. This is used to identify the controller and is required. Valid bus numbers are 0 to 3, inclusive. |
|
The bus sharing mode of the controller. If this is not set, noSharing will be used for new controllers. Choices:
|
|
The sub-type of the SCSI controller. Changing the type essentially removes the old controller and adds a new one in its place. Your VM should be powered off and dependent devices should support this kind of change. Choices:
|
|
Whether to remove SCSI controllers that are not specified in the If this is set to true, any SCSI controllers that are not specified in the If this is set to false, the module will ignore SCSI controllers that are not specified in the Choices:
|
|
Whether to ensure the VM is present or absent. Choices:
|
|
The timeout in seconds for the module to wait for the VM to be created or updated. Default: |
|
USB device controllers to manage on the VM. You may only specify two USB controllers per VM, and only one of each type. The order of this list does matter, since you cannot specify the controller/bus number in vSphere. In other words, the first controller in this list will always manage the controller assigned to bus 0. The ESXi host that the VM is running on must have the required hardware and firmware to support the USB controller type. |
|
The type of the USB controller. Choices:
|
|
Whether to remove USB controllers that are not specified in the If this is set to true, any USB controllers that are not specified in the If this is set to false, the module will ignore USB controllers beyond those listed in the Choices:
|
|
Whether to use the VMware instance UUID rather than the BIOS UUID. Choices:
|
|
The username of the vSphere vCenter server. If the value is not specified in the task, the value of environment variable |
|
Allows connection when SSL certificates are not valid. Set to If the value is not specified in the task, the value of environment variable Choices:
|
|
Advanced and miscellaneous options for the VM, including things like BIOS settings, remote console settings, and encryption settings. |
|
The boot firmware to use for the VM. Encryption ( Choices:
|
|
Whether to enable encryption for the VM. If this is set to false, you can still modify Secure boot ( Encryption requires EFI boot firmware ( Memory hot-add ( Choices:
|
|
Whether to enable hardware assisted virtualization. If true, the VM will be able to use hardware assisted, or nested, virtualization. Choices:
|
|
Whether to enable IO Memory Management Unit (IO MMU). Choices:
|
|
Whether to enable secure boot for the VM. Only (U)EFI boot firmware is supported. Secure boot cannot be enabled if encryption ( Choices:
|
|
Whether to enable the Virtualization Based Security feature for Windows on at least ESXi 6.7, 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. Choices:
|
|
Modify how the VM’s Fault Tolerance (FT) replication is encrypted, if encryption is enabled.
Choices:
|
|
Modify how the VM can be migrated using vMotion, if encryption is enabled.
Choices:
|
|
The maximum number of remote console sessions that can be established to the VM. Must be a value between 0 and 40 |
Notes
Note
All modules require API write access and hence are not supported on a free ESXi license.
All variables and VMware object names are case sensitive.
Modules may rely on the ‘requests’ python library, which does not use the system certificate store by default. You can specify the certificate store by setting the REQUESTS_CA_BUNDLE environment variable. Note having this variable set may cause a ‘false’ value for the ‘validate_certs’ option to be ignored in some cases. Example: ‘export REQUESTS_CA_BUNDLE=/path/to/your/ca_bundle.pem’
Examples
- name: Create a VM
vmware.vmware.vm:
name: my-new-vm
datacenter: DC01
cluster: Cluster01
datastore: Datastore01
guest_id: amazonlinux3_64Guest
cpu:
cores: 4
cores_per_socket: 2
enable_hot_add: true
enable_hot_remove: true
memory:
size_mb: 8096
enable_hot_add: true
reserve_all_memory: true
disks:
- size: 80gb
provisioning: thin
device_node: SCSI(0:0)
- size: 10gb
provisioning: thick
device_node: SCSI(0:1)
- size: 1tb
provisioning: thin
device_node: SCSI(1:0)
datastore: ArchiveDatastore
scsi_controllers:
- controller_type: paravirtual
bus_number: 0
- controller_type: paravirtual
bus_number: 1
bus_sharing: virtualSharing
sata_controllers:
- bus_number: 0
cdroms:
- connect_at_power_on: true
iso_media_path: '[ISODS01] OS/Linux/RHEL-9.iso'
device_node: SATA(0:0)
- client_device_mode: passthrough
device_node: IDE(0:0)
network_adapters:
- network: VM Network
adapter_type: vmxnet3
connected: true
connect_at_power_on: true
- network: Management
adapter_type: vmxnet3
connected: true
connect_at_power_on: true
mac_address: 11:11:11:11:11:11
vm_options:
maximum_remote_console_sessions: 10
enable_io_mmu: true
boot_firmware: efi
enable_hardware_assisted_virtualization: true
register: _new_vm
- name: Power on VM
vmware.vmware.vm_powerstate:
moid: "{{ _new_vm.vm.moid }}"
datacenter: DC01
state: powered-on
- name: Add additional network adapter to VM
vmware.vmware.vm:
moid: "{{ _new_vm.vm.moid }}"
network_adapters:
- network: VM Network
adapter_type: vmxnet3
- network: Management
adapter_type: vmxnet3
- network: Legacy
adapter_type: e1000e
- name: Change second adapter shares without removing the new adapter
vmware.vmware.vm:
moid: "{{ _new_vm.vm.moid }}"
network_adapter_remove_unmanaged: false
network_adapters:
- network: VM Network
adapter_type: vmxnet3
- network: Management
adapter_type: vmxnet3
shares: 50
- name: Update resources with a reboot, if needed
vmware.vmware.vm:
moid: "{{ _new_vm.vm.moid }}"
allow_power_cycling: true
cpu:
cores: 16
memory:
size_mb: 16384
- name: Delete VM
vmware.vmware.vm:
moid: "{{ _new_vm.vm.moid }}"
state: absent
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
A dictionary showing any changes in settings or devices on the VM If there are no changes, this is an empty dictionary Returned: On success Sample: |
|
Whether the VM was powered cycled to apply the update. This is always returned, but always false for create and delete operations. Returned: On success Sample: |
|
Information about the target VM Returned: On success Sample: |