ansible-kvm-provision/roles/kvm_provision/tasks/main.yml
2022-03-08 19:55:15 +01:00

71 lines
1.6 KiB
YAML

---
- name: Requirements
apt:
name:
- guestfs-tools
- python3-libvirt
- qemu-system
- libvirt-clients
- libvirt-daemon-system
state: present
become: yes
- name: Get VMs
community.libvirt.virt:
command: list_vms
uri: "qemu:///system"
register: existing_vms
changed_when: no
- name: Create VM if not exists
block:
- name: Download base image
get_url:
url: "{{ base_image_url }}"
dest: "/tmp/{{ base_image_name }}"
checksum: "sha512:{{ base_image_sha }}"
mode: '0660'
- name: Copy base image to libvirt
copy:
dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2"
src: "/tmp/{{ base_image_name }}"
force: no
remote_src: yes
mode: 0660
register: copy_results
- name: Custom kvm
command:
virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \
--hostname {{ vm_name }} \
--root-password password:{{ vm_root_pass }} \
--ssh-inject 'root:file:{{ ssh_key }}'
when: copy_results is changed
- name: Define vm
community.libvirt.virt:
uri: "qemu:///system"
command: define
xml: "{{ lookup('template','vm-template.xml.j2') }}"
when: "vm_name not in existing_vms.list_vms"
- name: Ensure VM is started
community.libvirt.virt:
name: "{{ vm_name }}"
uri: "qemu:///system"
state: running
register: vm_start_results
until: "vm_start_results is success"
retries: 15
delay: 2
- name: Ensure temporary file is deleted
file:
path: "/tmp/{{ base_image_name }}"
state: absent
when: cleanup_tmp | bool