---
# file: roles/ovn/tasks/main.yml

- name: "set bool ovn_central"
  ansible.builtin.set_fact:
    ovn_central: "{{ inventory_hostname in ovn_central_servers }}"
  tags: ovn

- name: "Install the OVN central package"
  ansible.builtin.apt:
    name:
      - ovn-central
  when: ovn_central
  tags: ovn

- name: "Install the OVN host package"
  ansible.builtin.apt:
    name:
      - ovn-host
  notify:
    - Configure OVS
  when: ovn_host
  tags: ovn

- name: "Create OVN config directory"
  ansible.builtin.file:
    path: /etc/ovn
    mode: "0755"
    state: directory
  tags: ovn

- name: "Set ovn_connection_type"
  ansible.builtin.set_fact:
    ovn_connection_type: "{{ ovn_ssl | ternary('ssl', 'tcp') }}"
  tags: ovn

- name: "include pki"
  ansible.builtin.import_tasks: pki.yml
  when: ovn_ssl
  tags:
    - ovn
    - ovn_pki

- name: "set facts ovn_central bounds lists"
  ansible.builtin.set_fact:
    ovn_central_northbound_list: "{{ (ovn_central_northbound_list | default([])) + [ovn_connection_type + ':' + hostvars[item]['ovn_ip'] + ':6641'] }}"
    ovn_central_southbound_list: "{{ (ovn_central_southbound_list | default([])) + [ovn_connection_type + ':' + hostvars[item]['ovn_ip'] + ':6642'] }}"
  loop: "{{ ovn_central_servers }}"
  tags: ovn

- name: "set facts ovn_central bounds and ovn_cluster_main_ip"
  ansible.builtin.set_fact:
    ovn_central_northbound: "{{ ovn_central_northbound_list | join(',') }}"
    ovn_central_southbound: "{{ ovn_central_southbound_list | join(',') }}"
    ovn_cluster_main_ip: "{{ hostvars[ovn_cluster_main_name]['ovn_ip'] }}"
  tags: ovn

- name: "Configure OVN central database"
  ansible.builtin.template:
    src: ovn-central.j2
    dest: /etc/default/ovn-central
  notify:
    - Restart OVN central
    - Configure OVN AZ name
    - Enable OVN IC route sharing
  when: ovn_central
  tags: ovn

- name: "Configure OVN host"
  ansible.builtin.template:
    src: ovn-host.j2
    dest: /etc/default/ovn-host
  notify:
    - Restart OVN host
  when: ovn_host
  tags: ovn

- name: "include ic"
  ansible.builtin.import_tasks: ic.yml
  when: (ovn_ic is defined and ovn_ic) or (ovn_ic_db is defined and ovn_ic_db)
  tags:
    - ovn
    - ovn_ic

- name: "/etc/sysctl.conf for networking, when conntrack is loaded only"
  ansible.posix.sysctl:
    name: "{{ item['name'] }}"
    value: "{{ item['value'] }}"
  loop:
    - { 'name' : 'net.netfilter.nf_conntrack_buckets', 'value' : '262144' }
    - { 'name' : 'net.netfilter.nf_conntrack_max', 'value' : '1048576' }
  tags:
    - ovn
    - sysctl

- name: "make sure that nf_conntrack is loaded before sysctl rules are applied"
  community.general.modprobe:
    name: nf_conntrack
    persistent: present
  tags:
    - ovn
    - sysctl

- name: "/etc/ovn/alias.sh"
  ansible.builtin.template:
    src: alias.sh.j2
    dest: /etc/ovn/alias.sh
  tags: ovn

- name: "admin_ovn.sh"
  ansible.builtin.copy:
    src: "admin_ovn.sh"
    dest: "/etc/profile.d/admin_common.sh"
  tags: ovn

- name: "Flush handlers"
  meta: flush_handlers
  tags: ovn
