--- # 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: Generate cloud-init user-data from template delegate_to: localhost ansible.builtin.copy: src: files/leander.pub dest: /tmp/ansible_cloudinit_ssh.pub - name: Generate cloud-init user-data from template delegate_to: localhost ansible.builtin.template: src: cloud-init.yml.j2 dest: /tmp/ansible_cloudinit_userdata.config - name: Copy network-config from file delegate_to: localhost ansible.builtin.template: src: templates/cloud-init-network.yml.j2 dest: /tmp/ansible_cloudinit_network.config - name: Configure the image command: | virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \ --hostname {{ vm_name }} \ --root-password password:{{ root_passwd }} \ --firstboot-command '/usr/bin/ssh-keygen -A' when: copy_results is changed - name: Install the vm via virt-install ansible.builtin.command: | virt-install \ --name "{{ vm_name }}" \ --boot uefi \ --machine q35 \ --memory "{{ vm_ram_mb }}" \ --vcpus "{{ vm_vcpus }}" \ --disk "path={{ vm_pool_dir }}/{{ vm_name }}.qcow2" \ --import \ --os-variant "debian11" \ --network "network={{ vm_net }},model=virtio,driver.iommu=on" \ --rng /dev/urandom,driver.iommu=on \ --memballoon driver.iommu=on \ --virt-type kvm \ --graphics vnc \ --noautoconsole \ --cloud-init user-data=/tmp/ansible_cloudinit_userdata.config,network-config=/tmp/ansible_cloudinit_network.config --noreboot \ --qemu-commandline="-smbios type=1,serial=ds=nocloud;h={{ vm_name }}" - 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