--- # tasks file for kvm_provision - name: Ensure prereqs are installed package: name: - guestfs-tools - libvirt-python state: present become: yes - name: Get VMs list virt: command: list_vms 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 }}" - name: Copy base image to libvirt storage pool copy: src: "/tmp/{{ base_image_name }}" dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2" force: no remote_src: yes mode: 0660 become: yes register: copy_results - name: Customize image command: | virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \ --hostname {{ vm_name }} \ --root-password password:{{ vm_root_pass }} when: copy_results is changed - name: Define VM virt: command: define xml: "{{ lookup('template', 'vm-template.xml.j2') }}" when: "vm_name not in existing_vms.list_vms" - name: Start new VM virt: name: "{{ vm_name }}" state: running register: vm_start_results until: "vm_start_results is success" retries: 15 delay: 2 - name: Clean up temporary files file: path: "/tmp/{{ base_image_name }}" state: absent when: cleanup_tmp | bool