---
# tasks file for networkd

- name: Assertions
  assert:
    that:
        - ansible_os_family == 'Debian'
        - ansible_distribution_major_version >= 8
        - ansible_service_mgr == 'systemd'

- name: APT install
  with_items:
      - libnss-myhostname
      - libnss-resolve
  apt:
      name: '{{ item }}'
      state: present
      update_cache: yes
      cache_valid_time: 3600

- name: Configure network interfaces
  with_fileglob:
      - '{{ role_path }}/templates/networkd/*.netdev'
      - '{{ playbook_dir }}/templates/networkd/*.netdev'
      - '{{ role_path }}/templates/networkd/*.network'
      - '{{ playbook_dir }}/templates/networkd/*.network'
      - '{{ role_path }}/templates/networkd/*.link'
      - '{{ playbook_dir }}/templates/networkd/*.link'
  template:
      src: '{{ item }}'
      dest: '/etc/systemd/network/{{ item|basename }}'
      owner: root
      group: root
      mode: 0o0644
  notify:
      - Restart Systemd-networkd

- name: Check if networking service is available
  stat:
      path: /etc/init.d/networking
  register: stat_networking

- name: Disable networking service
  when: stat_networking.stat.exists
  service:
      name: networking
      state: stopped
      enabled: no

- name: Enable services
  with_items:
      - systemd-resolved
      - systemd-networkd
  service:
      name: '{{ item }}'
      state: started
      enabled: yes
