From 513bddb44ad2840155cdd66ff42e9b9074632053 Mon Sep 17 00:00:00 2001 From: gen2kes <54024705+gen2kes@users.noreply.github.com> Date: Thu, 5 Sep 2019 17:52:12 +0200 Subject: [PATCH] VMware: Add disk_mode parameter in vmware_guest_disk (#60406) * Solve issue 58592 Added support to select disk_mode. --- .../modules/cloud/vmware/vmware_guest_disk.py | 23 +++++- .../targets/vmware_guest_disk/aliases | 3 + .../targets/vmware_guest_disk/tasks/main.yml | 79 +++++++++++++++++++ 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 test/integration/targets/vmware_guest_disk/aliases create mode 100644 test/integration/targets/vmware_guest_disk/tasks/main.yml diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_disk.py b/lib/ansible/modules/cloud/vmware/vmware_guest_disk.py index 7d197bb4664..07d7776f2f6 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest_disk.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest_disk.py @@ -90,6 +90,10 @@ options: - ' - C(eagerzeroedthick) eagerzeroedthick disk' - ' - C(thick) thick disk' - ' 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(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.' @@ -127,6 +131,7 @@ EXAMPLES = ''' scsi_controller: 1 unit_number: 1 scsi_type: 'paravirtual' + disk_mode: 'persistent' - size_gb: 10 type: eagerzeroedthick state: present @@ -134,6 +139,7 @@ EXAMPLES = ''' scsi_controller: 2 scsi_type: 'buslogic' unit_number: 12 + disk_mode: 'independent_persistent' - size: 10Gb type: eagerzeroedthick state: present @@ -141,6 +147,7 @@ EXAMPLES = ''' scsi_controller: 2 scsi_type: 'buslogic' unit_number: 1 + disk_mode: 'independent_nonpersistent' delegate_to: localhost register: disk_facts @@ -244,7 +251,7 @@ class PyVmomiHelper(PyVmomi): return scsi_ctl @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 Args: @@ -259,7 +266,7 @@ class PyVmomiHelper(PyVmomi): disk_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create disk_spec.device = vim.vm.device.VirtualDisk() 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.unitNumber = disk_index return disk_spec @@ -340,7 +347,7 @@ class PyVmomiHelper(PyVmomi): 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': # 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'] if disk['disk_type'] == 'thin': disk_spec.device.backing.thinProvisioned = True @@ -416,7 +423,8 @@ class PyVmomiHelper(PyVmomi): datastore=None, autoselect_datastore=True, disk_unit_number=0, - scsi_controller=0) + scsi_controller=0, + disk_mode='persistent') # Check state if 'state' in disk: if disk['state'] not in ['absent', 'present']: @@ -569,6 +577,13 @@ class PyVmomiHelper(PyVmomi): " 'disk_type' value from ['thin', 'thick', 'eagerzeroedthick']." % disk_index) 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_contrl_type = disk.get('scsi_type', 'paravirtual').lower() if scsi_contrl_type not in self.scsi_device_type.keys(): diff --git a/test/integration/targets/vmware_guest_disk/aliases b/test/integration/targets/vmware_guest_disk/aliases new file mode 100644 index 00000000000..3eede2cbf01 --- /dev/null +++ b/test/integration/targets/vmware_guest_disk/aliases @@ -0,0 +1,3 @@ +cloud/vcenter +shippable/vcenter/group1 +needs/target/prepare_vmware_tests diff --git a/test/integration/targets/vmware_guest_disk/tasks/main.yml b/test/integration/targets/vmware_guest_disk/tasks/main.yml new file mode 100644 index 00000000000..6385cda9375 --- /dev/null +++ b/test/integration/targets/vmware_guest_disk/tasks/main.yml @@ -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