# Install ovn onto cluster
# This is done when the cluster is up with no networking

# On a convienent host, clone ovn-kubernetes cd to dist/ansible 
# git clone https://github.com/ovn-org/ovn-kubernetes ~/ovn-kubernetes
# cd ~/ovn-kubernetes/dist/ansible

# edit hosts file for the hostname of the master

# ./run-playbook

- hosts: nodes
  become: yes
  become_user: root
  tasks:
  - name: copy tmpfile.d file
    template: src=../templates/cleanup-ovn-cni.conf.j2 dest=/usr/lib/tmpfiles.d/cleanup-ovn-cni.conf

- hosts: masters
  become: yes
  become_user: root
  tasks:
  # OVN data is persistent, so delete the database
  - name: Remove OVS DB Content
    file:
      state: absent
      name: /var/lib/openvswitch/ovnnb_db.db
  - name: Remove OVS DB Content
    file:
      state: absent
      name: /var/lib/openvswitch/ovnsb_db.db

  # copy yaml files to master
  - name: Make dirctory /root/ovn/yaml
    file:
      state: directory
      path: /root/ovn/yaml
  - name: Make directory /root/ovn/scripts
    file:
      state: directory
      path: /root/ovn/scripts
  - name: Copy ovn-logs
    copy:
      src: scripts/ovn-logs
      dest: /root/ovn/ovn-logs
      mode: 0755
  - name: Copy ovn-display
    copy:
      src: scripts/ovn-display
      dest: /root/ovn/ovn-display
      mode: 0755
  - name: Copy ovn-debug
    copy:
      src: scripts/ovn-debug
      dest: /root/ovn/ovn-debug
      mode: 0755
  - name: Copy ovnkube-master.yaml
    template: src=../templates/ovnkube-master.yaml.j2 dest=/root/ovn/yaml/ovnkube-master.yaml
  - name: Copy ovnkube-node.yaml
    template: src=../templates/ovnkube-node.yaml.j2 dest=/root/ovn/yaml/ovnkube-node.yaml

  - name: Get the k8s_apiserver
    shell: grep server /etc/origin/node/node.kubeconfig | awk '{ print $2 }'
    register: k8s_apisvr
  - set_fact: k8s_apiserver={{ k8s_apisvr }}
  - name: Set up ovn
    template: src=../templates/ovn-setup.yaml.j2 dest=/root/ovn/yaml/ovn-setup.yaml

  - name: Provision the OVN 
    shell: oc create -f /root/ovn/yaml/ovn-setup.yaml
    ignore_errors: yes
  - name: Provision the OVN Project
    shell: oc project ovn-kubernetes
    ignore_errors: yes
  - name: Add adm policy auyuid
    shell: oc adm policy add-scc-to-user anyuid -z ovn
    ignore_errors: yes

  - name: Start the node ovn daemonset on all nodes
    shell: oc create -f /root/ovn/yaml/ovnkube-node.yaml
    ignore_errors: yes
  - name: Start the master ovn daemonset
    shell: oc create -f /root/ovn/yaml/ovnkube-master.yaml
    ignore_errors: yes
