kayobe/ansible/seed-vm.yml

121 lines
4.5 KiB
YAML

---
- name: Ensure that the seed VM configdrive exists
hosts: seed-hypervisor
vars:
seed_host: "{{ groups['seed'][0] }}"
seed_hostvars: "{{ hostvars[seed_host] }}"
pre_tasks:
- name: Ensure the image cache directory exists
file:
path: "{{ image_cache_path }}"
state: directory
roles:
- role: jriguera.configdrive
# For now assume the VM OS family is the same as the hypervisor's.
configdrive_os_family: "{{ ansible_os_family }}"
configdrive_uuid: "{{ seed_host | to_uuid }}"
configdrive_fqdn: "{{ seed_host }}"
configdrive_name: "{{ seed_host }}"
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: True
configdrive_resolv:
domain: "{{ seed_hostvars.resolv_domain | default }}"
search: "{{ seed_hostvars.resolv_search | default }}"
dns: "{{ seed_hostvars.resolv_nameservers | default([]) }}"
configdrive_network_device_list: >
{{ seed_hostvars.seed_vm_interfaces |
map(attribute='net_name') |
map('net_configdrive_network_device', seed_host) |
list }}
tasks:
- name: Set a fact containing the configdrive image path
set_fact:
seed_vm_configdrive_path: "{{ image_cache_path }}/{{ seed_host }}.iso"
- name: Ensure configdrive is decoded and decompressed
shell: >
base64 -d {{ image_cache_path }}/{{ seed_host | to_uuid }}.gz
| gunzip
> {{ seed_vm_configdrive_path }}
- name: Ensure compressed configdrive is removed
file:
path: "{{ image_cache_path }}/{{ seed_host | to_uuid }}.gz"
state: absent
- name: Ensure that the seed VM is provisioned
hosts: seed-hypervisor
vars:
seed_host: "{{ groups['seed'][0] }}"
seed_hostvars: "{{ hostvars[seed_host] }}"
pre_tasks:
- name: Check the size of the configdrive
stat:
path: "{{ seed_vm_configdrive_path }}"
register: stat_result
roles:
- role: libvirt-vm
seed_vm_configdrive_volume:
name: "{{ seed_hostvars.seed_vm_name }}-configdrive"
pool: "{{ seed_hostvars.seed_vm_pool }}"
# Round size up to next multiple of 4096.
capacity: "{{ (stat_result.stat.size + 4095) // 4096 * 4096 }}"
device: "cdrom"
format: "raw"
image: "{{ seed_vm_configdrive_path }}"
libvirt_vm_name: "{{ seed_hostvars.seed_vm_name }}"
libvirt_vm_memory_mb: "{{ seed_hostvars.seed_vm_memory_mb }}"
libvirt_vm_vcpus: "{{ seed_hostvars.seed_vm_vcpus }}"
libvirt_vm_volumes: "{{ seed_hostvars.seed_vm_volumes + [seed_vm_configdrive_volume] }}"
libvirt_vm_interfaces: "{{ seed_hostvars.seed_vm_interfaces }}"
libvirt_vm_image_cache_path: "{{ image_cache_path }}"
become: True
tasks:
- name: Check the seed VM's IP address
shell: virsh domifaddr {{ seed_vm_name }} | awk 'NR > 2 { print $4 }'
register: ifaddr_result
changed_when: False
become: True
until: "{{ ifaddr_result | failed or ifaddr_result.stdout != '' }}"
retries: 60
delay: 1
- name: Ensure the inventory contains a group variables directory for the seed
local_action:
module: file
path: "{{ item.path }}"
state: directory
become: "{{ item.become | bool }}"
with_items:
- { path: "{{ kayobe_config_path }}/inventory/group_vars/seed", become: False }
# Generate inventory group variable files for the seed VM. Note that this
# host will not be accessible to this instance of Ansible - another process
# should be started.
- name: Ensure the inventory contains a group variables file for the seed
local_action:
module: copy
content: |
---
# Do not edit this file - it is managed by Ansible and changes will be lost.
# Ansible inventory file for the Kayobe seed VM.
# This host will provide the Bifrost undercloud.
ansible_host: "{{ ifaddr_result.stdout_lines[0] | ipaddr('address') }}"
dest: "{{ item.path }}"
become: "{{ item.become | bool }}"
with_items:
- { path: "{{ kayobe_config_path }}/inventory/group_vars/seed/ansible-host", become: False }
- name: Wait for SSH access to the seed VM
local_action:
module: wait_for
host: "{{ ifaddr_result.stdout_lines[0] | ipaddr('address') }}"
port: 22
state: started