- name: Install kvm virtualization
  pacman:
    name:
      - virt-manager
      - qemu-desktop
      - libvirt
      - dmidecode
      - edk2-ovmf
      - dnsmasq
      - samba
      - iptables-nft
      - mkcert

- name: update qemu.conf
  lineinfile:
    path: /etc/libvirt/qemu.conf
    regexp: '{{ item.regexp }}'
    line: '{{ item.replace }}'
  with_items:
    - { regexp: '^#user = ', replace: 'user = "{{ username }}"' }
    - { regexp: '^#group = ', replace: 'group = "{{ username }}"' }

- name: Add user {{ username }} to libvirt group
  ansible.builtin.user:
    groups: libvirt
    append: True
    user: "{{ username }}"

- name: copy samba config
  ansible.builtin.template:
    src: smb.conf.j2
    dest: /etc/samba/smb.conf
    owner: root
    group: root
    mode: '0644'

- name: Enable libvirt service
  ansible.builtin.systemd:
    name: libvirtd
    state: started
    enabled: yes

- name: Enable samba service
  ansible.builtin.systemd:
    name: smb
    state: started
    enabled: yes

- name: Define a new network for virtualization
  community.libvirt.virt_net:
    command: define
    name: jungle
    xml: '{{ lookup("template", "libvirt/jungle.xml.j2") }}'

- name: Ensure virtualization network active
  community.libvirt.virt_net:
    autostart: yes
    name: jungle
