Files
kayobe/ansible/roles/infra-vms/tasks/deploy.yml
Jack Hodgkiss 0137015e6c 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
2025-01-10 13:44:40 +00:00

92 lines
3.4 KiB
YAML

---
- name: "[{{ vm_name }}] Ensure that the VM configdrive exists"
include_role:
name: jriguera.configdrive
vars:
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
configdrive_instance_dir: "{{ configdrive_uuid }}"
configdrive_fqdn: "{{ vm_name }}"
configdrive_name: "{{ vm_name }}"
configdrive_ssh_public_key: "{{ lookup('file', ssh_public_key_path) }}"
configdrive_config_dir: "{{ image_cache_path }}"
configdrive_volume_path: "{{ image_cache_path }}"
configdrive_config_dir_delete: False
configdrive_resolv:
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:
vm_configdrive_path: "{{ image_cache_path }}/{{ vm_name }}.iso"
- name: "[{{ vm_name }}] Ensure configdrive is decoded and decompressed"
shell: >
base64 -d {{ image_cache_path }}/{{ vm_name | to_uuid }}.gz
| gunzip
> {{ vm_configdrive_path }}
- name: "[{{ vm_name }}] Ensure unnecessary files are removed"
file:
path: "{{ item }}"
state: absent
with_items:
- "{{ image_cache_path }}/{{ vm_name | to_uuid }}.gz"
- name: "[{{ vm_name }}] Check the size of the configdrive image"
stat:
path: "{{ vm_configdrive_path }}"
get_checksum: False
mime: False
register: stat_result
# NOTE(mgoddard): Prior to the Xena release, the seed VM was provisioned using
# the stackhpc.livirt-vm role with become=true. This resulted in the cached
# image being owned by root. Since Xena, we execute the role without
# become=true. Correct the image ownership to avoid a permission denied error
# when downloading a new image of the same name.
- name: "[{{ vm_name }}] Stat image files"
stat:
path: "{{ image_cache_path }}/{{ item.image | basename }}"
with_items: "{{ vm_volumes | selectattr('image', 'defined') }}"
register: image_stat_result
- name: "[{{ vm_name }}] Fix image ownership"
file:
path: "{{ image_cache_path }}/{{ item.item.image | basename }}"
owner: "{{ ansible_facts.user_uid }}"
group: "{{ ansible_facts.user_gid }}"
with_items: "{{ image_stat_result.results }}"
when: item.stat.exists
become: true
- name: "[{{ vm_name }}] Ensure that the VM is provisioned"
include_role:
name: stackhpc.libvirt-vm
vars:
vm_configdrive_device: cdrom
vm_configdrive_volume:
name: "{{ vm_name }}-configdrive"
pool: "{{ vm_pool }}"
# Round size up to next multiple of 4096.
capacity: "{{ (stat_result.stat.size + 4095) // 4096 * 4096 }}"
device: "{{ vm_configdrive_device }}"
format: "raw"
image: "{{ vm_configdrive_path }}"
remote_src: true
libvirt_vm_image_cache_path: "{{ image_cache_path }}"
libvirt_vms:
- name: "{{ vm_name }}"
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