Skip to content

Commit

Permalink
Fix #421 Use vmware_guest_disk module to add IDE boot disk (#492)
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Zhang <qiz@vmware.com>
  • Loading branch information
keirazhang authored Aug 22, 2023
1 parent 18cb5c7 commit f1e8d9b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 77 deletions.
61 changes: 58 additions & 3 deletions common/vm_create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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) }}"
Expand All @@ -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
51 changes: 0 additions & 51 deletions common/vm_create_with_ide_disk.yml

This file was deleted.

20 changes: 11 additions & 9 deletions common/vm_hot_add_remove_disk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}"
Expand All @@ -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
9 changes: 1 addition & 8 deletions linux/deploy_vm/deploy_vm_from_iso.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions windows/deploy_vm/deploy_vm_from_iso.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f1e8d9b

Please sign in to comment.