Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support boot disk renaming and resizing. #705

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- vm_infra - Support boot disk renaming and resizing. (https://github.com/oVirt/ovirt-ansible-collection/pull/705)
3 changes: 3 additions & 0 deletions roles/vm_infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ The `vms` and `profile` variables can contain following attributes, note that if
| clone | No | If yes then the disks of the created virtual machine will be cloned and independent of the template. This parameter is used only when state is running or present and VM didn't exist before. |
| template | Blank | Name of template that the virtual machine should be based on. |
| template_version | UNDEF | Version number of the template to be used for VM. By default the latest available version of the template is used. |
| boot_disk_name | UNDEF | Renames the bootable disk after the VM is created. Useful when creating VMs from templates |
| boot_disk_use_vm_name_prefix | true | Use the name of the virtual machine as prefix when renaming the VM boot disk. The resulting boot disk name would be <i>{{vm_name}}_{{boot_disk_name}}</i>|
| boot_disk_size | UNDEF | Resizes the bootable disk after the VM is created. A suffix that complies to the IEC 60027-2 standard (for example 10GiB, 1024MiB) can be used. |
| memory | UNDEF | Amount of virtual machine memory. |
| memory_max | UNDEF | Upper bound of virtual machine memory up to which memory hot-plug can be performed. |
| memory_guaranteed | UNDEF | Amount of minimal guaranteed memory of the Virtual Machine. Prefix uses IEC 60027-2 standard (for example 1GiB, 1024MiB). <i>memory_guaranteed</i> parameter can't be lower than <i>memory</i> parameter. |
Expand Down
59 changes: 59 additions & 0 deletions roles/vm_infra/tasks/rename_resize_boot_disk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
- name: "Get the '{{ current_vm.name }}' VM info"
ovirt_vm_info:
auth: "{{ ovirt_auth }}"
pattern: "name={{ current_vm.name }}"
fetch_nested: true
nested_attributes:
- "bootable"
all_content: true
register: current_vm_info

- name: "Get the '{{ current_vm.name }}' VM boot disk ID"
ansible.builtin.set_fact:
current_vm_boot_disk_id: "{{ item.id }}"
loop: "{{ current_vm_info.ovirt_vms[0].disk_attachments }} "
when: item.bootable is defined and item.bootable

- name: "Get the '{{ current_vm.name }}' VM boot disk info"
ovirt_disk_info:
auth: "{{ ovirt_auth }}"
pattern: "id={{ current_vm_boot_disk_id }}"
fetch_nested: true
register: current_vm_boot_disk_info
when: current_vm_boot_disk_id is defined

- name: "Get the Storage Domain info from the '{{ current_vm.name }}' VM boot disk"
ovirt_storage_domain:
auth: "{{ ovirt_auth }}"
id: "{{ current_vm_boot_disk_info.ovirt_disks[0].storage_domains[0].id }}"
register: current_vm_boot_disk_sd_info
when: current_vm_boot_disk_id is defined

- name: "Get the '{{ current_vm.name }}' VM boot disk name"
ansible.builtin.set_fact:
current_vm_boot_disk_name: "{{ current_vm.boot_disk_name | default(current_vm.profile.boot_disk_name) }}"
when: current_vm.boot_disk_name is defined or current_vm.profile.boot_disk_name is defined

- name: "Use '{{ current_vm.name }}' as prefix for the VM boot disk name"
ansible.builtin.set_fact:
current_vm_boot_disk_use_vm_name_prefix: >
"{{ current_vm.boot_disk_use_vm_name_prefix | default(current_vm.profile.boot_disk_use_vm_name_prefix) | default(true) }}"

- name: "Rename the '{{ current_vm.name }}' VM boot disk"
ovirt_disk:
auth: "{{ ovirt_auth }}"
id: "{{ current_vm_boot_disk_id }}"
name: "{% if current_vm_boot_disk_use_vm_name_prefix %}{{ current_vm.name }}_{% endif %}{{ current_vm_boot_disk_name }}"
vm_name: "{{ current_vm.name }}"
storage_domain: "{{ current_vm_boot_disk_sd_info.storagedomain.name }}"
when: current_vm_boot_disk_id is defined and current_vm_boot_disk_name is defined

- name: "Resize the '{{ current_vm.name }}' VM boot disk"
ovirt_disk:
auth: "{{ ovirt_auth }}"
id: "{{ current_vm_boot_disk_id }}"
size: "{{ current_vm.boot_disk_size | default(current_vm.profile.boot_disk_size) }}"
vm_name: "{{ current_vm.name }}"
when: ( current_vm.boot_disk_size is defined or current_vm.profile.boot_disk_size is defined ) and current_vm_boot_disk_id is defined
...
6 changes: 6 additions & 0 deletions roles/vm_infra/tasks/vm_state_present.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
- name: Apply any Affinity Labels
import_tasks: affinity_labels.yml

- name: Rename and resize boot disk
include_tasks: rename_resize_boot_disk.yml
with_items: "{{ create_vms }}"
loop_control:
loop_var: "current_vm"

- name: Manage profile disks
ovirt_disk:
auth: "{{ ovirt_auth }}"
Expand Down