Skip to content

Commit

Permalink
vmware_guest_disk: Add iolimit modifications on an existing disk with… (
Browse files Browse the repository at this point in the history
#1466)

vmware_guest_disk: Add iolimit modifications on an existing disk with…

SUMMARY
This commit add iolimit modifications on an existing disk without changing size.
ISSUE TYPE
Feature Pull Request
COMPONENT NAME
plugin community.vmware.vmware_guest_disk
ADDITIONAL INFORMATION
As of today, one can't change an existing disk iops limit and/or shares level without also changing the disk size.
Idempotency is only done on disk size, so task will "ok" whatever iolimit is set to.
This commit fix that

- name: Gather disk info from virtual machine using name
  community.vmware.vmware_guest_disk_info:
    datacenter: "{{ vmware_datacenter }}"
    hostname: "{{ vmware_vcenter }}"
    username: "{{ vmware_username }}"
    password: "{{ vmware_password }}"
    name: "{{ inventory_hostname }}"
  delegate_to: localhost
  register: disk_info

- name: Apply iops
  community.vmware.vmware_guest_disk:
    datacenter: "{{ vmware_datacenter }}"
    hostname: "{{ vmware_vcenter }}"
    username: "{{ vmware_username }}"
    password: "{{ vmware_password }}"
    name: "{{ inventory_hostname }}"
    disk:
      - filename: '{{ disk_info.guest_disk_info["0"].backing_filename }}'
        size_gb: "{{ vm_disk_size }}"
        type: "{{ vm_disk_type }}"
        datastore: "{{ vm_datastore }}"
        disk_mode: '{{ disk_info.guest_disk_info["0"].backing_diskmode }}'
        scsi_controller: '{{ disk_info.guest_disk_info["0"].controller_bus_number }}'
        unit_number: '{{ disk_info.guest_disk_info["0"].unit_number }}'
        iolimit:
          limit: "{{ vm_disk_iops }}"
          shares:
            level: normal

Reviewed-by: Mario Lenz <[email protected]>
  • Loading branch information
yo000 authored Sep 29, 2022
1 parent a8ec8dc commit 1dab1f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- vmware_guest_disk - Adding `iolimit` modifications of an existing disk without changing size (https://github.com/ansible-collections/community.vmware/pull/1466).
32 changes: 28 additions & 4 deletions plugins/modules/vmware_guest_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ def ensure_disks(self, vm_obj=None):
disk_change_list = list()
results = dict(changed=False, disk_data=None, disk_changes=dict())
new_added_disk_ctl = list()
sharesval = {'low': 500, 'normal': 1000, 'high': 2000}

# Deal with controller
for disk in disk_data:
Expand Down Expand Up @@ -694,6 +695,7 @@ def ensure_disks(self, vm_obj=None):
# Deal with Disks
for disk in disk_data:
disk_found = False
update_io = False
disk_change = False
ctl_found = False
for device in self.vm.config.hardware.device:
Expand All @@ -704,8 +706,27 @@ def ensure_disks(self, vm_obj=None):
disk_found = True
if disk['state'] == 'present':
disk_spec = vim.vm.device.VirtualDeviceSpec()
# set the operation to edit so that it knows to keep other settings
disk_spec.device = disk_device
# Deal with iolimit. Note that if iolimit is set, you HAVE TO both set limit and shares,
# or ansible will break with "'NoneType' object is not subscriptable"
if 'iolimit' in disk:
if disk['iolimit']['limit'] != disk_spec.device.storageIOAllocation.limit:
update_io = True

if 'shares' in disk['iolimit']:
# 'low', 'normal' and 'high' values in disk['iolimit']['shares']['level'] are converted to int values on vcenter side
if (disk['iolimit']['shares']['level'] != 'custom'
and sharesval.get(disk['iolimit']['shares']['level'], 0) != disk_spec.device.storageIOAllocation.shares.shares) or \
(disk['iolimit']['shares']['level'] == 'custom'
and disk['iolimit']['shares']['level_value'] != disk_spec.device.storageIOAllocation.shares.shares):
update_io = True

if update_io:
# set the operation to edit so that it knows to keep other settings
disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
disk_spec = self.get_ioandshares_diskconfig(disk_spec, disk)
disk_change = True

# If this is an RDM ignore disk size
if disk['disk_type'] != 'rdm':
if disk['size'] < disk_spec.device.capacityInKB:
Expand All @@ -714,14 +735,17 @@ def ensure_disks(self, vm_obj=None):
% (disk['disk_index'], disk['size'],
disk_spec.device.capacityInKB))
if disk['size'] != disk_spec.device.capacityInKB:
# set the operation to edit so that it knows to keep other settings
disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
if disk['disk_type'] != 'vpmemdisk':
disk_spec = self.get_ioandshares_diskconfig(disk_spec, disk)
disk_spec.device.capacityInKB = disk['size']
self.config_spec.deviceChange.append(disk_spec)
disk_change = True
disk_change_list.append(disk_change)
results['disk_changes'][disk['disk_index']] = "Disk reconfigured."

if disk_change:
self.config_spec.deviceChange.append(disk_spec)
disk_change_list.append(disk_change)
results['disk_changes'][disk['disk_index']] = "Disk reconfigured."

elif disk['state'] == 'absent':
# Disk already exists, deleting
Expand Down

0 comments on commit 1dab1f7

Please sign in to comment.