## Make sure to use an initrd providing firmware:
##   wget https://cdimage.debian.org/cdimage/firmware/testing/current/firmware.cpio.gz
##   cat initrd.gz firmware.cpio.gz > initrd-fw.gz
---
- name: Install packages related to iwd
  ansible.builtin.apt:
    name:
      - iwd
      - systemd-resolved
      - firmware-realtek # for our wifi sticks
    state: latest

- name: Disable wpa-supplicant
  ansible.builtin.systemd:
    name: wpa_supplicant.service
    enabled: False

- name: Enable iwd
  ansible.builtin.systemd:
    name: iwd.service
    enabled: True

- name: Prepare directory for iwd
  file:
    path: /var/lib/iwd/
    state: directory

- name: Configure iwd for wifi device
  ansible.builtin.copy:
    dest: /var/lib/iwd/{{ ssid }}.psk
    content: |
      [Security]
      Passphrase={{ wifipasswd }}

      # - name: Use iwd but ignore wlan interfaces in NetworkManager
      #   blockinfile:
      #     dest: /etc/NetworkManager/NetworkManager.conf
      #     block: |
      #       [device]
      #       wifi.backend=iwd
      #       match-device=interface-name:wl*
      #       managed=0
      #
      # - name: Configure systemd-networkd
      #   ansible.builtin.copy:
      #     dest: /etc/systemd/network/80-wlan-dhcp.network
      #     content: |
      #       [Match]
      #       Name=wl*
      #       [Network]
      #       DHCP=yes
      #       [DHCPv4]
      #       UseDomains=true
      #
- name: Enable systemd-networkd
  ansible.builtin.systemd:
    name: systemd-networkd.service
    enabled: True
