infra-vm: efficiently handle hostvars during deploy and destroy

Pass only the required variables from `hostvars` for a given infra vm to
significantly reduce wait time during `kayobe infra vm provision` and
`kayobe infra vm deprovision`. Time to create and destroy a VM now take
a minute or less.

Closes-Bug: #2093255
Change-Id: I15d17f45d15c04aced0a4c0fe97e1d798ff46799
This commit is contained in:
Jack Hodgkiss 2025-01-08 13:34:18 +00:00
parent a5a8e513b2
commit 0137015e6c
4 changed files with 39 additions and 23 deletions

View File

@ -4,7 +4,7 @@
include_role:
name: jriguera.configdrive
vars:
configdrive_os_family: "{{ vm_hostvars.infra_vm_os_family }}"
configdrive_os_family: "{{ vm_os_family }}"
configdrive_uuid: "{{ vm_name | to_uuid }}"
# Must set configdrive_instance_dir when using a loop
# https://github.com/jriguera/ansible-role-configdrive/blob/8438592c84585c86e62ae07e526d3da53629b377/tasks/main.yml#L17
@ -16,13 +16,10 @@
configdrive_volume_path: "{{ image_cache_path }}"
configdrive_config_dir_delete: False
configdrive_resolv:
domain: "{{ vm_hostvars.resolv_domain | default }}"
search: "{{ vm_hostvars.resolv_search | default }}"
dns: "{{ vm_hostvars.resolv_nameservers | default([]) }}"
configdrive_network_device_list: >
{{ vm_hostvars.network_interfaces |
map('net_configdrive_network_device', vm_hostvars.inventory_hostname) |
list }}
domain: "{{ vm_resolv_domain | default }}"
search: "{{ vm_resolv_search | default }}"
dns: "{{ vm_resolv_nameservers | default([]) }}"
configdrive_network_device_list: "{{ vm_network_device_list }}"
- name: "[{{ vm_name }}] Set a fact containing the configdrive image path"
set_fact:
@ -56,7 +53,7 @@
- name: "[{{ vm_name }}] Stat image files"
stat:
path: "{{ image_cache_path }}/{{ item.image | basename }}"
with_items: "{{ vm_hostvars.infra_vm_volumes | selectattr('image', 'defined') }}"
with_items: "{{ vm_volumes | selectattr('image', 'defined') }}"
register: image_stat_result
- name: "[{{ vm_name }}] Fix image ownership"
@ -75,7 +72,7 @@
vm_configdrive_device: cdrom
vm_configdrive_volume:
name: "{{ vm_name }}-configdrive"
pool: "{{ vm_hostvars.infra_vm_pool }}"
pool: "{{ vm_pool }}"
# Round size up to next multiple of 4096.
capacity: "{{ (stat_result.stat.size + 4095) // 4096 * 4096 }}"
device: "{{ vm_configdrive_device }}"
@ -85,10 +82,10 @@
libvirt_vm_image_cache_path: "{{ image_cache_path }}"
libvirt_vms:
- name: "{{ vm_name }}"
boot_firmware: "{{ vm_hostvars.infra_vm_boot_firmware | default }}"
machine: "{{ vm_hostvars.infra_vm_machine | default }}"
memory_mb: "{{ vm_hostvars.infra_vm_memory_mb }}"
vcpus: "{{ vm_hostvars.infra_vm_vcpus }}"
volumes: "{{ vm_hostvars.infra_vm_volumes + [vm_configdrive_volume] }}"
interfaces: "{{ vm_hostvars.infra_vm_interfaces }}"
boot_firmware: "{{ vm_boot_firmware | default }}"
machine: "{{ vm_machine | default }}"
memory_mb: "{{ vm_memory_mb }}"
vcpus: "{{ vm_vcpus }}"
volumes: "{{ vm_volumes + [vm_configdrive_volume] }}"
interfaces: "{{ vm_interfaces }}"
console_log_enabled: true

View File

@ -6,12 +6,12 @@
vars:
infra_vm_configdrive_volume:
name: "{{ vm_name }}-configdrive"
pool: "{{ hostvars[vm_hostvars.infra_vm_hypervisor].infra_vm_pool }}"
pool: "{{ vm_pool }}"
libvirt_vms:
- name: "{{ vm_name }}"
boot_firmware: "{{ vm_hostvars.infra_vm_boot_firmware | default }}"
memory_mb: "{{ vm_hostvars.infra_vm_memory_mb }}"
vcpus: "{{ vm_hostvars.infra_vm_vcpus }}"
volumes: "{{ vm_hostvars.infra_vm_volumes + [infra_vm_configdrive_volume] }}"
boot_firmware: "{{ vm_boot_firmware | default }}"
memory_mb: "{{ vm_memory_mb }}"
vcpus: "{{ vm_vcpus }}"
volumes: "{{ vm_volumes + [infra_vm_configdrive_volume] }}"
state: "absent"
become: True

View File

@ -10,8 +10,21 @@
- name: "{{ infra_vm_action | capitalize }} infra VMs (loop)"
include_tasks: "{{ infra_vm_action }}.yml"
vars:
vm_name: "{{ vm_hostvars.infra_vm_name }}"
vm_hostvars: "{{ hostvars[vm_item] }}"
vm_name: "{{ hostvars[vm_item].infra_vm_name }}"
vm_os_family: "{{ hostvars[vm_item].infra_vm_os_family }}"
vm_resolv_domain: "{{ hostvars[vm_item].resolv_domain }}"
vm_resolv_search: "{{ hostvars[vm_item].resolv_search }}"
vm_resolv_nameservers: "{{ hostvars[vm_item].resolv_nameservers }}"
vm_network_device_list: "{{ hostvars[vm_item].network_interfaces |
map('net_configdrive_network_device', hostvars[vm_item].inventory_hostname) |
list }}"
vm_volumes: "{{ hostvars[vm_item].infra_vm_volumes }}"
vm_pool: "{{ hostvars[vm_item].infra_vm_pool }}"
vm_boot_firmware: "{{ hostvars[vm_item].infra_vm_boot_firmware }}"
vm_machine: "{{ hostvars[vm_item].infra_vm_machine }}"
vm_memory_mb: "{{ hostvars[vm_item].infra_vm_memory_mb }}"
vm_vcpus: "{{ hostvars[vm_item].infra_vm_vcpus }}"
vm_interfaces: "{{ hostvars[vm_item].infra_vm_interfaces }}"
loop: "{{ infra_vm_vms }}"
when: (infra_vm_action == "deploy" and vm_name not in all_vms.list_vms) or infra_vm_action == "destroy"
loop_control:

View File

@ -0,0 +1,6 @@
---
features:
- |
Deploying and destroying infrastructure VMs is now significantly faster
as only the required variables are passed to the relevant tasks as opposed
to the entire collection of hostvars for each VM.