---
- name: networking | Overwrite /etc/hosts to support IPv6
  copy:
    src: networking/hosts
    dest: /etc/hosts
    owner: root
    group: root
    mode: '644'

# NOTE: already set in /lib/sysctl.d/00-alpine.conf but it doesn't hurt re-apply
- name: networking | Set IPv6 Privacy Extension (RFC 4941)
  ansible.posix.sysctl:
    name: '{{ (item | split("=") | map("trim") | list)[0] }}'
    value: '{{ (item | split("=") | map("trim") | list)[1] }}'
    state: present
  loop:
    - net.ipv6.conf.all.use_tempaddr = 2
    - net.ipv6.conf.default.use_tempaddr = 2

- name: networking | Set IPv6 stable privacy address (RFC 7217)
  ansible.posix.sysctl:
    name: '{{ (item | split("=") | map("trim") | list)[0] }}'
    value: '{{ (item | split("=") | map("trim") | list)[1] }}'
    state: present
  loop:
    - net.ipv6.conf.default.addr_gen_mode = 2
    - net.ipv6.conf.all.addr_gen_mode = 2
    - net.ipv6.conf.default.stable_secret = {{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }}:{{ (2**16 - 1) | random_hex }} # noqa: yaml[line-length]
  when: ipv6_stable_privacy_addr | bool

- name: networking | Install {{ dhcp_client }}
  community.general.apk:
    name: '{{ dhcp_client }}'
    state: present
  when: dhcp_client != 'udhcpc'

# Assume 'networking' service is already started on 'boot' before the playbook is run
- name: networking | Tweak the default 'networking' service
  import_tasks: networking/networking.yml

- name: networking | Configure iwd
  import_tasks: networking/iwd.yml
  tags: laptop

- name: networking | Configure connman
  import_tasks: networking/connman.yml
  when: dhcp_client == 'connman'
