VMware: Add disk_mode parameter in vmware_guest_disk (#60406)

* Solve issue 58592

Added support to select disk_mode.
This commit is contained in:
gen2kes 2019-09-05 17:52:12 +02:00 committed by Gonéri Le Bouder
parent 1aca1f86b6
commit 513bddb44a
3 changed files with 101 additions and 4 deletions

View file

@ -90,6 +90,10 @@ options:
- ' - C(eagerzeroedthick) eagerzeroedthick disk' - ' - C(eagerzeroedthick) eagerzeroedthick disk'
- ' - C(thick) thick disk' - ' - C(thick) thick disk'
- ' Default: C(thick) thick disk, no eagerzero.' - ' Default: C(thick) thick disk, no eagerzero.'
- ' - C(disk_mode) (string): Type of disk mode. Valid values are:'
- ' - C(persistent) Changes are immediately and permanently written to the virtual disk. This is default.'
- ' - C(independent_persistent) Same as persistent, but not affected by snapshots.'
- ' - C(independent_nonpersistent) Changes to virtual disk are made to a redo log and discarded at power off, but not affected by snapshots.'
- ' - C(datastore) (string): Name of datastore or datastore cluster to be used for the disk.' - ' - C(datastore) (string): Name of datastore or datastore cluster to be used for the disk.'
- ' - C(autoselect_datastore) (bool): Select the less used datastore. Specify only if C(datastore) is not specified.' - ' - C(autoselect_datastore) (bool): Select the less used datastore. Specify only if C(datastore) is not specified.'
- ' - C(scsi_controller) (integer): SCSI controller number. Valid value range from 0 to 3.' - ' - C(scsi_controller) (integer): SCSI controller number. Valid value range from 0 to 3.'
@ -127,6 +131,7 @@ EXAMPLES = '''
scsi_controller: 1 scsi_controller: 1
unit_number: 1 unit_number: 1
scsi_type: 'paravirtual' scsi_type: 'paravirtual'
disk_mode: 'persistent'
- size_gb: 10 - size_gb: 10
type: eagerzeroedthick type: eagerzeroedthick
state: present state: present
@ -134,6 +139,7 @@ EXAMPLES = '''
scsi_controller: 2 scsi_controller: 2
scsi_type: 'buslogic' scsi_type: 'buslogic'
unit_number: 12 unit_number: 12
disk_mode: 'independent_persistent'
- size: 10Gb - size: 10Gb
type: eagerzeroedthick type: eagerzeroedthick
state: present state: present
@ -141,6 +147,7 @@ EXAMPLES = '''
scsi_controller: 2 scsi_controller: 2
scsi_type: 'buslogic' scsi_type: 'buslogic'
unit_number: 1 unit_number: 1
disk_mode: 'independent_nonpersistent'
delegate_to: localhost delegate_to: localhost
register: disk_facts register: disk_facts
@ -244,7 +251,7 @@ class PyVmomiHelper(PyVmomi):
return scsi_ctl return scsi_ctl
@staticmethod @staticmethod
def create_scsi_disk(scsi_ctl_key, disk_index): def create_scsi_disk(scsi_ctl_key, disk_index, disk_mode):
""" """
Create Virtual Device Spec for virtual disk Create Virtual Device Spec for virtual disk
Args: Args:
@ -259,7 +266,7 @@ class PyVmomiHelper(PyVmomi):
disk_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create disk_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create
disk_spec.device = vim.vm.device.VirtualDisk() disk_spec.device = vim.vm.device.VirtualDisk()
disk_spec.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo() disk_spec.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
disk_spec.device.backing.diskMode = 'persistent' disk_spec.device.backing.diskMode = disk_mode
disk_spec.device.controllerKey = scsi_ctl_key disk_spec.device.controllerKey = scsi_ctl_key
disk_spec.device.unitNumber = disk_index disk_spec.device.unitNumber = disk_index
return disk_spec return disk_spec
@ -340,7 +347,7 @@ class PyVmomiHelper(PyVmomi):
scsi_controller = disk['scsi_controller'] + 1000 # VMware auto assign 1000 + SCSI Controller scsi_controller = disk['scsi_controller'] + 1000 # VMware auto assign 1000 + SCSI Controller
if disk['disk_unit_number'] not in current_scsi_info[scsi_controller]['disks'] and disk['state'] == 'present': if disk['disk_unit_number'] not in current_scsi_info[scsi_controller]['disks'] and disk['state'] == 'present':
# Add new disk # Add new disk
disk_spec = self.create_scsi_disk(scsi_controller, disk['disk_unit_number']) disk_spec = self.create_scsi_disk(scsi_controller, disk['disk_unit_number'], disk['disk_mode'])
disk_spec.device.capacityInKB = disk['size'] disk_spec.device.capacityInKB = disk['size']
if disk['disk_type'] == 'thin': if disk['disk_type'] == 'thin':
disk_spec.device.backing.thinProvisioned = True disk_spec.device.backing.thinProvisioned = True
@ -416,7 +423,8 @@ class PyVmomiHelper(PyVmomi):
datastore=None, datastore=None,
autoselect_datastore=True, autoselect_datastore=True,
disk_unit_number=0, disk_unit_number=0,
scsi_controller=0) scsi_controller=0,
disk_mode='persistent')
# Check state # Check state
if 'state' in disk: if 'state' in disk:
if disk['state'] not in ['absent', 'present']: if disk['state'] not in ['absent', 'present']:
@ -569,6 +577,13 @@ class PyVmomiHelper(PyVmomi):
" 'disk_type' value from ['thin', 'thick', 'eagerzeroedthick']." % disk_index) " 'disk_type' value from ['thin', 'thick', 'eagerzeroedthick']." % disk_index)
current_disk['disk_type'] = disk_type current_disk['disk_type'] = disk_type
# Mode of Disk
temp_disk_mode = disk.get('disk_mode', 'persistent').lower()
if temp_disk_mode not in ['persistent', 'independent_persistent', 'independent_nonpersistent']:
self.module.fail_json(msg="Invalid 'disk_mode' specified for disk index [%s]. Please specify"
" 'disk_mode' value from ['persistent', 'independent_persistent', 'independent_nonpersistent']." % disk_index)
current_disk['disk_mode'] = temp_disk_mode
# SCSI Controller Type # SCSI Controller Type
scsi_contrl_type = disk.get('scsi_type', 'paravirtual').lower() scsi_contrl_type = disk.get('scsi_type', 'paravirtual').lower()
if scsi_contrl_type not in self.scsi_device_type.keys(): if scsi_contrl_type not in self.scsi_device_type.keys():

View file

@ -0,0 +1,3 @@
cloud/vcenter
shippable/vcenter/group1
needs/target/prepare_vmware_tests

View file

@ -0,0 +1,79 @@
# Test code for the vmware_guest_disk_disk module.
- import_role:
name: prepare_vmware_tests
vars:
setup_attach_host: true
setup_datastore: true
setup_virtualmachines: true
- name: create new disk with invalid disk mode
vmware_guest_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
name: "{{ virtual_machines[0].name }}"
disk:
- datastore: "{{ ds1 }}"
disk_mode: "invalid_disk_mode"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 2
register: test_create_disk1
ignore_errors: True
- debug:
msg: "{{ test_create_disk1 }}"
- name: assert that changes were not made
assert:
that:
- not(test_create_disk1 is changed)
- name: create new disk(s) with valid disk mode
vmware_guest_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
name: "{{ virtual_machines[0].name }}"
disk:
- datastore: "{{ ds1 }}"
disk_mode: "independent_persistent"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 2
- datastore: "{{ ds1 }}"
disk_mode: "independent_nonpersistent"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 3
- datastore: "{{ ds1 }}"
disk_mode: "persistent"
scsi_controller: 0
scsi_type: 'paravirtual'
size_gb: 10
state: present
type: eagerzeroedthick
unit_number: 4
register: test_create_disk2
- debug:
msg: "{{ test_create_disk2 }}"
- name: assert that changes were made
assert:
that:
- test_create_disk2 is changed