---
# https://github.com/nextcloud/helm/tree/master/charts/nextcloud


- name: Create a namespace for nextcloud
  k8s:
    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    name: "{{apps.nextcloud.namespace}}"
    api_version: v1
    kind: Namespace
    state: "{{apps.nextcloud.state}}"
  become: true


- name: create persistent volume resources
  kubernetes.core.k8s:
    kubeconfig: "/etc/rancher/k3s/k3s.yaml"
    state: "{{apps.nextcloud.state}}"
    definition: "{{ lookup('template', item) | from_yaml }}"
  loop:
#    - nextcloud/pv-apps.yaml
#    - nextcloud/pv-data.yaml
#    - nextcloud/pv-configs.yaml
    - nextcloud/pv-main.yaml
#    - nextcloud/pv-apps-claim.yaml
#    - nextcloud/pv-data-claim.yaml
#    - nextcloud/pv-configs-claim.yaml
    - nextcloud/pv-main-claim.yaml
  become: true

- name: Install nextcloud globally available
  block:
    - name: Add nextcloud chart helm repo
      local_action:
        module: kubernetes.core.helm_repository
        name: nextcloud
        repo_url: https://nextcloud.github.io/helm/

    - name: load variables files/nextcloud/values.yaml
      ansible.builtin.include_vars:
        file: files/nextcloud/values.yaml
        name: stash_values

    - name: Install Nextcloud Release
      local_action:
        module: kubernetes.core.helm
        release_state: "{{apps.nextcloud.state}}"
        name: nextcloud
        namespace: "{{apps.nextcloud.namespace}}"
        create_namespace: yes
        update_repo_cache: True
        chart_ref: nextcloud/nextcloud
        chart_version: "{{apps.nextcloud.chart_version}}"
        values: "{{stash_values}}"
        wait: true
