58 lines
1.3 KiB
YAML
58 lines
1.3 KiB
YAML
---
|
|
# tasks file for kvm_provision
|
|
|
|
- name: Ensure requirements in place
|
|
package:
|
|
name:
|
|
- guestfs-tools
|
|
- python3-libvirt
|
|
state: present
|
|
become: yes
|
|
|
|
- name: Get VMs list
|
|
community.libvirt.virt:
|
|
command: list_vms
|
|
register: existing_vms
|
|
changed_when: no
|
|
|
|
- name: Create VM if not exists
|
|
block:
|
|
|
|
- name: Copy base image to libvirt directory
|
|
copy:
|
|
dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2"
|
|
src: "{{ libvirt_pool_dir }}/{{ base_image_name }}"
|
|
force: no
|
|
remote_src: yes
|
|
mode: 0660
|
|
register: copy_results
|
|
- name: Define vm
|
|
community.libvirt.virt:
|
|
command: define
|
|
xml: "{{ lookup('template', 'vm-template.xml.j2') }}"
|
|
- name: Configure the image
|
|
command: |
|
|
virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \
|
|
--hostname {{ vm_name }} \
|
|
--password {{ vm_user }}:password:{{ vm_pass }} \
|
|
--firstboot-command '/usr/bin/ssh-keygen -A'
|
|
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
|
|
|
|
- name: Ensure temporary file is deleted
|
|
file:
|
|
path: "/tmp/{{ base_image_name }}"
|
|
state: absent
|
|
when: cleanup_tmp | bool
|