4

I'm trying to define a VM using the virt module and a XML file. This task:

 - name: Define VM using the XML file
   virt: command=define
         name={{ new_vm_name }}
         xml={{ vm_images_path }}/{{ new_vm_xml }}
   remote_user: root
   delegate_to: "{{ hyperv }}"

fails with this error:

failed: [vm-002 -> nat] => {"failed": true}
msg: (domain_definition):1: Start tag expected, '<' not found
/var/lib/libvirt/images//vm-002-ver1.1.0-00042.xml

while this works:

 - name: Define VM using the XML file
   command: virsh define {{ vm_images_path }}/{{ new_vm_xml }}
   remote_user: root
   delegate_to: "{{ hyperv }}"

XML file is the same in both cases, any idea of why the first one fails? I do always prefer using modules than custom commands so I'd really like to make the first one work.

Mxx
  • 8,979
  • 4
  • 27
  • 37
Ignacio Verona
  • 655
  • 2
  • 8
  • 22

1 Answers1

3

The xml parameter of the virt module takes a XML string, not a filename.

You could achieve what you want like this:

- name: Define VM using the XML file
   virt: command=define
         name={{ new_vm_name }}
         xml={{ lookup('template', './libvirt_create_template.j2') }}
   remote_user: root
   delegate_to: "{{ hyperv }}"

The file you reference in the lookup is located on the Ansible control server (i.e. where you run your playbooks).

By using a template lookup, you can have the VM specific settings replaces with the values from the inventory for the VM you are installing.

Rik Tytgat
  • 136
  • 6