Skip to content

Latest commit

 

History

History

libvirt

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

xsrv.libvirt

This role will install and configure libvirt, a collection of software that provides a convenient way to manage virtual machines and other virtualization functionality, such as storage and network interface management.

Requirements/dependencies/example playbook

See meta/main.yml

# playbook.yml
- hosts: my.CHANGEME.org
  roles:
    - nodiscc.xsrv.libvirt

See defaults/main.yml for all configurable variables

Usage

Below is an example of a simple setup with 2 libvirt hypervisors using shared storage:

# $ xsrv edit-group
# ~/playbooks/default/group_vars/all/all.yml
libvirt_networks:
  - name: default
    state: absent
  - name: prod1
    mac_address: "52:54:00:18:0c:cd"
    forward_dev: eth0
    bridge_name: virbr1
    ip_address: 10.10.10.1
    netmask: 255.255.255.0
    autostart: yes
    state: active
  - name: prod2
    mac_address: "52:54:00:18:0c:cd"
    forward_dev: eth0
    bridge_name: virbr2
    ip_address: 10.20.20.1
    netmask: 255.255.255.0
    autostart: yes
    state: active

# shared storage
libvirt_storage_pools:
  - name: LIBVIRT-STORAGE
    path: /mnt/LIBVIRT-STORAGE
    mode: "0770"

# wait for the shared storage to be mounted before autostarting VMs
libvirt_service_after:
  - 'mnt-LIBVIRT\x2dSTORAGE.mount'
# $ xsrv edit-host default hv1.example.org
# playbooks/default/host_vars/hv1.example.org/hv1.example.org.yml
libvirt_vms:
  - name: web.example.org
    xml_file: "{{ playbook_dir }}/data/libvirt/web.example.org.xml"
    state: running
  - name: tools.example.org
    xml_file: "{{ playbook_dir }}/data/libvirt/web.example.org.xml"
    state: running

libvirt_port_forwards:
  - vm_name: web.example.org
    vm_ip: 10.10.10.100
    vm_bridge: virbr1
    dnat:
      - host_interface: eth0 # forward HTTP connections on the host's public interface to this VM
        host_port: 80
        vm_port: 80
      - host_interface: eth0 # forward HTTPS connections on the host's public interface to this VM
        host_port: 443
        vm_port: 443
      - host_interface: eth0 # forward SSH connections on port 22100 of the host's public interface to this VM on port 22
        host_port: 22100
        vm_port: 22
    forward:
      - source_interface: virbr2 # allow VMs on virbr2/libvirt network prod2 to access netdata on this VM
        vm_port: 19999
      - source_interface: virbr2 # only allow the VM with IP 10.20.20.113 on virbr2/libvirt network prod2 to access port udp/123 on this VM
        source_ip: 10.20.20.113
        vm_port: 123
        protocol: udp
# $ xsrv edit-host default hv1.example.org
# playbooks/default/host_vars/hv1.example.org/hv1.example.org.yml
libvirt_vms:
  - name: demo1.example.org
    xml_file: "{{ playbook_dir }}/data/libvirt/demo1.example.org.xml"
  - name: demo2.example.org
    xml_file: "{{ playbook_dir }}/data/libvirt/demo2.example.org.xml"
  - name: demo777.example.org
    xml_file: "{{ playbook_dir }}/data/libvirt/demo777.example.org.xml"
    autostart: no

VM XML definitions follow the libvirt domain XML format and can be generated by creating a new VM from virt-manager or by the xsrv init-vm command.

Note: changing VM resources (RAM/CPU) in the XML definition, and applying the role will not affect running VMs until they are stopped and restarted. To force the current memory values defined in XML definitions to be applied immediately, without stopping/restarting VMs, use the tag utils-libvirt-setmem (or SSH to the hypervisor and use virsh setmem --live directly). The maximum memory allocation for the VM (<memory> XML tag ) must already be greater than the requested/current memory (<currentMemory> XML tag).

This role will not implement wrappers for all features of libvirt - notably, you should use virsh directly to edit VM XML definitions to add/remove hardware, change boot options, etc. See also The page about libvirt/virt-manager.

Tags

libvirt - setup libvirt virtualization toolkit
libvirt-storage - setup libvirt storage pools
libvirt-networks - setup libvirt virtual networks
libvirt-port-forwards - setup libvirt port forwards
libvirt-vms - setup libvirt virtual machines
utils-libvirt-setmem - (manual) update libvirt VMs current memory settings immediately

License

GNU GPLv3

References