---
# NOTE: The order of tasks here is quite important. We must properly configure the network
# interfaces before being able to continue on to doing things that require the network (e.g.
# install apt packages).
- block:

  - name: Remove persistent net rules and generator
    file:
      path: "{{ item }}"
      state: absent
    with_items:
      - /lib/udev/rules.d/75-persistent-net-generator.rules
      - /etc/udev/rules.d/70-persistent-net.rules

  - name: Install udev rules for ethernet
    copy:
      src: "73-zoox-ethernet-{{ zoox_compute_version }}.rules"
      dest: "/etc/udev/rules.d/73-zoox-ethernet.rules"
      owner: root
      group: root
      mode: '644'

  - name: Install networking packages
    apt:
      name:
        - dnsmasq
        - ifenslave
        - resolvconf
        - ifplugd
        - vlan
      cache_valid_time: "{{ apt_cache_valid_time }}"
      install_recommends: no

  - name: Uninstall network-manager
    apt:
      name: network-manager
      state: absent
      purge: yes

  - name: Install rt_tables
    copy:
      src: "rt_tables"
      dest: "/etc/iproute2/rt_tables"
      owner: root
      group: root
      mode: '644'
    when: zoox_compute_version is version('3', '>=')

  # Right now, one line change.  If more configuration is needed
  # consider splitting into dedicated dnsmasq role.
  - name: Configure dnsmasq strict order
    lineinfile:
      path: /etc/dnsmasq.conf
      state: present
      regexp: "^#?strict-order"
      line: "strict-order"

  - name: Configure resolvconf interface order
    copy:
      src: "interface-order"
      dest: "/etc/resolvconf/interface-order"
      owner: root
      group: root
      mode: 0644

  - name: Configure network interfaces
    template:
      src: "{{ 'interfaces-3.5.j2' if zoox_compute_version is version('3.5', '=') else 'interfaces.j2' }}"
      dest: /etc/network/interfaces
      owner: root
      group: root
      mode: '644'

  - name: Install bond1 interface definition file
    template:
      src: bond1.j2
      dest: /etc/network/interfaces.d/bond1
      owner: root
      group: root
      mode: '644'
    # These are defaults; the file will be modified by scripts/ansible/roles/cellular_routing
    vars:
      lte_gateway_ip_1: '192.168.12.51'
      lte_gateway_ip_2: '192.168.12.52'
      gateway: '192.168.12.50'
    when: zoox_compute_version is version('3.5', '=')

  - name: Enable IP forwarding
    sysctl:
      name: net.ipv4.ip_forward
      value: '1'
      state: present
      reload: no

  - name: Enable ARP Filtering (all and default)
    sysctl:
      name: "{{ item }}"
      value: '1'
      state: present
      reload: no
    with_items:
      - net.ipv4.conf.all.arp_filter
      - net.ipv4.conf.default.arp_filter

  - name: Increase max multicast group memberships
    sysctl:
      name: net.ipv4.igmp_max_memberships
      value: '100'
      state: present
      reload: no

  - name: Install ifplugd configuration
    copy:
      src: "{{ 'ifplugd_configuration-bowtie' if zoox_compute_version is version('3.5', '=') else 'ifplugd_configuration' }}"
      dest: /etc/default/ifplugd
      owner: root
      group: root
      mode: '644'

  - name: Enable services
    service:
      name: "{{ item }}"
      enabled: true
      state: restarted
    with_items:
      - dnsmasq
      - resolvconf
      - ifplugd

  become: yes
