- name: Figure out netplan file name
  shell: ls /etc/netplan
  register: netplan

- name: Disable cloud-init the easy way
  shell: touch /etc/cloud/cloud-init.disabled
  when: ("item" == "50-cloud-init.yaml")
  with_items:
    - "{{ netplan.stdout_lines }}"

- name: Remove stock netplan template
  file:
    state: absent
    path: /etc/netplan/{{ item }}
  with_items:
    - "{{ netplan.stdout_lines }}"
  when: netplan.stdout.find("yaml") != -1

- name: Cheap way to do systemd unmask
  file:
    state: absent
    dest: /etc/systemd/system/systemd-networkd-wait-online.service

- name: Ensure systemd-networkd-wait-online is enabled
  systemd:
    name: "{{ item }}"
    enabled: yes
  with_items:
    - systemd-networkd-wait-online
  when: systemd_networkd_active | bool

# ICO will always set gui_static_wan_ip away from the default of 'unset' while
# gui_static_wan turns dhcp on/off through wan_ip in computed_network and
# overrides gui_static_wan_ip that is present. Changing wan_ip in local_vars
# is a oneway street to static.
- 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: Supply netplan template
  template:
    dest: /etc/netplan/01-iiab-config.yaml
    src: network/netplan.j2
    backup: no

# should blow up here if we messed up the yml file
#- name: Generate netplan config
#  shell: netplan generate --debug

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

# wants a controlling terminal for the ENTER key, so it fails
#- name: Test netplan config
#  shell: netplan try --debug --timeout=2
#  register: test-netplan
#  ignore_errors: True

# and does not apply the generated config until rebooted
# or ignore the above test - on the fence atm...
#- name: Reload netplan
#  shell: netplan apply
#  when: not no_net_restart and test-netplan == "Configuration accepted"
