MNAIO: Fix file-backed VM deployment

Recent patches broke the file-backed VM deployment.
This patch fixes it again. It also ensures that the
cleanup of VM's removes any VM's running, rather than
the active inventory.

Change-Id: Ic1cc887bf61162a8c8d96dd53f6db9583c4c089b
This commit is contained in:
Jesse Pretorius 2018-09-26 13:59:07 +01:00
parent 14567e0c3c
commit ddcef522d4
3 changed files with 91 additions and 80 deletions

View File

@ -16,9 +16,11 @@
- name: Gather Facts for vm_hosts
hosts: vm_hosts
gather_facts: yes
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
- deploy-vms
tasks:
- name: Get info about the virt storage pools
- name: Get info about existing virt storage pools
virt_pool:
command: info
register: _virt_pools
@ -27,65 +29,39 @@
set_fact:
virt_pools: "{{ _virt_pools }}"
- name: Prepare & Create VMs
hosts: pxe_servers
gather_facts: no
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
- deploy-vms
tasks:
- name: Stop running VMs
- name: Get info about existing VM's
virt:
name: "{{ server_hostname }}"
command: list_vms
register: _virt_list
- name: Stop all running VM's
virt:
name: "{{ item }}"
command: destroy
failed_when: false
when:
- server_vm | default(false) | bool
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
with_items: "{{ _virt_list.list_vms }}"
- name: Delete VM LV
- name: Delete any LV's related to running VM's
lvol:
vg: "{{ default_vm_disk_vg }}"
lv: "{{ server_hostname }}"
lv: "{{ item }}"
state: absent
force: yes
failed_when: false
when:
- server_vm | default(false) | bool
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
with_items: "{{ _virt_list.list_vms }}"
- name: Delete VM Disk Image
- name: Delete any disk images related to running VM's
file:
path: "{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}.img"
path: "{{ _virt_pools.pools.default.path | default('/data/images') }}/{{ item }}.img"
state: absent
when:
- server_vm | default(false) | bool
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
with_items: "{{ _virt_list.list_vms }}"
- name: Undefine the VM
- name: Undefine all running VM's
virt:
name: "{{ server_hostname }}"
name: "{{ item }}"
command: undefine
failed_when: false
when:
- server_vm | default(false) | bool
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
- name: Create VM LV
lvol:
vg: "{{ default_vm_disk_vg }}"
lv: "{{ server_hostname }}"
size: "{{ default_vm_storage }}"
when:
- server_vm | default(false) | bool
- default_vm_disk_mode == "lvm"
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
with_items: "{{ _virt_list.list_vms }}"
- name: Setup/clean-up file-based disk images
when:
@ -93,11 +69,9 @@
block:
- name: Find existing base image files
find:
paths: "{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}"
paths: "{{ _virt_pools.pools.default.path | default('/data/images') }}"
patterns: '*-base.img'
register: _base_images
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
- name: Enable/disable vm_use_snapshot based on whether there are base image files
set_fact:
@ -110,6 +84,23 @@
with_items: "{{ _base_images.files }}"
when:
- not (vm_use_snapshot | bool)
- name: Prepare VM storage
hosts: pxe_servers
gather_facts: no
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
- deploy-vms
tasks:
- name: Create VM LV
lvol:
vg: "{{ default_vm_disk_vg }}"
lv: "{{ server_hostname }}"
size: "{{ default_vm_storage }}"
when:
- server_vm | default(false) | bool
- default_vm_disk_mode == "lvm"
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
@ -117,16 +108,25 @@
command: >-
qemu-img create
-f qcow2
{% if vm_use_snapshot | bool %}
{% if hostvars[item]['vm_use_snapshot'] | bool %}
-b {{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}-base.img
{% endif %}
{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}.img
{{ default_vm_storage }}m
when:
- server_vm | default(false) | bool
- default_vm_disk_mode == "file"
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
- name: Prepare file-based disk images
hosts: vm_hosts
gather_facts: yes
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
- deploy-vms
tasks:
# Note (odyssey4me):
# This will only work on a host which has
# libguestfs >= 1.35.2 and >= 1.34.1
@ -142,7 +142,7 @@
virt-sysprep
--enable customize
--ssh-inject root:file:/root/.ssh/id_rsa.pub
--add {{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}.img
--add {{ virt_pools.pools.default.path | default('/data/images') }}/{{ hostvars[item]['server_hostname'] }}.img
when:
- hostvars[item]['server_vm'] | default(false) | bool
with_items: "{{ groups['pxe_servers'] }}"
@ -165,22 +165,21 @@
when:
- "'Image preparation completed.' not in _galera_prepare.stdout_lines"
- name: Wait for guest capabilities to appear
command: "virsh capabilities"
register: virsh_caps
until: "'<guest>' in virsh_caps.stdout"
retries: 6
delay: 10
delegate_to: "{{ item }}"
with_items: "{{ groups['vm_hosts'] }}"
- name: Prepare VM's
hosts: pxe_servers
gather_facts: no
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
- deploy-vms
tasks:
- name: Define the VM
virt:
name: "{{ server_hostname }}"
command: define
xml: >-
{%- if vm_use_snapshot | bool %}
{{ lookup('file', hostvars[item]['virt_pools'].pools.default.path | default('/data/images') ~ '/' ~ hostvars[item]['server_hostname'] ~ '.xml') }}
{%- if hostvars[item]['vm_use_snapshot'] | bool %}
{{ lookup('file', hostvars[item]['virt_pools'].pools.default.path | default('/data/images') ~ '/' ~ server_hostname ~ '.xml') }}
{%- else %}
{{ lookup('template', 'kvm/kvm-vm.xml.j2') }}
{%- endif %}
@ -209,7 +208,8 @@
- "{{ groups['vm_hosts'] }}"
- "{{ vm_xml.results }}"
- name: Start VMs
- name: Start VM's
hosts: vm_hosts
gather_facts: "{{ gather_facts | default(true) }}"
environment: "{{ deployment_environment_variables | default({}) }}"
@ -227,7 +227,7 @@
tags:
- always
- name: Start the VM
- name: Start VM
virt:
name: "{{ hostvars[item]['server_hostname'] }}"
command: start
@ -252,6 +252,7 @@
- name: Create vm_servers group
hosts: localhost
connection: local
gather_facts: false
environment: "{{ deployment_environment_variables | default({}) }}"
tags:
@ -293,7 +294,8 @@
tags:
- deploy-vms
tasks:
- file:
- name: Remove immutable attr from /etc/network/interfaces
file:
path: /etc/network/interfaces
attr: ""
when:
@ -319,7 +321,8 @@
tags:
- always
- lineinfile:
- name: Set MaxStartups
lineinfile:
path: /etc/ssh/sshd_config
line: MaxStartups 100
state: present
@ -327,7 +330,8 @@
notify:
- restart sshd
- lineinfile:
- name: Set MaxSessions
lineinfile:
path: /etc/ssh/sshd_config
line: MaxSessions 100
state: present

View File

@ -41,7 +41,7 @@
{% elif default_vm_disk_mode == "file" %}
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' discard='unmap' cache='none' io='native'/>
<source file='{{ _virt_pools.pools.default.path | default('/data/images') }}/{{ server_hostname }}.img'/>
<source file='{{ hostvars[item]['virt_pools'].pools.default.path | default('/data/images') }}/{{ server_hostname }}.img'/>
{% endif %}
<target dev='sda' bus='scsi'/>
<alias name='scsi0-0-0-0'/>

View File

@ -492,3 +492,10 @@
echo ${module} | tee -a /etc/modules
fi
done
- name: Wait for guest capabilities to appear
command: "virsh capabilities"
register: virsh_caps
until: "'<guest>' in virsh_caps.stdout"
retries: 6
delay: 10