# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

---

# /*---------------------------------------------------------------------------8
# |                                                                            |
# |                Perform the Disk system mounts for observer node            |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

# Mount Filesystems

- name:                                "2.6 SAP Mounts: - Set the NFS Server name list"
  ansible.builtin.set_fact:
    nfs_server_temp:                   "{{ nfs_server_temp | default([]) + [item] }}"
  with_items:
    - "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_SCS') }}"
    - "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_DB') }}"

- name:                                "2.6 SAP Mounts: - Set the NFS Server name"
  ansible.builtin.set_fact:
    nfs_server:                        "{{ nfs_server_temp | first }}"

- name:                                "2.6 SAP Mounts: - Set the usr/sap/install path"
  ansible.builtin.set_fact:
    usr_sap_install_mount_point:       "{% if NFS_provider in ['AFS', 'ANF'] %}{% if usr_sap_install_mountpoint is defined %}{{ usr_sap_install_mountpoint }}{% else %}{{ nfs_server }}:{{ target_media_location }}{% endif %}{% else %}{{ nfs_server }}:{{ target_media_location }}{% endif %}"

- name:                                "2.6 SAP Mounts: - Show info"
  ansible.builtin.debug:
    msg:
      - "NFS Provider: {{ NFS_provider }}"
      - "Install path: {{ usr_sap_install_mount_point }}"
    verbosity:                         2

- name:                                "ORACLE: Create sap_deployment_automation folder"
  become:                              true
  become_user:                         root
  ansible.builtin.file:
    path:                              /etc/sap_deployment_automation
    mode:                              0755
    state:                             directory
    owner:                             oracle
    group:                             oinstall


- name:                                "2.6 SAP Mounts: - sapmnt"
  block:
    - name:                            "2.6 SAP Mounts: - Mount sapmnt file system when not using external NFS (on observer)"
      ansible.posix.mount:
        src:                           "{{ item.src }}"
        path:                          "{{ item.path }}"
        fstype:                        "{{ item.type }}"
        opts:                          defaults
        state:                         mounted
      loop:
        - { type: 'nfs4',  src: '{{ nfs_server }}:/sapmnt/{{ sap_sid | upper }}',  path: '/sapmnt/{{ sap_sid | upper }}' }
      when:
        - node_tier == 'observer'
        - sap_mnt is undefined
  rescue:
    - name:                            "2.6 SAP Mounts: - Re-mount File systems when not using external NFS (app & pas)"
      ansible.builtin.debug:
        msg:                           "Trying to remount sap_mnt"
    - name:                            "2.6 SAP Mounts: - Mount sapmnt file system when not using external NFS (all app tier)"
      ansible.posix.mount:
        src:                           "{{ item.src }}"
        path:                          "{{ item.path }}"
        fstype:                        "{{ item.type }}"
        opts:                          defaults
        state:                         remounted
      loop:
        - { type: 'nfs4',  src: '{{ nfs_server }}:/sapmnt/{{ sap_sid | upper }}',  path: '/sapmnt/{{ sap_sid | upper }}' }
      when:
        - node_tier == 'observer'
        - sap_mnt is undefined

- name:                                "2.6 SAP Mounts: - Mount Install folder when not using AFS"
  ansible.posix.mount:
    src:                               "{{ item.src }}"
    path:                              "{{ item.path }}"
    fstype:                            "{{ item.type }}"
    opts:                              defaults
    state:                             mounted
  loop:
    - { type: 'nfs4', src: '{{ usr_sap_install_mount_point }}', path: '{{ target_media_location }}' }
  when:
    - node_tier == 'observer'
    - usr_sap_install_mountpoint is not defined


- name:                                "AFS Mount: install"
  ansible.builtin.include_tasks:       2.6.0.1-afs-mount.yaml
  loop:
    - {
      'type':               'install',
      'temppath':           'sapinstall',
      'folder':             '{{ bom_base_name }}',
      'mount':              '{{ usr_sap_install_mountpoint }}',
      'opts':               'rw,hard,rsize=65536,wsize=65536,sec=sys,vers=4.1,tcp',
      'owner':              '{{ sidadm_uid }}',
      'path':               '/usr/sap/install',
      'permissions':        '0777',
      'set_chattr_on_dir':  false,
      'target_nodes':       ['all'],
      'create_temp_folders': true
    }
  vars:
    primary_host:                     "{{ nfs_server }}"
  when:
    - tier == 'sapos'
    - usr_sap_install_mountpoint is defined
    - node_tier == 'observer'
