---
- name: libvirt | Install libvirt and qemu
  apk:
    name: libvirt-daemon, qemu-img, qemu-system-x86_64, qemu-modules
    state: present

- name: libvirt | Allow IPv6 RA passthrough for libvirt NAT
  sysctl:
    name: 'net.ipv6.conf.{{ item.name }}.accept_ra'
    value: '2'
    state: present
    reload: no
  loop: '{{ network_interfaces }}'

# This is for PulseAudio
- name: libvirt | Allow the normal user to interact with qemu system instance
  lineinfile:
    path: /etc/libvirt/qemu.conf
    owner: root
    group: root
    mode: 0644
    regexp: '^#?user = "'
    line: 'user = "{{ username }}"'

- name: libvirt | Use file-based permissions when polkit is not available
  lineinfile:
    path: /etc/libvirt/{{ item[0] }}.conf
    owner: root
    group: root
    mode: 0644
    regexp: '^#?{{ item[1] | regex_replace(" =.*$", "") }}'
    line: '{{ item[1] }}'
  loop: '{{ libvirt_daemons | product(libvirt_daemons_conf) | list }}'
  when: not use_polkit

- name: libvirt | Start modular services on runlevel 'default'
  service:
    name: '{{ item }}'
    runlevel: default
    enabled: yes
    state: started
  loop: '{{ libvirt_daemons + ["virtlogd", "virtlockd"] }}'
