<mohammadrony>
---
- name: Download MetalLB manifests
  get_url:
    url: https://raw.githubusercontent.com/metallb/metallb/{{ metallb.version }}/config/manifests/metallb-native.yaml
    # https://raw.githubusercontent.com/metallb/metallb/v0.14.3/config/manifests/metallb-native.yaml
    dest: /tmp/metallb-native.yaml

- name: Apply MetalLB YAML
  become_user: "{{ user }}"
  shell: |
    kubectl apply -f metallb-native.yaml
    kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
  args:
    chdir: /tmp/

- name: Pause for MetalLB pods to ready
  become_user: "{{ user }}"
  shell: kubectl wait pod --all --for=condition=Ready --namespace=metallb-system --timeout=300s

- name: Copy MetalLB IP pool config
  template:
    src: files/metallb-ip.yaml.j2
    dest: /tmp/metallb-ip.yaml

- name: Apply MetalLB IP pool
  become_user: "{{ user }}"
  shell: kubectl apply -f /tmp/metallb-ip.yaml
  args:
    chdir: /tmp/

- name: "Apply Nginx controller manifests"
  get_url:
    url: https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-{{ ingress_nginx.version }}/deploy/static/provider/{{ ingress_nginx.provider }}/deploy.yaml
    # https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.9.6/deploy/static/provider/baremetal/deploy.yaml
    dest: /tmp/ingress_nginx.yaml

- name: Apply Ingress YAML
  become_user: "{{ user }}"
  shell: kubectl apply -f ingress_nginx.yaml
  args:
    chdir: /tmp/

# For EXT4 and XFS file system
- name: Install Longhorn support package in CentOS
  yum:
    name:
      - iscsi-initiator-utils
      - nfs-utils
    state: present
    update_cache: true
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

- name: Install Longhorn support package in Ubuntu
  apt:
    name:
      - open-iscsi
      - nfs-common
    state: present
    update_cache: true
  when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

- name: Start iscsid
  service:
    name: iscsid
    state: started
    enabled: true

- name: Download Helm
  get_url:
    url: https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
    dest: /tmp/get-helm-3.sh

- name: Install Helm
  become_user: "{{ user }}"
  shell: bash get-helm-3.sh
  args:
    chdir: /tmp/

- name: Add Longhorn repo
  become_user: "{{ user }}"
  shell: |
    helm repo add longhorn https://charts.longhorn.io
    helm repo update

- name: Install longhorn using Helm
  become_user: "{{ user }}"
  shell: helm upgrade --install longhorn longhorn/longhorn --create-namespace --namespace longhorn-system