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

# /*---------------------------------------------------------------------------8
# |                                                                            |
# |             SAP: Register BOM                                              |
# |         SAP SCS: deploy SCS Parameter file install template                |
# |         SAP SCS: Install                                                   |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

---

- name:                                "SCS Install"
  ansible.builtin.debug:
    msg:
      - "SID:        {{ sid_to_be_deployed.sid | upper }}"

- name:                                "SCS Install: Calculating the domain value from {{ domain_name }}"
  ansible.builtin.set_fact:
    domain:                            "{{ domain_name | split('.') | first }}"
  when:
    - domain_name is defined
    - domain_name | type_debug != 'NoneType'
    - domain_name | trim | length > 1
    - domain is not defined

- name:                                Get the SCS Server name list
  ansible.builtin.set_fact:
    scs_server_temp: "{{ scs_server_temp | default([]) + [item] }}"
  with_items:
    - "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_SCS') }}"
    - "{{ query('inventory_hostnames', '{{ sap_sid | upper }}_DB') }}"

- name:                                "Set the SCS Server name"
  ansible.builtin.set_fact:
    scs_server:                        "{{ hostvars[scs_server_temp | first]['virtual_host'] }}"
    dir_params:                        '{{ tmp_directory_windows }}\{{ sid_to_be_deployed.sid | lower }}-params\'
    ha_identifier:                     "_ha"

# /*---------------------------------------------------------------------------8
# |                   Windows cluster configuration : START                    |
# +------------------------------------4--------------------------------------*/
# Start: call cluster configuration only if cluster is enabled
- name:                                "SCS Install: High availability Cluster tasks"
  when:
    - "'scs' in supported_tiers or 'ers' in supported_tiers "
    - scs_high_availability
    - ansible_os_family | upper == 'WINDOWS'
  block:
    - name:                            "SCS Install: High availability tasks"
      ansible.builtin.import_role:
        name:                          roles-os/windows/1.17-generic-wincluster
        tasks_from:                    main
      register:                        cluster_output
      vars:
        scs_cluster_hostname:                 "{{ scs_virtual_hostname }}"
        ers_cluster_hostname:                 "{{ ers_virtual_hostname }}"
        ansible_winrm_transport:              credssp
        ansible_winrm_server_cert_validation: ignore

    - name:                            "SCS Install: High availability flag"
      ansible.windows.win_file:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\cluster_config_done.txt'
        state:                         touch

    - name:                                "SCS Install"
      ansible.builtin.debug:
        msg:
          - "SCS Install: Reboot Cluster nodes"

    - name:                            "SCS Install: Reboot Cluster nodes"
      ansible.windows.win_reboot:
        reboot_timeout:                120
        post_reboot_delay:             60

# End: call cluster configuration only if cluster is enabled
# /*---------------------------------------------------------------------------8
# |                    Windows cluster configuration : END                     |
# +------------------------------------4--------------------------------------*/

# /*---------------------------------------------------------------------------8
# |                            SCS Install : START                             |
# +------------------------------------4--------------------------------------*/

- name:                                "SCS Install: Execute SAPINST on Central Services node {{ primary_node }}"
  when:
    - "'scs' in supported_tiers "
    - ansible_os_family | upper == 'WINDOWS'
  block:
    - name:                            "SCS Install: Create run flag directory"
      ansible.windows.win_file:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\'
        state:                         directory

    - name:                            "SCS Install: reset"
      ansible.windows.win_file:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_scs.txt'
        state:                         absent
      when:                            reinstall

    - name:                            "SCS Install: check if {{ sid_to_be_deployed.sid | upper }} is installed"
      ansible.windows.win_stat:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_scs.txt'
      register:                        scs_installed
      when:                            "'scs' in supported_tiers"

    - name:                            "SCS Install: check if installed"
      ansible.builtin.debug:
        var:                           scs_installed
        verbosity:                     2


    #   0x) Create directory for parameter files
    - name:                            "SCS Install: reset"
      ansible.windows.win_file:
        path:                          "{{ dir_params }}"
        state:                         absent

    - name:                            "SCS Install: Create params directory"
      ansible.windows.win_file:
        path:                          "{{ dir_params }}"
        state:                         directory

    - name:                            "SCS Install: Create temp directory for sid"
      ansible.windows.win_file:
        path:                          '{{ tmp_directory_windows }}\{{ sid_to_be_deployed.sid | upper }}\'
        state:                         directory

    #   1x) Create directories for sapinst_extraction and logs
    - name:                            "SCS Install: Create sapinst Extraction Directory"
      ansible.windows.win_file:
        path:                          '{{ download_directory_windows }}\sap_extract'
        state:                         directory
      when:
        - "'scs' in supported_tiers"

    - name:                            "SCS Install: Create log Directory"
      ansible.windows.win_file:
        path:                          '{{ log_dir_windows }}\sapinst_logs'
        state:                         directory

    - name:                            "SCS Install: Include 3.3.1-bom-utility role"
      ansible.builtin.include_role:
        name:                          roles-sap/windows/3.3.1-bom-utility
        tasks_from:                    bom-register
      vars:
        bom_name:                      "{{ bom_base_name }}"
        tier:                          "scs"
        task_prefix:                   "SCS Install: "
        sa_enabled:                    true

    - name:                            "SCS Install: Set BOM facts"
      ansible.builtin.set_fact:
        sap_inifile:                   "{{ bom_base_name }}-scs-{{ sid_to_be_deployed.sid | lower }}-{{ ansible_hostname }}.params"
        sap_inifile_template:          "{% if scs_high_availability %}scs_ha-inifile-param.j2{% else %}scs-inifile-param.j2{% endif %}"
        dir_params:                    '{{ tmp_directory_windows }}\{{ sid_to_be_deployed.sid | lower }}-params'
        product_catalog:               '{{ download_directory_windows }}\SWPM\product.catalog'
        product_id:                    "{% if scs_high_availability %}{{ bom.product_ids.scs_ha }}{% else %}{{ bom.product_ids.scs }}{% endif %}"
      when: ansible_hostname == primary_node

    - name:                            "SCS Install: Set BOM facts"
      ansible.builtin.set_fact:
        sap_inifile:                   "{{ bom_base_name }}-ers-{{ sid_to_be_deployed.sid | lower }}-{{ ansible_hostname }}.params"
        sap_inifile_template:          "ers-inifile-param.j2"
        dir_params:                    '{{ tmp_directory_windows }}\{{ sid_to_be_deployed.sid | lower }}-params'
        product_catalog:               '{{ download_directory_windows }}\SWPM\product.catalog'
        product_id:                    '{{ bom.product_ids.ers }}'
      when: ansible_hostname == secondary_node

    - name:                                "PAS Install: Create ini file {{ sap_inifile }}"
      ansible.windows.win_template:
        src:                               "{{ sap_inifile_template }}"
        dest:                              '{{ dir_params }}\{{ sap_inifile }}'
        force:                             true
      vars:
        sap_cd_package_cd1:
        sap_cd_package_cd2:
        sap_cd_package_cd3:
        sap_cd_package_cd4:
        sap_cd_package_cd5:
        sap_cd_package_hdbclient:
        sap_ciInstanceNumber:
        app_instance_number:
        sap_ciDialogWPNumber:
        sap_ciBtcWPNumber:
        sap_installSAPHostAgent:
        sap_profile_dir:
        sap_scs_hostname:              "{{ virtual_host }}"
        sap_db_hostname:
        sap_ciVirtualHostname:
        sap_appVirtualHostname:
        param_directory:               "{{ dir_params }}"
        sap_sid:                       "{{ sid_to_be_deployed.sid }}"
        scs_instance_number:           "{{ sid_to_be_deployed.ascs_inst_no }}"
        sidadm_uid:                    "{{ sid_to_be_deployed.sidadm_uid }}"

    - name:                            "SCS Install: install variables"
      ansible.builtin.debug:
        msg:
          - "INSTALLED:  {{ scs_installed.stat.exists }}"
          - "INIFILE:    {{ sap_inifile }}"
          - "PRODUCT ID: {{ product_id }}"
          - "INSTANCE:   {{ sid_to_be_deployed.ascs_inst_no }}"
          - "MEDIA:      {{ sap_drive }}"
          - "HOST:       {{ virtual_host }}"
          - "SID:        {{ sid_to_be_deployed.sid | upper }}"


    - name:                            "SCS Install status"
      block:

        - name:                        "SCS Install status"
          ansible.builtin.debug:
            msg:                       "SCS is already installed"

        - name:                        "SCS: - return value"
          ansible.builtin.set_fact:
            scs_already_installed:     true
      when:
        - scs_installed.stat.exists

    - name:                            "SCS Install"
      when:
        - "'scs' in supported_tiers"
        - not scs_installed.stat.exists
      block:

        # *====================================4=======================================8
        # |  SAP SCS: Install                                                          |
        # | 2230669 - System Provisioning Using a Parameter Input File                 |
        # *====================================4=======================================8

        - name:                        "SCS Install: Progress"
          ansible.builtin.debug:
            msg:                       "Start SCS Installation media extraction"

        - name:                        "SCS Install: sapinst Extraction"
          ansible.windows.win_shell: |
                                      {{ download_directory_windows }}\SWPM\sapinst.exe -extract
          args:
            chdir:                     '{{ download_directory_windows }}\sap_extract'
            creates:                   '{{ download_directory_windows }}\sap_extract\sapinst.exe'
          become:                      true
          become_method:               ansible.builtin.runas
          become_user:                 '{{ sap_sid }}adm@{{ domain_name }}'
          vars:
            ansible_become_password:   "{{ domain_user_password }}"
          register:                    scs_extraction
          failed_when:                 scs_extraction.rc > 0

        - name:                        "SCS Install: progress"
          ansible.builtin.debug:
            msg:                       "Starting SCS Installation for {{ sid_to_be_deployed.sid | upper }}. Please wait"

        - name:                        "SCS Installation"
          block:
            - name:                    "SCS Install: SAP SCS Install on {{ ansible_hostname }}"
              become:                  true
              become_method:           ansible.builtin.runas
              become_user:             '{{ sap_sid }}adm@{{ domain_name }}'
              ansible.windows.win_shell: |
                                          {{ download_directory_windows }}\sap_extract\sapinst.exe SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}\{{ sap_inifile }}   `
                                                                                                    SAPINST_EXECUTE_PRODUCT_ID={{ product_id }}                      `
                                                                                                    SAPINST_SKIP_DIALOGS=true                                        `
                                                                                                    SAPINST_START_GUI=false                                          `
                                                                                                    SAPINST_CONTROL_URL={{ product_catalog }}                        `
                                                                                                    SAPINST_START_GUISERVER=false
              args:
                chdir:                 '{{ log_dir_windows }}\sapinst_logs'
                creates:               '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_scs.txt'
              vars:
                ansible_become_password:   "{{ domain_user_password }}"
              register:                scs_installation
              failed_when:             scs_installation.rc > 0
          rescue:
            - name:                    "RESCUE - SCS Install: Show installation result"
              ansible.builtin.debug:
                var:                   scs_installation
              when:                    scs_installation is defined

            - name:                    'RESCUE - SCS Install: Check {{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64 exists'
              ansible.windows.win_stat:
                path:                  '{{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
              register:                stat_sys_exe_uc
              when:                    node_tier == 'scs'

            - name:                    'RESCUE - SCS Install: Check Check {{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64 is empty'
              ansible.windows.win_find:
                paths:                 '{{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
                patterns:              "*"
                file_type:             directory
              register:                find_sys_exe_uc_result
              when:
                - node_tier == 'scs'
                - stat_sys_exe_uc.stat.exists


            - name:                    'RESCUE - SCS Install: Cleanup {{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
              ansible.windows.win_file:
                path:                  '{{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
                state:                 absent
              when:
                - node_tier == 'scs'
                - find_sys_exe_uc_result is defined
                - find_sys_exe_uc_result | length > 0

            - name:                    "RESCUE - SCS Install: SAP SCS Install on {{ ansible_hostname }}"
              become:                  true
              become_method:           ansible.builtin.runas
              become_user:             '{{ sap_sid }}adm@{{ domain_name }}'
              ansible.windows.win_shell: |
                                          {{ download_directory_windows }}\sap_extract\sapinst.exe SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}\{{ sap_inifile }}   `
                                                                                                    SAPINST_EXECUTE_PRODUCT_ID={{ product_id }}             `
                                                                                                    SAPINST_SKIP_DIALOGS=true                                        `
                                                                                                    SAPINST_START_GUI=false                                          `
                                                                                                    SAPINST_CONTROL_URL={{ product_catalog }}                        `
                                                                                                    SAPINST_START_GUISERVER=false
              args:
                chdir:                 '{{ log_dir_windows }}\sapinst_logs'
                creates:               '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_scs.txt'
              vars:
                ansible_become_password:   "{{ domain_user_password }}"
              register:                scs_installation

        - name:                        "RESCUE - SCS Install: Show installation result"
          ansible.builtin.debug:
            var:                       scs_installation
          when:
            - scs_installation is defined
            - scs_installation.rc > 0

        - name:                        "SCS Install: Failure on {{ ansible_hostname }}"
          ansible.builtin.fail:
            msg:                       "SCS Installation failed on {{ ansible_hostname }}"
          when:                        scs_installation.rc > 0

        - name:                        "SCS Install: Cleanup ini file {{ ansible_hostname }}"
          ansible.windows.win_file:
            path:                      '{{ dir_params }}\{{ sap_inifile }}'
            state:                     absent

        - name:                        "SCS Install: Installation Failed"
          ansible.builtin.debug:
            var:                       scs_installation
          when:                        scs_installation.rc > 0

        - name:                        Find the installationSuccesfullyFinished.dat (SAPINST)
          ansible.windows.win_find:
            paths:                     '{{ log_dir_windows }}\sapinst_logs'
            file_type:                 file
            patterns:                  'installationSuccesfullyFinished.dat'
            recurse:                   true
          register:                    scs_installed_according_to_sapinst

        - name:                        "SCS Install: Installation results"
          ansible.builtin.debug:
            msg:                       "SCS Installation succeeded"
          when:
            - scs_installation.rc == 0
            - scs_installed_according_to_sapinst.matched > 0

        - name:                        "SCS Install: show"
          ansible.builtin.debug:
            var:                       scs_installed_according_to_sapinst
            verbosity:                 2

        - name:                        "SCS Install: flag"
          ansible.windows.win_file:
            path:                      '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_scs.txt'
            state:                     touch
          when:
            - scs_installation.rc == 0
            - scs_installed_according_to_sapinst is defined
            - scs_installed_according_to_sapinst.matched > 0

        - name:                        "SCS Install: High availability tasks"
          when:
            - "'scs' in supported_tiers "
            - scs_high_availability
            - scs_installation.rc == 0
            - scs_installed_according_to_sapinst.matched > 0
            - ansible_os_family | upper == 'WINDOWS'
          block:
            - name:                    "SCS Install: High availability post installation tasks"
              ansible.builtin.import_role:
                name:                  roles-os/windows/1.17-generic-wincluster
                tasks_from:            1.17.2-wincluster-postconfig.yaml
              register:                cluster_post_config_output
              vars:
                scs_cluster_hostname:  "{{ scs_virtual_hostname }}"
                ers_cluster_hostname:  "{{ ers_virtual_hostname }}"

# /*---------------------------------------------------------------------------8
# |                            SCS Install : END                               |
# +------------------------------------4--------------------------------------*/

# /*---------------------------------------------------------------------------8
# |                            ERS Install : START                             |
# +------------------------------------4--------------------------------------*/

- name:                                "ERS Install: Execute SAPINST for ERS in {{ secondary_node }}"
  when:
    - "'ers' in supported_tiers "
    - ansible_os_family | upper == 'WINDOWS'
  block:
    - name:                            "ERS Install: Create run flag directory"
      ansible.windows.win_file:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}'
        state:                         directory

    - name:                            "ERS Install: reset"
      ansible.windows.win_file:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_ers.txt'
        state:                         absent
      when:                            reinstall

    - name:                            "ERS Install: check if {{ sid_to_be_deployed.sid | upper }} is installed"
      ansible.windows.win_stat:
        path:                          '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_ers.txt'
      register:                        ers_installed
      when:                            "'ers' in supported_tiers"

    - name:                            "ERS Install: check if installed"
      ansible.builtin.debug:
        var:                           ers_installed
        verbosity:                     2


    #   0x) Create directory for parameter files
    - name:                            "ERS Install: Create params directory"
      ansible.windows.win_file:
        path:                          "{{ dir_params }}"
        state:                         directory

    - name:                            "ERS Install: Create temp directory for sid"
      ansible.windows.win_file:
        path:                          '{{ tmp_directory_windows }}\{{ sid_to_be_deployed.sid | upper }}'
        state:                         directory

    #   1x) Create directories for sapinst_extraction and logs
    - name:                            "ERS Install: Create sapinst Extraction Directory"
      ansible.windows.win_file:
        path:                          '{{ download_directory_windows }}\sap_extract'
        state:                         directory
      when:
        - "'ers' in supported_tiers"

    - name:                            "ERS Install: Create log Directory"
      ansible.windows.win_file:
        path:                          '{{ log_dir_windows }}\sapinst_logs'
        state:                         directory

    - name:                            "SCS Install: Include 3.3.1-bom-utility role"
      ansible.builtin.include_role:
        name:                          roles-sap/windows/3.3.1-bom-utility
        tasks_from:                    bom-register
      vars:
        bom_name:                      "{{ bom_base_name }}"
        tier:                          "ers"
        task_prefix:                   "ERS Install: "
        sa_enabled:                    true

    - name:                            "ERS Install: BOM Product Ids"
      ansible.builtin.debug:
        msg:
          - "BOM_PRODUCT_IDS:          {{ bom.product_ids }}"

    - name:                            "ERS Install: Set BOM facts"
      ansible.builtin.set_fact:
        sap_inifile:                   "{{ bom_base_name }}-ers-{{ sid_to_be_deployed.sid | lower }}-{{ ansible_hostname }}.params"
        sap_inifile_template:          "ers-inifile-param.j2"
        dir_params:                    '{{ tmp_directory_windows }}\{{ sid_to_be_deployed.sid | lower }}-params'
        product_catalog:               '{{ download_directory_windows }}\SWPM\product.catalog'
        product_id:                    '{{ bom.product_ids.ers }}'
      when: ansible_hostname == secondary_node or ( 'ers' in supported_tiers )

    # Notes: We currently support only ENSA1 with standalone enqueue replication servers
    #        When we support ENSA2 we need to change the ers_virtual_hostname to reflect
    #        the VCO that would be used to run ERS.
    - name:                            "ERS Install: Create ini file {{ sap_inifile }}"
      ansible.windows.win_template:
        src:                           "{{ sap_inifile_template }}"
        dest:                          '{{ dir_params }}\{{ sap_inifile }}'
        force:                         true
      vars:
        tier:                          "ers"
        sap_cd_package_cd1:
        sap_cd_package_cd2:
        sap_cd_package_cd3:
        sap_cd_package_cd4:
        sap_cd_package_cd5:
        sap_cd_package_hdbclient:
        sap_ciInstanceNumber:
        app_instance_number:
        sap_ciDialogWPNumber:
        sap_ciBtcWPNumber:
        sap_installSAPHostAgent:
        sap_profile_dir:
        sap_scs_hostname:              "{{ scs_virtual_hostname }}"
        sap_db_hostname:
        sap_ciVirtualHostname:
        sap_appVirtualHostname:
        param_directory:               "{{ dir_params }}"
        sap_sid:                       "{{ sid_to_be_deployed.sid }}"
        scs_instance_number:           "{{ sid_to_be_deployed.ascs_inst_no }}"
        sidadm_uid:                    "{{ sid_to_be_deployed.sidadm_uid }}"

    - name:                            "ERS Install: install variables"
      ansible.builtin.debug:
        msg:
          - "INSTALLED:  {{ ers_installed.stat.exists }}"
          - "INIFILE:    {{ sap_inifile }}"
          - "PRODUCT ID: {{ product_id }}"
          - "INSTANCE:   {{ sid_to_be_deployed.ascs_inst_no }}"
          - "MEDIA:      {{ sap_drive }}"
          - "HOST:       {{ virtual_host }}"
          - "SID:        {{ sid_to_be_deployed.sid | upper }}"


    - name:                            "ERS Install status"
      when:
        - ers_installed.stat.exists
      block:

        - name:                        "ERS Install status"
          ansible.builtin.debug:
            msg:                       "ERS is already installed"

        - name:                        "ERS: - return value"
          ansible.builtin.set_fact:
            ers_already_installed:     true


    - name:                            "ERS Install"
      when:
        - "'ers' in supported_tiers"
        - not ers_installed.stat.exists
      block:

# *====================================4=======================================8
# |  SAP ERS: Install                                                          |
# | 2230669 - System Provisioning Using a Parameter Input File                 |
# *====================================4=======================================8

        - name:                        "ERS Install: Progress"
          ansible.builtin.debug:
            msg:                       "Start ERS Installation media extraction"

        - name:                        "ERS Install: sapinst Extraction"
          ansible.windows.win_shell: |
                                          {{ download_directory_windows }}\SWPM\sapinst.exe -extract
          args:
            chdir:                     '{{ download_directory_windows }}\sap_extract'
            creates:                   '{{ download_directory_windows }}\sap_extract\sapinst.exe'
          become:                      true
          become_method:               ansible.builtin.runas
          become_user:                 '{{ sap_sid }}adm@{{ domain_name }}'
          vars:
            ansible_become_password:   "{{ domain_user_password }}"
          register:                    ers_extraction
          failed_when:                 ers_extraction.rc > 0

        - name:                        "ERS Install: progress"
          ansible.builtin.debug:
            msg:                       "Starting ERS Installation for {{ sid_to_be_deployed.sid | upper }}. Please wait"

        - name:                        "ERS Installation"
          block:
            - name:                    "ERS Install: SAP ERS Install on {{ ansible_hostname }}"
              become:                  true
              become_method:           ansible.builtin.runas
              become_user:             '{{ sap_sid }}adm@{{ domain_name }}'
              ansible.windows.win_shell: |
                                         {{ download_directory_windows }}\sap_extract\sapinst.exe SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}\{{ sap_inifile }}   `
                                                                                                    SAPINST_EXECUTE_PRODUCT_ID={{ product_id }}             `
                                                                                                    SAPINST_SKIP_DIALOGS=true                                        `
                                                                                                    SAPINST_START_GUI=false                                          `
                                                                                                    SAPINST_CONTROL_URL={{ product_catalog }}                        `
                                                                                                    SAPINST_START_GUISERVER=false
              args:
                chdir:                 '{{ log_dir_windows }}\sapinst_logs'
                creates:               '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_ers.txt'
              vars:
                ansible_become_password:   "{{ domain_user_password }}"
              register:                    ers_installation
              failed_when:                 ers_installation.rc > 0
          rescue:
            - name:                    'RESCUE - ERS Install: Check {{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64 exists'
              ansible.windows.win_stat:
                path:                  '{{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
              register:                stat_sys_exe_uc
              when:                    node_tier == 'ers'

            - name:                    'RESCUE - ERS Install: Check Check {{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64 is empty'
              ansible.windows.win_find:
                paths:                 '{{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
                patterns:              "*"
                file_type:             directory
              register:                find_sys_exe_uc_result
              when:
                - node_tier == 'ers'
                - stat_sys_exe_uc.stat.exists

            - name:                    "RESCUE - ERS Install: Show installation result"
              ansible.builtin.debug:
                var:                   ers_installation
                verbosity:             2
              when:                    ers_installation is defined

            - name:                    'RESCUE - ERS Install: Cleanup {{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
              ansible.windows.win_file:
                path:                  '{{ sap_drive }}\usr\sap\{{ sap_sid | upper }}\SYS\exe\uc\NTAMD64'
                state:                 absent
              when:
                - node_tier == 'ers'
                - find_sys_exe_uc_result is defined
                - find_sys_exe_uc_result | length > 0

        # - name:                            "ERS Install: Cleanup ini file {{ ansible_hostname }}"
        #   ansible.windows.win_file:
        #     path:                          '{{ dir_params }}\{{ sap_inifile }}'
        #     state:                         absent

        - name:                        "ERS Install: Installation Failed"
          ansible.builtin.debug:
            var:                       ers_installation
          when:                        ers_installation.rc > 0

        - name:                        Find the installationSuccesfullyFinished.dat (SAPINST)
          ansible.windows.win_find:
            paths:                     '{{ log_dir_windows }}\sapinst_logs'
            file_type:                 file
            patterns:                  'installationSuccesfullyFinished.dat'
            recurse:                   true
          register:                    ers_installed_according_to_sapinst

        - name:                        "ERS Install: Installation results"
          ansible.builtin.debug:
            msg:                       "ERS Installation succeeded"
          when:
            - ers_installation.rc == 0
            - ers_installed_according_to_sapinst.matched > 0

        - name:                        "ERS Install: show"
          ansible.builtin.debug:
            var:                       ers_installed_according_to_sapinst
            verbosity:                 2

        - name:                        "ERS Install: flag"
          ansible.windows.win_file:
            path:                      '{{ sap_deployment_automation }}\{{ sid_to_be_deployed.sid | upper }}\sap_deployment_ers.txt'
            state:                     touch
          when:
            - ers_installation.rc == 0
            - ers_installed_according_to_sapinst is defined
            - ers_installed_according_to_sapinst.matched > 0

        # ToDO: Add support for ENSA2 deployment's post config.
        #       Essentially it would be a copy of:
        #       "SCS Install: High availability post installation tasks"

# /*---------------------------------------------------------------------------8
# |                            ERS Install : END                               |
# +------------------------------------4--------------------------------------*/

...
# /*---------------------------------------------------------------------------8
# |                                   END                                      |
# +------------------------------------4--------------------------------------*/
