---
# Download debs:
- name: OVS | downloading debs
  get_url:
    url: "{{ ovs_info.debs_targz_link }}"
    dest: "{{ ovs_info.prebuilt_packages_download_path }}/ovs_debs.tar.gz"
    force_basic_auth: yes
    mode: 0755
    timeout: 30
  retries: 3

- name: OVS | unarchive ovs_debs.tar.gz
  unarchive:
    src: "{{ ovs_info.prebuilt_packages_download_path }}/ovs_debs.tar.gz"
    dest: "{{ ovs_info.prebuilt_packages_download_path }}"
    remote_src: yes

- name: OVS | install datapath debs
  apt: deb="{{ ovs_info.prebuilt_packages_download_path }}/{{item}}_{{ ovs_info.prebuilt_version }}_all.deb"
  with_items:
    - openvswitch-datapath-dkms
    - python-openvswitch

- name: OVS | add modules
  lineinfile:
    path: "{{ ovs_info.modules_file_path }}"
    create: yes
    line: |
      openvswitch
      vport_stt
      vport_geneve

- name: OVS | reload modules
  action: shell depmod -a

- name: OVS | remove modprobe
  modprobe:
    name: "{{item}}"
    state: absent
  with_items:
    - openvswitch
    - vport-geneve
    - vport-stt

- name: OVS | add modprobe
  modprobe:
    name: "{{item}}"
    state: present
  with_items:
    - openvswitch
    - vport-geneve
    - vport-stt

- name: OVS | install debs
  apt: deb="{{ ovs_info.prebuilt_packages_download_path }}/{{item}}_{{ ovs_info.prebuilt_version }}_amd64.deb"
  with_items:
    - libopenvswitch
    - openvswitch-common
    - openvswitch-switch
    - ovn-common
    - ovn-host

- name: OVS | install controller deb if applicable
  apt: deb="{{ ovs_info.prebuilt_packages_download_path }}/ovn-central_{{ ovs_info.prebuilt_version }}_amd64.deb"
  when: controller

# TODO: make this better
- name: OVS | Mock OVS services
  lineinfile:
    path: "{{item}}"
    create: yes
    line: |
      [Unit]
      Description=openvswitch
      Documentation=https://github.com/openvswitch/ovs
      [Service]
      ExecStart=/bin/bash sleep 1
      [Install]
      WantedBy=multi-user.target
  with_items:
    - /etc/systemd/system/openvswitch.service
    - /etc/systemd/system/ovn-controller.service
    - /etc/systemd/system/ovn-northd.service
