aboutsummaryrefslogtreecommitdiff
path: root/roles/libvirt/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'roles/libvirt/tasks')
-rw-r--r--roles/libvirt/tasks/vm-check-duplicate.yml10
-rw-r--r--roles/libvirt/tasks/vm-check-exists.yml10
-rw-r--r--roles/libvirt/tasks/vm-confirm-info.yml28
-rw-r--r--roles/libvirt/tasks/vm-install.yml12
-rw-r--r--roles/libvirt/tasks/vm-undefine.yml9
5 files changed, 69 insertions, 0 deletions
diff --git a/roles/libvirt/tasks/vm-check-duplicate.yml b/roles/libvirt/tasks/vm-check-duplicate.yml
new file mode 100644
index 0000000..5070cfd
--- /dev/null
+++ b/roles/libvirt/tasks/vm-check-duplicate.yml
@@ -0,0 +1,10 @@
+- name: Fetch list of all VMs
+ community.libvirt.virt:
+ command: list_vms
+ register: vms_list
+
+- name: Fail if VM exists
+ ansible.builtin.fail:
+ msg: "VM {{ libvirt_vm_name }} is already in-use."
+ when: libvirt_vm_name | string in vms_list.list_vms
+
diff --git a/roles/libvirt/tasks/vm-check-exists.yml b/roles/libvirt/tasks/vm-check-exists.yml
new file mode 100644
index 0000000..c6adf31
--- /dev/null
+++ b/roles/libvirt/tasks/vm-check-exists.yml
@@ -0,0 +1,10 @@
+- name: Fetch list of all VMs
+ community.libvirt.virt:
+ command: list_vms
+ register: vms_list
+
+- name: Fail if VM does not exist
+ ansible.builtin.fail:
+ msg: "VM {{ libvirt_vm_name }} does not exist."
+ when: libvirt_vm_name | string not in vms_list.list_vms
+
diff --git a/roles/libvirt/tasks/vm-confirm-info.yml b/roles/libvirt/tasks/vm-confirm-info.yml
new file mode 100644
index 0000000..3121354
--- /dev/null
+++ b/roles/libvirt/tasks/vm-confirm-info.yml
@@ -0,0 +1,28 @@
+- name: Fetch VM information
+ community.libvirt.virt:
+ command: get_xml
+ name: "{{ libvirt_vm_name }}"
+ register: vm_info
+
+- name: Parse VM information
+ community.general.xml:
+ xmlstring: "{{ vm_info.get_xml }}"
+ xpath: "/domain/devices/disk/source"
+ content: attribute
+ register: vm_xml_output
+
+- name: Fail if expected disk is not found in VM XML
+ ansible.builtin.fail:
+ msg: "{{ libvirt_vm_destination }} was not found in VM disk definition {{ vm_xml_output.matches[0].source.file }}"
+ when: libvirt_vm_destination not in vm_xml_output.matches[0].source.file
+
+- name: Check filesystem for expected VM disk
+ ansible.builtin.stat:
+ path: "/{{ libvirt_vm_destination }}/{{ libvirt_vm_name }}.img"
+ get_checksum: false
+ register: vm_check_image_exists
+
+- name: Fail if expected VM disk is not found on destination filesystem
+ ansible.builtin.fail:
+ msg: "Expected VM disk /{{ libvirt_vm_destination }}/{{ libvirt_vm_name }}.img not found on filesystem."
+ when: not vm_check_image_exists.stat.exists
diff --git a/roles/libvirt/tasks/vm-install.yml b/roles/libvirt/tasks/vm-install.yml
new file mode 100644
index 0000000..a194ae7
--- /dev/null
+++ b/roles/libvirt/tasks/vm-install.yml
@@ -0,0 +1,12 @@
+- name: Copy kickstart file to destination filesystem
+ ansible.builtin.template:
+ src: "kickstart/{{ libvirt_vm_kickstart_file }}"
+ dest: "/{{ libvirt_vm_destination }}/{{ libvirt_vm_kickstart_file }}"
+
+- name: Create VM in destination filesystem
+ ansible.builtin.command: 'virt-install --name {{ libvirt_vm_name }} --memory {{ libvirt_vm_memory }} --vcpus {{ libvirt_vm_vcpus }} --network {{ libvirt_vm_network }} --disk size={{ libvirt_vm_disk_size }},path=/{{ libvirt_vm_destination }}/{{ libvirt_vm_name }}.img,format={{ libvirt_vm_disk_format }} --location {{ libvirt_vm_iso_path }} --os-variant {{ libvirt_vm_os }} --initrd-inject=/{{ libvirt_vm_destination }}/{{ libvirt_vm_kickstart_file }} --extra-args="inst.ks=file:/{{ libvirt_vm_kickstart_file }}"'
+
+- name: Remove kickstart file from destination filesystem
+ ansible.builtin.file:
+ path: "/{{ libvirt_vm_destination }}/{{ libvirt_vm_kickstart_file }}"
+ state: absent
diff --git a/roles/libvirt/tasks/vm-undefine.yml b/roles/libvirt/tasks/vm-undefine.yml
new file mode 100644
index 0000000..a43b12b
--- /dev/null
+++ b/roles/libvirt/tasks/vm-undefine.yml
@@ -0,0 +1,9 @@
+- name: Destroy VM
+ community.libvirt.virt:
+ name: "{{ libvirt_vm_name }}"
+ state: destroyed
+
+- name: Undefine VM
+ community.libvirt.virt:
+ name: "{{ libvirt_vm_name }}"
+ command: undefine