This section shows you how to use Ansible to modify an existing virtual machine.
You’ve already followed How to create a Virtual Machine and created a VM.
In this example, we use the vcenter_vm_hardware_*
modules to add a new CDROM to an existing VM.
First we create a new SATA adapter. We specify the pci_slot_number
. This way if we run the task again it won’t do anything if there is already an adapter there.
- name: Create a SATA adapter at PCI slot 34 vmware.vmware_rest.vcenter_vm_hardware_adapter_sata: vm: '{{ test_vm1_info.id }}' pci_slot_number: 34 register: _sata_adapter_result_1
{ "value": { "bus": 0, "pci_slot_number": 34, "label": "SATA controller 0", "type": "AHCI" }, "id": "15000", "changed": true }
Now we can create the CDROM drive:
- name: Attach an ISO image to a guest VM vmware.vmware_rest.vcenter_vm_hardware_cdrom: vm: '{{ test_vm1_info.id }}' type: SATA sata: bus: 0 unit: 2 start_connected: true backing: iso_file: '[ro_datastore] fedora.iso' type: ISO_FILE register: _result
{ "value": { "start_connected": true, "backing": { "iso_file": "[ro_datastore] fedora.iso", "type": "ISO_FILE" }, "allow_guest_control": false, "label": "CD/DVD drive 1", "state": "NOT_CONNECTED", "type": "SATA", "sata": { "bus": 0, "unit": 2 } }, "id": "16002", "changed": true }
Here we attach the VM to the network (through the portgroup). We specify a pci_slot_number
for the same reason.
The second task adjusts the NIC configuration.
- name: Attach a VM to a dvswitch vmware.vmware_rest.vcenter_vm_hardware_ethernet: vm: '{{ test_vm1_info.id }}' pci_slot_number: 4 backing: type: DISTRIBUTED_PORTGROUP network: "{{ my_portgroup_info.dvs_portgroup_info.dvswitch1[0].key }}" start_connected: false register: vm_hardware_ethernet_1
{ "value": { "start_connected": false, "pci_slot_number": 4, "backing": { "connection_cookie": 2145337177, "distributed_switch_uuid": "50 33 88 3a 8c 6e f9 02-7a fd c2 c0 2c cf f2 ac", "distributed_port": "2", "type": "DISTRIBUTED_PORTGROUP", "network": "dvportgroup-1649" }, "mac_address": "00:50:56:b3:49:5c", "mac_type": "ASSIGNED", "allow_guest_control": false, "wake_on_lan_enabled": false, "label": "Network adapter 1", "state": "NOT_CONNECTED", "type": "VMXNET3", "upt_compatibility_enabled": false }, "id": "4000", "changed": true }
- name: Turn the NIC's start_connected flag on vmware.vmware_rest.vcenter_vm_hardware_ethernet: nic: '{{ vm_hardware_ethernet_1.id }}' start_connected: true vm: '{{ test_vm1_info.id }}'
{ "id": "4000", "changed": true }
We can also adjust the amount of memory that we dedicate to our VM.
- name: Increase the memory of a VM vmware.vmware_rest.vcenter_vm_hardware_memory: vm: '{{ test_vm1_info.id }}' size_MiB: 1080 register: _result
{ "id": null, "changed": true }
Here we use the vcenter_vm_hardware
module to upgrade the version of the hardware:
- name: Upgrade the VM hardware version vmware.vmware_rest.vcenter_vm_hardware: upgrade_policy: AFTER_CLEAN_SHUTDOWN upgrade_version: VMX_13 vm: '{{ test_vm1_info.id }}' register: _result
{ "id": null, "changed": true }
You can use vcenter_vm_hardware_cpu
for that:
- name: Dedicate one core to the VM vmware.vmware_rest.vcenter_vm_hardware_cpu: vm: '{{ test_vm1_info.id }}' count: 1 register: _result
{ "value": { "hot_remove_enabled": false, "count": 1, "hot_add_enabled": false, "cores_per_socket": 1 }, "id": null, "changed": false }
In this example, we remove the SATA controller of the PCI slot 34.
{ "changed": true }
{ "changed": true }
Here we attach a floppy drive to a VM.
- name: Add a floppy disk drive vmware.vmware_rest.vcenter_vm_hardware_floppy: vm: '{{ test_vm1_info.id }}' allow_guest_control: true register: my_floppy_drive
{ "value": { "start_connected": false, "backing": { "auto_detect": true, "type": "HOST_DEVICE", "host_device": "" }, "allow_guest_control": true, "label": "Floppy drive 1", "state": "NOT_CONNECTED" }, "id": "8000", "changed": true }
Here we attach a tiny disk to the VM. The capacity
is in bytes.
- name: Create a new disk vmware.vmware_rest.vcenter_vm_hardware_disk: vm: '{{ test_vm1_info.id }}' type: SATA new_vmdk: capacity: 320000 register: my_new_disk
{ "value": { "backing": { "vmdk_file": "[local] test_vm1_8/test_vm1_1.vmdk", "type": "VMDK_FILE" }, "label": "Hard disk 2", "type": "SATA", "sata": { "bus": 0, "unit": 0 }, "capacity": 320000 }, "id": "16000", "changed": true }
© 2012–2018 Michael DeHaan
© 2018–2021 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/scenario_guides/vmware_rest_scenarios/vm_hardware_tuning.html