From f1e8d9bf0e06ab6b4a9f83d0fed82cbcc9be319e Mon Sep 17 00:00:00 2001 From: "Qi (Keira) Zhang" Date: Tue, 22 Aug 2023 11:05:53 +0800 Subject: [PATCH] Fix #421 Use vmware_guest_disk module to add IDE boot disk (#492) Signed-off-by: Qi Zhang --- common/vm_create.yml | 61 ++++++++++++++++++++++-- common/vm_create_with_ide_disk.yml | 51 -------------------- common/vm_hot_add_remove_disk.yml | 20 ++++---- linux/deploy_vm/deploy_vm_from_iso.yml | 9 +--- windows/deploy_vm/deploy_vm_from_iso.yml | 7 +-- 5 files changed, 71 insertions(+), 77 deletions(-) delete mode 100644 common/vm_create_with_ide_disk.yml diff --git a/common/vm_create.yml b/common/vm_create.yml index 37ccc4c54..cfefe7473 100644 --- a/common/vm_create.yml +++ b/common/vm_create.yml @@ -27,6 +27,15 @@ # vm_network_name (optional): the name of network to connect, default is 'VM Network', # and type of of IP assignment is set to 'dhcp'. # +# VM with IDE boot disk can not be created directly. To create a VM with IDE boot disk, +# here needs to create a VM with PVSCSI boot disk firstly and then remove the PVSCSI +# disk and controller, then add an IDE disk to the VM as boot disk +- name: "Initialize the fact of VM's boot disk controller type" + ansible.builtin.set_fact: + vm_boot_disk_controller: >- + {%- if boot_disk_controller | default('paravirtual') in ['paravirtual', 'ide'] -%}paravirtual + {%- else -%}{{ boot_disk_controller }}{%- endif -%} + - name: "Create a new VM '{{ vm_name }}' on server '{{ vsphere_host_name }}'" community.vmware.vmware_guest: hostname: "{{ vsphere_host_name }}" @@ -49,7 +58,7 @@ - size_gb: "{{ boot_disk_size_gb | default(40) }}" type: thin datastore: "{{ datastore }}" - controller_type: "{{ boot_disk_controller | default('paravirtual') }}" + controller_type: "{{ vm_boot_disk_controller }}" controller_number: 0 unit_number: 0 cdrom: "{{ vm_cdroms | default(omit) }}" @@ -59,12 +68,58 @@ type: "dhcp" register: vm_create_result -- name: Display the result of new VM creation +- name: "Display the result of new VM creation" ansible.builtin.debug: var=vm_create_result when: enable_debug is defined and enable_debug -- name: Check the result of new VM creation +- name: "Check the result of new VM creation" ansible.builtin.assert: that: - vm_create_result is changed fail_msg: "New VM '{{ vm_name }}' is not created since 'vm_create_result.changed' is False." + +- name: "Change VM's boot disk to IDE disk" + when: + - boot_disk_controller is defined + - boot_disk_controller == "ide" + block: + - name: "Update VM's boot disk controller to {{ boot_disk_controller }}" + ansible.builtin.set_fact: + vm_boot_disk_controller: "{{ boot_disk_controller }}" + + - name: "Remove pre-added PVSCSI boot disk from VM" + include_tasks: vm_hot_add_remove_disk.yml + vars: + disk_operation: "absent" + disk_controller_type: "paravirtual" + ctrl_number: 0 + unit_number: 0 + + - name: "Remove pre-added PVSCSI controller from VM" + include_tasks: vm_hot_add_remove_disk_ctrl.yml + vars: + disk_controller_ops: "absent" + disk_controller_type: "paravirtual" + disk_controller_number: 0 + + - name: "Add IDE boot disk to VM" + community.vmware.vmware_guest_disk: + hostname: "{{ vsphere_host_name }}" + username: "{{ vsphere_host_user }}" + password: "{{ vsphere_host_user_password }}" + validate_certs: "{{ validate_certs | default(false) }}" + datacenter: "{{ vsphere_host_datacenter }}" + folder: "{{ vm_folder }}" + name: "{{ vm_name }}" + disk: + - size_gb: "{{ boot_disk_size_gb | default(40) }}" + type: "thin" + datastore: "{{ datastore }}" + controller_type: "{{ vm_boot_disk_controller }}" + controller_number: 0 + unit_number: 0 + register: add_ide_disk_result + + - name: "Print the result of adding IDE boot disk" + ansible.builtin.debug: var=add_ide_disk_result + when: enable_debug diff --git a/common/vm_create_with_ide_disk.yml b/common/vm_create_with_ide_disk.yml deleted file mode 100644 index d5a2daa6c..000000000 --- a/common/vm_create_with_ide_disk.yml +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright 2021-2023 VMware, Inc. -# SPDX-License-Identifier: BSD-2-Clause ---- -# Create a new VM with IDE disk as boot disk -# Parameters: -# vm_name (required): the name of the new VM. -# guest_id (required): the guest ID of new created VM. -# datastore (required): the name of datastore where new VM created. -# vm_power_state (optional): the power state of VM after creation, valid values are -# 'poweredon', 'powered-on', 'poweredoff', 'powered-off', default value is 'poweredoff'. -# memory_mb (optional): the VM memory size in MB, default value is 4096. -# cpu_number (optional): the VM CPU number, default value is 2. -# num_cpu_cores_per_socket (optional): VM CPU cores per socket number, default is 1. -# firmware (optional): VM firmware type, valid values are 'efi', 'bios', -# default value is 'efi'. -# hardware_version (optional): the hardware version of VM, if not specified will create -# VM with the latest hardware version ESXi supported. -# vm_cdroms (optional): VM CDROM list, this parameter is optional, but if you want to install -# guest OS after VM created, please configure the OS installation ISO files. -# boot_disk_size_gb (optional): the boot disk size in GB, default value is 32. -# network_adapter_type (optional): the network adapter type, valid values are 'e1000', -# 'e1000e', 'vmxnet3', default value is 'vmxnet3'. -# vm_network_name (optional): the name of network to connect, default is 'VM Network', -# and type of of IP assignment is set to 'dhcp'. -# -- include_tasks: vm_create.yml - vars: - boot_disk_controller: 'paravirtual' - -# Get VM id -- include_tasks: vm_get_id.yml - -- name: "Get IDE controller number and unit number for boot disk" - ansible.builtin.set_fact: - ide_disk_ctlr_num: 0 - ide_disk_unit_num: 0 - -- name: "Set boot disk size in KB" - ansible.builtin.set_fact: - boot_disk_size_kb: "{{ (boot_disk_size_gb | int) * 1024 * 1024 }}" - -- block: - - name: "Remove pre-added PVSCSI boot disk" - ansible.builtin.shell: "vim-cmd /vmsvc/device.diskremove {{ vm_id }} 0 0 scsi" - - - name: "Remove pre-added PVSCSI controller" - ansible.builtin.shell: "vim-cmd /vmsvc/device.ctlrremove {{ vm_id }} scsi 0" - - - name: "Add IDE boot disk" - ansible.builtin.shell: "vim-cmd /vmsvc/device.diskadd {{ vm_id }} {{ boot_disk_size_kb }} {{ ide_disk_ctlr_num }} {{ ide_disk_unit_num }} {{ datastore }} ide" - delegate_to: "{{ esxi_hostname }}" diff --git a/common/vm_hot_add_remove_disk.yml b/common/vm_hot_add_remove_disk.yml index 639bf636b..74d9071c2 100644 --- a/common/vm_hot_add_remove_disk.yml +++ b/common/vm_hot_add_remove_disk.yml @@ -16,7 +16,7 @@ # delete_disk_file: true or false. If 'disk_operation' is 'absent', whether # delete disk file in datastore or not. # -- name: Check required parameter +- name: "Check required parameter for managing VM disk" ansible.builtin.assert: that: - disk_operation is defined and disk_operation @@ -30,18 +30,19 @@ - "disk_controller_type is required and valid value is 'buslogic', 'lsilogic', 'lsilogicsas', 'paravirtual', 'sata', or 'nvme'." - "ctrl_number and unit_number are also required." -- block: - - name: Set default disk size to 1 GB +- name: "Set facts for adding new disk" + when: disk_operation | lower == 'present' + block: + - name: "Set default disk size to 1 GB" ansible.builtin.set_fact: disk_size_gb: 1 when: disk_size_gb is undefined or disk_size_gb|int == 0 - - name: Set default disk provision type to 'thin' + - name: "Set default disk provision type to 'thin'" ansible.builtin.set_fact: disk_provision_type: 'thin' when: disk_provision_type is undefined or not disk_provision_type - when: disk_operation | lower == 'present' -- name: "{{ disk_operation }} disk to VM" +- name: "Manage disk on VM with state {{ disk_operation }}" community.vmware.vmware_guest_disk: hostname: "{{ vsphere_host_name }}" username: "{{ vsphere_host_user }}" @@ -59,7 +60,8 @@ controller_number: "{{ ctrl_number }}" unit_number: "{{ unit_number }}" destroy: "{{ delete_disk_file | default(omit) }}" - register: disk_add_facts -- name: Display the VM disk operation result - ansible.builtin.debug: var=disk_add_facts + register: manage_disk_result + +- name: "Display the result of manageing VM disk" + ansible.builtin.debug: var=manage_disk_result when: enable_debug diff --git a/linux/deploy_vm/deploy_vm_from_iso.yml b/linux/deploy_vm/deploy_vm_from_iso.yml index 2e9dfd9fa..ad77b5924 100644 --- a/linux/deploy_vm/deploy_vm_from_iso.yml +++ b/linux/deploy_vm/deploy_vm_from_iso.yml @@ -77,17 +77,10 @@ - name: "Compose VM CDROMs to mount OS install ISO files" include_tasks: ../../common/compose_vm_cdroms.yml - - name: "Create a new VM with {{ boot_disk_controller }} disk" + - name: "Create a new VM with boot disk of controller type {{ boot_disk_controller }}" include_tasks: ../../common/vm_create.yml vars: memory_mb: "{{ vm_memory_mb }}" - when: boot_disk_controller != 'ide' - - - name: "Create a new VM with {{ boot_disk_controller }} disk" - include_tasks: ../../common/vm_create_with_ide_disk.yml - vars: - memory_mb: "{{ vm_memory_mb }}" - when: boot_disk_controller == 'ide' - name: "Get VM info" include_tasks: ../../common/vm_get_vm_info.yml diff --git a/windows/deploy_vm/deploy_vm_from_iso.yml b/windows/deploy_vm/deploy_vm_from_iso.yml index e3a554d53..84fc6a7a7 100644 --- a/windows/deploy_vm/deploy_vm_from_iso.yml +++ b/windows/deploy_vm/deploy_vm_from_iso.yml @@ -34,13 +34,8 @@ - name: "Compose VM CDROMs with ISO file list" include_tasks: ../../common/compose_vm_cdroms.yml -- name: "Create new VM" +- name: "Create a new VM with boot disk of controller type {{ boot_disk_controller }}" include_tasks: ../../common/vm_create.yml - when: boot_disk_controller != 'ide' - -- name: "Create new VM with IDE boot disk controller" - include_tasks: ../../common/vm_create_with_ide_disk.yml - when: boot_disk_controller == 'ide' - name: "Get new VM info" include_tasks: ../../common/vm_get_vm_info.yml