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

- name: "set bool ovn_central"
  ansible.builtin.set_fact:
    ovn_ic_db: "{{ inventory_hostname in ovn_ic_db_servers }}"

- name: "set facts ovn_ic bounds lists"
  ansible.builtin.set_fact:
    ovn_ic_northbound_list: "{{ (ovn_ic_northbound_list | default([])) + [ovn_connection_type + ':' + hostvars[item]['ovn_ip'] + ':6645'] }}"
    ovn_ic_southbound_list: "{{ (ovn_ic_southbound_list | default([])) + [ovn_connection_type + ':' + hostvars[item]['ovn_ip'] + ':6646'] }}"
  loop: "{{ ovn_ic_db_servers }}"

- name: "set facts ovn_ic bounds ovn_ic_cluster_main_ip"
  ansible.builtin.set_fact:
    ovn_ic_northbound: "{{ ovn_ic_northbound_list | join(',') }}"
    ovn_ic_southbound: "{{ ovn_ic_southbound_list | join(',') }}"
    ovn_ic_cluster_main_ip: "{{ hostvars[ovn_ic_cluster_main_name]['ovn_ip'] }}"

- name: "Install the OVN IC database package"
  ansible.builtin.apt:
    name:
      - ovn-ic-db
    install_recommends: false
    state: present
  when: ovn_ic_db

- name: "Install the OVN IC package"
  ansible.builtin.apt:
    name:
      - ovn-ic
    install_recommends: false
    state: present
  notify:
    - Enable OVN IC gateway
  when: ovn_ic

- name: "Create OVN IC override directory"
  ansible.builtin.file:
    path: /etc/systemd/system/ovn-ic.service.d
    mode: "0755"
    state: directory
  when: ovn_ic

- name: "Transfer OVN IC override"
  ansible.builtin.copy:
    content: |
      [Service]
      EnvironmentFile=-/etc/default/ovn-ic
      ExecStart=
      ExecStart=/usr/share/ovn/scripts/ovn-ctl start_ic --no-monitor $OVN_CTL_OPTS
    dest: /etc/systemd/system/ovn-ic.service.d/ansible.conf
  notify: Restart OVN IC
  when: ovn_ic

- name: "Configure OVN IC database"
  ansible.builtin.template:
    src: ovn-ic.j2
    dest: /etc/default/ovn-ic
  notify:
    - Restart OVN IC DB
    - Restart OVN IC
  when: ovn_ic_db or ovn_ic
