# NM-debian.yml
- name: Stopping services
  include_tasks: down-debian.yml

# provide keyfile layout like the XO's used way back.
#- name: Create uuid for NM's keyfile store
#  shell: uuidgen
#  register: uuid_response

#- name: Put the uuid in place
#  set_fact:
#    gen_uuid: "{{ uuid_response.stdout_lines[0] }}"

# systemd-networkd handles br0 except for Raspbian
- name: Remove stale br0 for NetworkManager
  file:
    dest: /etc/NetworkManager/system-connections/bridge-br0
    state: absent

- name: Copy ap0-manage.conf for NetworkManager
  template:
    dest: /etc/NetworkManager/conf.d/ap0-manage.conf
    src: network/ap0-manage.conf
    mode: 0644
  when: wifi_up_down

- name: Copy manage.conf for NetworkManager when wifi_up_down False
  template:
    dest: /etc/NetworkManager/conf.d/wifi-manage.conf
    src: network/manage.conf.j2
    mode: 0644
  when: discovered_wireless_iface != "none" and not wifi_up_down

- name: Remove manage.conf for NetworkManager when wifi_up_down True
  file:
    dest: /etc/NetworkManager/conf.d/wifi-manage.conf
    state: absent
  when: wifi_up_down

- name: Removing static for NetworkManager
  file:
    dest: /etc/NetworkManager/system-connections/iiab-static
    state: absent
  when: wan_ip == "dhcp"

- name: Static IP computing CIDR
  shell: netmask {{ wan_ip }}/{{ wan_netmask }} | awk -F "/" '{print $2}'
  register: CIDR
  when: wan_ip != "dhcp"

- name: Static IP setting CIDR
  set_fact:
    wan_cidr: "{{ CIDR.stdout }}"
  when: wan_ip != "dhcp"

- name: Create uuid for NM's keyfile store static
  shell: uuidgen
  register: uuid_response2
  when: wan_ip != "dhcp"

- name: Put the uuid in place
  set_fact:
    gen_uuid2: "{{ uuid_response2.stdout_lines[0] }}"
  when: wan_ip != "dhcp"

- name: Copy static template for NetworkManager
  template:
    dest: /etc/NetworkManager/system-connections/iiab-static
    src: network/NM-static.j2
    mode: 0600
  when: wan_ip != "dhcp"

- name: Reload systemd
  systemd:
    daemon_reload: yes

- name: Restart the NetworkManager service
  systemd:
    name: NetworkManager
    state: restarted
  when: not nobridge is defined and not no_net_restart
