78 lines
2.3 KiB
YAML
78 lines
2.3 KiB
YAML
---
|
|
- name: Deploy VM on Xen
|
|
block:
|
|
- name: Build disk definitions
|
|
ansible.builtin.set_fact:
|
|
virtualization_xen_disks: "{{ virtualization_xen_disks | default([]) + [virtualization_xen_disk_cfg] }}"
|
|
vars:
|
|
device_letter_map: "abcdefghijklmnopqrstuvwxyz"
|
|
device_letter: "{{ device_letter_map[ansible_loop.index0] }}"
|
|
virtualization_xen_disk_cfg: >-
|
|
{{
|
|
{
|
|
'path': (
|
|
virtualization_xen_disk_path ~ '/' ~ hostname ~ '.qcow2'
|
|
if ansible_loop.index0 == 0
|
|
else virtualization_xen_disk_path ~ '/' ~ hostname ~ '-disk' ~ ansible_loop.index0 ~ '.qcow2'
|
|
),
|
|
'target': 'xvd' ~ device_letter,
|
|
'size': (item.size | float)
|
|
}
|
|
}}
|
|
loop: "{{ system_cfg.disks }}"
|
|
loop_control:
|
|
label: "{{ item | to_json }}"
|
|
extended: true
|
|
changed_when: false
|
|
|
|
- name: Create VM disks for Xen
|
|
delegate_to: localhost
|
|
ansible.builtin.command:
|
|
argv:
|
|
- qemu-img
|
|
- create
|
|
- -f
|
|
- qcow2
|
|
- "{{ item.path }}"
|
|
- "{{ item.size }}G"
|
|
creates: "{{ item.path }}"
|
|
loop: "{{ virtualization_xen_disks }}"
|
|
loop_control:
|
|
label: "{{ item.path }}"
|
|
|
|
- name: Render Xen VM configuration
|
|
delegate_to: localhost
|
|
vars:
|
|
xen_installer_media_enabled: true
|
|
ansible.builtin.template:
|
|
src: xen.cfg.j2
|
|
dest: /tmp/xen-{{ hostname }}.cfg
|
|
mode: "0644"
|
|
|
|
- name: Create Xen VM
|
|
delegate_to: localhost
|
|
ansible.builtin.command:
|
|
argv:
|
|
- xl
|
|
- create
|
|
- /tmp/xen-{{ hostname }}.cfg
|
|
register: virtualization_xen_create_result
|
|
changed_when: virtualization_xen_create_result.rc == 0
|
|
|
|
- name: Ensure VM is running
|
|
delegate_to: localhost
|
|
ansible.builtin.command:
|
|
argv:
|
|
- xl
|
|
- list
|
|
register: virtualization_xen_list_result
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Set VM created fact
|
|
ansible.builtin.set_fact:
|
|
virtualization_vm_created_in_run: true
|
|
when:
|
|
- virtualization_xen_list_result is defined
|
|
- hostname in virtualization_xen_list_result.stdout
|