stackhpc / ansible-role-libvirt-vm

This role configures and creates VMs on a KVM hypervisor.
128 stars 67 forks source link

Custom xml domain definition file and disk device start at vda #21

Closed JPvRiel closed 5 years ago

JPvRiel commented 5 years ago

As per commit comments.

I found it a bit awkward trying to supply / set vm.xml.j2 as a default in defaults/main.yml. When the role runs, if the calling play doesn't set xml_file, the role appears to have vm.xml_file undefined and the value in defaults/main.yml doesn't have any effect - so duplicating it there is for cosmetic/documentation value. Couldn't think of a cleaner way about it given the defaults are a singleton and omitted parts don't seem to set defaults.

Tested both setting xml_file and not setting xml_file as working while my custom file el7_vm.xml.j2 was in the playbook dir /templates location.

E.g.

         libvirt_vms:
            - state: present
              xml_file: el7_vm.xml.j2
              name: test
              memory_mb: 16384
              vcpus: 4
              volumes:
                - name: test-hostvg
                  device: disk
                  format: qcow2
                  capacity: 512GB
                  backing_image: rhel_backing_image.qcow2
                  pool: default
                - name: test-vg_data_hdd
                  device: disk
                  # Note logical volume format for qemu driver is "raw"
                  format: raw
                  capacity: 5TB
                  pool: hdd
              interfaces:
                - type: bridge
                  network: uat
JPvRiel commented 5 years ago

FYI, this is how el7_vm_xml.j2 looked:

<domain type='{{ libvirt_vm_engine }}'>
  <name>{{ vm.name }}</name>
  <description>Custom xml template for RHEL or CentOS 7 guest</description>
  <memory>{{ vm.memory_mb | int * 1024 }}</memory>
  <vcpu>{{ vm.vcpus }}</vcpu>
  <os>
    <type arch='{{ libvirt_vm_arch }}'{% if machine is not none %} machine='{{ machine }}'{% endif %}>hvm</type>
    <bootmenu enable='no'/>
    <boot dev='hd'/>
    <bios useserial='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu{% if cpu_mode is not none %} mode='{{ cpu_mode }}'{% endif %}>
    <model fallback='allow'/>
  </cpu>
  <clock offset='utc'>
    <timer name="kvmclock" present="yes"/>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>{{ libvirt_vm_emulator }}</emulator>
{% for volume in volumes %}
    <disk type='volume' device='{{ volume.device | default(libvirt_volume_default_device) }}'>
      <driver name='qemu' type='{{ volume.format | default(libvirt_volume_default_format) }}'/>
      <source pool='{{ volume.pool }}' volume='{{ volume.name }}'/>
      <target dev='vd{{ 'abcdefghijklmnopqrstuvwxyz'[loop.index - 1] }}'/>
    </disk>
{% endfor %}
{% for interface in interfaces %}
{% if interface.type is defined and interface.type == 'direct' %}
    <interface type='direct'>
      <source dev='{{ interface.source.dev }}' mode='{{ interface.source.mode | default('vepa') }}'/>
{% else %}
    <interface type='network'>
      <source network='{{ interface.network }}'/>
{% endif %}
      <model type='virtio'/>
    </interface>
{% endfor %}
    <!-- support qemu guest agent -->
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
    </controller>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0'/>
    </channel>
{% if console_log_enabled | bool %}
    <serial type='file'>
      <source path='{{ console_log_path }}'/>
    </serial>
    <serial type='pty'/>
    <console type='file'>
      <source path='{{ console_log_path }}'/>
      <target type='serial'/>
    </console>
{% else %}
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
{% endif %}
  </devices>
</domain>