ansible/roles/kvm_installvm/tasks/main.yml

65 lines
1.8 KiB
YAML

---
- name: Get VMs list
community.libvirt.virt:
command: list_vms
register: existing_vms
changed_when: no
- name: Create VM if not exists
block:
# - name: Upload base image archiv to core's tmp directory
# copy:
# src: "{{ vm_base_image_archiv }}"
# dest: "/home/core/tmp/"
# owner: libvirt-qemu
# group: kvm
# mode: "0600"
# - name: Gunzip base image in core's tmp directory
# shell:
# cmd: "/usr/bin/gunzip /home/core/tmp/{{ vm_base_image_archiv }}"
# Existierende VM-Disk NICHT überschreiben: force=no. Ergbnis in copy_results speichern: copy_results is changed
- name: Copy unarchived base image to /var/lib/libvirt/images and rename to $vm_name.qcow2 if not exists
copy:
src: "/home/core/tmp/{{ vm_base_image }}"
dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2"
remote_src: "yes"
owner: libvirt-qemu
group: kvm
mode: "0600"
force: "no"
register: copy_results
- name: Configure the image
command: |
virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \
--hostname {{ vm_name }} \
--root-password password:{{ password }} \
--ssh-inject 'core:file:{{ ssh_key_core }}'
when: copy_results is changed
# Falls das V-File kopiert wurde - weil es das noch nicht gab - wird die VM defined
# Ist eigentlich unnötig, denn der ganze Block wird nicht ausgeführt wenn die VM bereits defined ist
- name: Define vm
community.libvirt.virt:
command: define
xml: "{{ lookup('template', 'template2204.xml') }}"
when: copy_results is changed
when: "vm_name not in existing_vms.list_vms"
- name: Ensure VM is started
community.libvirt.virt:
name: "{{ vm_name }}"
state: running
register: vm_start_results
until: "vm_start_results is success"
retries: 15
delay: 2
...