From 0137015e6cd7b6486e2a80f38612e1aa40ab1031 Mon Sep 17 00:00:00 2001 From: Jack Hodgkiss Date: Wed, 8 Jan 2025 13:34:18 +0000 Subject: [PATCH] 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 --- ansible/roles/infra-vms/tasks/deploy.yml | 29 +++++++++---------- ansible/roles/infra-vms/tasks/destroy.yml | 10 +++---- ansible/roles/infra-vms/tasks/main.yml | 17 +++++++++-- ...m-action-performance-5d8a218b75574be2.yaml | 6 ++++ 4 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 releasenotes/notes/improve-infra-vm-action-performance-5d8a218b75574be2.yaml diff --git a/ansible/roles/infra-vms/tasks/deploy.yml b/ansible/roles/infra-vms/tasks/deploy.yml index 2000bfdc6..ae6663359 100644 --- a/ansible/roles/infra-vms/tasks/deploy.yml +++ b/ansible/roles/infra-vms/tasks/deploy.yml @@ -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 diff --git a/ansible/roles/infra-vms/tasks/destroy.yml b/ansible/roles/infra-vms/tasks/destroy.yml index c45b996c7..b1a9965f5 100644 --- a/ansible/roles/infra-vms/tasks/destroy.yml +++ b/ansible/roles/infra-vms/tasks/destroy.yml @@ -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 diff --git a/ansible/roles/infra-vms/tasks/main.yml b/ansible/roles/infra-vms/tasks/main.yml index 9fec42e3c..a7306935e 100644 --- a/ansible/roles/infra-vms/tasks/main.yml +++ b/ansible/roles/infra-vms/tasks/main.yml @@ -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: diff --git a/releasenotes/notes/improve-infra-vm-action-performance-5d8a218b75574be2.yaml b/releasenotes/notes/improve-infra-vm-action-performance-5d8a218b75574be2.yaml new file mode 100644 index 000000000..4a955502f --- /dev/null +++ b/releasenotes/notes/improve-infra-vm-action-performance-5d8a218b75574be2.yaml @@ -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.