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

---
# /*----------------------------------------------------------------------------8
# |                                                                            |
# |                      Playbook for HANA DB HSR                          |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

- hosts:                               localhost
  name:                                "Get SAP Password from KeyVault"
  gather_facts:                        true
  vars_files:
    - vars/ansible-input-api.yaml      # API Input template with defaults
  tasks:

    - name:                            "Backward Compatibility - Check required Database HA variables"
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"
      when:
        - db_high_availability is defined
        - database_high_availability is not defined

    - name:                            "Initialization"
      when:                            database_high_availability
      block:

        - name:                        "Create Progress folder"
          ansible.builtin.file:
            path:                      "{{ _workspace_directory }}/.progress"
            state:                     directory
            mode:                      0755

        - name:                        "Remove db-ha-done flag"
          ansible.builtin.file:
            path:                      "{{ _workspace_directory }}/.progress/db-ha-done"
            state:                     absent

        - name:                        "Ensure passlib is installed on the controller"
          become:                      true
          become_user:                 root
          ansible.builtin.pip:
            name:                      passlib
            state:                     present
          tags:
            - always

        # MKD - Fencing is not a tier... it is a misuse to code for that
        #       To detect we should be using:
        #         database_high_availability
        #           AND
        #         database_cluster_type == "AFA"
        #         database_cluster_type == "ASD"
        #         database_cluster_type == "ISCSI"

        # - name:                           Initialize facts
        #   ansible.builtin.set_fact:
        #     tier:                         fencing

        - name:                        "Run the misc/password role"
          ansible.builtin.include_role:
            name:                      roles-misc/0.1-passwords
            public:                    true
          tags:
                                       - 0.1-passwords

        - name:                        "OS configuration playbook: - Read password"
          ansible.builtin.include_role:
            name:                      roles-misc/0.1-passwords
            tasks_from:                windows.yaml
            public:                    true
          when:                        platform == "SQLSERVER"
          tags:
                                       - 0.1-win-passwords

        - name:                        "WinCluster-Create: Get witness account details"
          ansible.builtin.include_role:
            name:                      roles-misc/0.2-kv-secrets
            tasks_from:                wincluster-witness.yaml
            public:                    true
          vars:
            operation:                 fencing
          when:
                                       - platform | upper == "SQLSERVER"
          tags:
                                       - 0.2-wincluster-witness

        - name:                        "Run the keyvault role"
          ansible.builtin.include_role:
            name:                      roles-misc/0.2-kv-secrets
          vars:
            operation:                 fencing
          tags:
                                       - kv-secrets


# +------------------------------------4--------------------------------------*/

- hosts:                               "{{ sap_sid | upper }}_DB"
  name:                                HANA DB HA Configuration
  remote_user:                         "{{ orchestration_ansible_user }}"
  gather_facts:                        true                                    # Important to collect hostvars information
  vars_files:
    - vars/ansible-input-api.yaml                                              # API Input template with defaults

  tasks:
  # -------------------------------------+---------------------------------------8
  #
  # Build the list of tasks to be executed in order here.
  #
  # -------------------------------------+---------------------------------------8
    - name:                            "Backward Compatibility - Check required Database HA variables"
      when:
                                       - db_high_availability is defined
                                       - database_high_availability is not defined
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"


    - name:                            "0.0 Validations: - Gather facts for first time"
      ansible.builtin.setup:
      tags:
                                       - always

    - name:                            "HANA HA Setup"
      become:                          true
      when:
                                       - database_high_availability
                                       - platform == 'HANA'
      block:
        - name:                        "Install HANA System Replication"
          block:
            - name:                    "Retrieve Azure Instance Metadata results"
              ansible.builtin.uri:
                url:                   http://169.254.169.254/metadata/instance?api-version=2021-02-01
                use_proxy:             false
                headers:
                  Metadata:            true
              register:                azmetadata

            - name:                    "Show IMDS results"
              ansible.builtin.debug:
                var:                   azmetadata.json
                verbosity:             2

            - name:                    "Set the primary intance nic and secondary instance nic IP"
              ansible.builtin.set_fact:
                primary_ip:            "{{ azmetadata.json.network.interface[0].ipv4.ipAddress[0].privateIpAddress }}"
                subnet_prefix:         "{{ azmetadata.json.network.interface[0].ipv4.subnet[0].prefix }}"

            - name:                    "Initialize facts for hdb-hsr role"
              ansible.builtin.set_fact:
                tier:                      hdb                                      # Actions for HANA DB Servers
                primary_instance_name :    "{{ ansible_play_hosts_all[0] }}"        # Setting up Primary Instance Name
                primary_instance_ip_db:    "{{ hostvars[ansible_play_hosts_all[0]]['primary_ip'] | string }}"
                secondary_instance_name:   "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                secondary_instance_ip_db:  "{{ hostvars[ansible_play_hosts_all[1]]['primary_ip'] | string }}"
                main_password:             "{{ hostvars.localhost.sap_password }}"

            - name:                    "Run the db/hdb-hsr role"
              ansible.builtin.include_role:
                name:                  roles-db/4.0.1-hdb-hsr
              when:
                                       - node_tier == 'hana'
              tags:
                                       - 4.0.1-hdb-hsr

        - name:                        "Install HANA Pacemaker resources"
          block:
            - name:                    "Initialize facts for HANA pacemaker resources"
              ansible.builtin.set_fact:
                tier:                      ha
                password_ha_db_cluster:    "{{ hostvars.localhost.db_cluster_password }}"
                main_password:             "{{ hostvars.localhost.sap_password }}"
                primary_instance_name:     "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                primary_instance_ip_db:    "{{ hostvars[ansible_play_hosts_all[0]]['primary_ip'] | string }}"
                secondary_instance_name:   "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                secondary_instance_ip_db:  "{{ hostvars[ansible_play_hosts_all[1]]['primary_ip'] | string }}"
                fencing_spn_client_id:     "{% if not use_msi_for_clusters %}{{ hostvars.localhost.sap_fencing_spn_client_id }}{% endif %}"
                fencing_spn_client_pwd:    "{% if not use_msi_for_clusters %}{{ hostvars.localhost.sap_fencing_spn_pwd }}{% endif %}"
                fencing_spn_tenant_id:     "{% if not use_msi_for_clusters %}{{ hostvars.localhost.sap_fencing_spn_tenant_id }}{% endif %}"
              tags:
                                       - always

            - name:                    "HANA HA Setup: - Ensure the needed services are started"
              ansible.builtin.include_role:
                name:                  roles-os/1.16-services
              tags:
                                       - 1.16-services

            - name:                    "HANA HA Setup: - run the Pacemaker role"
              ansible.builtin.include_role:
                name:                  roles-sap/5.5-hanadb-pacemaker
              when:
                                       - database_high_availability
                                       - not database_scale_out
              tags:
                                       - 5.5-hanadb-pacemaker

            - name:                    "HANA HA Setup: - run the Pacemaker role for scale out"
              ansible.builtin.include_role:
                name:                  roles-sap/5.8-hanadb-scaleout-pacemaker
              when:
                                       - database_high_availability
                                       - database_scale_out
              tags:
                                       - 5.8-hanadb-scaleout-pacemaker


# -------------------------------------+---------------------------------------8
#
# Build the list of tasks to be executed in order here.
#
# -------------------------------------+---------------------------------------8

# Oracle installation has to be done with oracle user. Change the user to become orasid and call the installer.

- hosts:                               "{{ sap_sid | upper }}_DB"
  name:                                DB Data Guard setup - Oracle
  remote_user:                         "{{ orchestration_ansible_user }}"
  gather_facts:                        true
  vars_files:
    - vars/ansible-input-api.yaml                               # API Input template with defaults

  tasks:
    - name:                            "Backward Compatibility - Check required Database HA variables"
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"
      when:
        - db_high_availability is defined
        - database_high_availability is not defined

    - name:                            "Initialize facts for HA resources"
      ansible.builtin.set_fact:
        tier:                          ha
        primary_instance_name:         "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
        secondary_instance_name:       "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
      when:
        - database_high_availability
        - ansible_play_hosts_all | length >= 2

    - name:                            "Oracle Data Guard Setup"
      when:
        - database_high_availability
        - platform in ['ORACLE', 'ORACLE-ASM']
        - inventory_hostname == primary_instance_name
      become:                          true
      block:
        - name:                        Setting the DB facts
          ansible.builtin.set_fact:
            tier:                      ora                                    # Actions for Oracle DB Servers
            action:                    'Prepare_Secondary'
            main_password:             "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        - name:                        Oracle Data guard
          ansible.builtin.include_role:
            name:                      roles-db/4.1.3-ora-dg
          tags:
            - 4.1.3-ora-dg

    - name:                            "Oracle Data Guard Setup"
      when:
        - database_high_availability
        - platform in ['ORACLE', 'ORACLE-ASM']
      become:                          true
      block:
        - name:                        Setting the DB facts
          ansible.builtin.set_fact:
            tier:                      ora                                    # Actions for Oracle DB Servers
            action:                    'Prepare_and_Restore'
            main_password:             "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        - name:                        Oracle Data guard
          ansible.builtin.include_role:
            name:                      roles-db/4.1.3-ora-dg
          tags:
            - 4.1.3-ora-dg

        - name:                       "Observer Playbook: - Run post installation routines"
          ansible.builtin.include_role:
            name:                     roles-sap/7.0.0-post-install
          vars:
            suffix:                   "_DC_ACTION_1"
            tier:                     'oracle'

    - name:                            "Oracle Data Guard Setup"
      when:
        - database_high_availability
        - platform in ['ORACLE', 'ORACLE-ASM']
        - inventory_hostname == primary_instance_name
      become:                          true
      block:
        - name:                        Setting the DB facts
          ansible.builtin.set_fact:
            tier:                      ora                                    # Actions for Oracle DB Servers
            action:                    'Post_Processing_Primary'
            main_password:             "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        - name:                        Oracle Data guard
          ansible.builtin.include_role:
            name:                      roles-db/4.1.3-ora-dg
          tags:
            - 4.1.3-ora-dg

        - name:                       "Observer Playbook: - Run post installation routines"
          ansible.builtin.include_role:
            name:                     roles-sap/7.0.0-post-install
          vars:
            suffix:                   "_DC_ACTION_2"
            tier:                     'oracle'


    - name:                            "Oracle Data Guard Setup on Secondary"
      when:
        - db_high_availability
        - platform in ['ORACLE', 'ORACLE-ASM']
        - inventory_hostname == secondary_instance_name
      become:                          true
      block:
        - name:                        Setting the DB facts
          ansible.builtin.set_fact:
            tier:                      ora                                    # Actions for Oracle DB Servers
            action:                    'Post_Processing_Secondary'
            main_password:             "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        - name:                        Oracle Data guard
          ansible.builtin.include_role:
            name:                      roles-db/4.1.3-ora-dg
          tags:
            - 4.1.3-ora-dg

        - name:                       "Observer Playbook: - Run post installation routines"
          ansible.builtin.include_role:
            name:                     roles-sap/7.0.0-post-install
          vars:
            suffix:                   "_DC_ACTION_2"
            tier:                     'oracle'


    - name:                            "Oracle Data Guard Setup on Secondary"
      when:
        - db_high_availability
        - platform in ['ORACLE', 'ORACLE-ASM']
        - inventory_hostname == secondary_instance_name
      become:                          true
      block:
        - name:                        Setting the DB facts
          ansible.builtin.set_fact:
            tier:                      ora                                    # Actions for Oracle DB Servers
            action:                    'Post_Processing_Secondary'
            main_password:             "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        - name:                        Oracle Data guard
          ansible.builtin.include_role:
            name:                      roles-db/4.1.3-ora-dg
          tags:
            - 4.1.3-ora-dg

        - name:                       "Observer Playbook: - Run post installation routines"
          ansible.builtin.include_role:
            name:                     roles-sap/7.0.0-post-install
          vars:
            suffix:                   "_DC_ACTION_2"
            tier:                     'oracle'

# /*----------------------------------------------------------------------------8
# |                                                                            |
# |                         PLAY FOR Observer Node setup                       |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

- hosts:                               "{{ sap_sid | upper }}_OBSERVER_DB"

  name:                                Observer Playbook
  remote_user:                         "{{ orchestration_ansible_user }}"
  gather_facts:                        true                                    # Important to collect hostvars information
  vars_files:
    - vars/ansible-input-api.yaml                                              # API Input template with defaults

  tasks:
    - name:                            "Backward Compatibility - Check required Database HA variables"
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"
      when:
        - db_high_availability is defined
        - database_high_availability is not defined

    - name:                            "Get Instance names"
      ansible.builtin.set_fact:
        primary_observer_name :        "{{ ansible_play_hosts_all[0] }}"        # Setting up Primary Instance Name
        secondary_observer_name:       "{{ ansible_play_hosts_all[1] | default('N/A') }}"         # Setting up Secondary Instance Name

    - name:                            "Oracle: Observer setup"
      become:                          true
      when:
        - database_high_availability
        - platform in ['ORACLE', 'ORACLE-ASM']
      block:
        - name:                            "Observer Playbook: Setting the DB facts"
          ansible.builtin.set_fact:
            node_tier:                     observer
            action:                        'Setup_Observer'
            main_password:                 "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        # Specify the active tier for this playbook run
        - name:                            "Observer Playbook: Setting the os tier facts"
          ansible.builtin.set_fact:
            tier:                          os
          tags:
            - always

        - name:                            "Observer Play"
          become:                          true
          block:
            - name:                        "Observer Playbook: Check if the mounts place"
              ansible.builtin.include_role:
                name:                      roles-sap-os/2.6-sap-mounts

        - name:                            "Observer Play"
          become:                          true
          block:
            - name:                        "Observer Playbook: Check if the mounts place"
              ansible.builtin.include_role:
                name:                      roles-sap-os/2.6-sap-mounts

            - name:                        "Observer Playbook: Oracle Observer setup"
              ansible.builtin.include_role:
                name:                      roles-db/4.1.3-ora-dg

            - name:                        "Observer Playbook: - Run post installation routines"
              ansible.builtin.include_role:
                name:                      roles-sap/7.0.0-post-install
              vars:
                suffix:                    "_OBSERVER"
                tier:                      'oracle'

# # /*-------------------------------------------------------------------------8
# # |                                                                          |
# # |           Playbook for DB2 DB Install  -  High Availability              |
# # |                                                                          |
# # +------------------------------------4------------------------------------*/
# -------------------------------------+---------------------------------------8
#   DB2 - High Availabiliy Setup                                               |
#     a. Install DB2 on Primary Server                                         |
#     b. Take offline backup of Primary DB                                     |
#     c. Install DB2 on Secondary Server using backup restore option           |
#     d. Rrestore Secondary/Standby from backup of primary system              |
#     e. HADR  parameters on primary and secodary                              |
#     f. HADR definition on primary & Secondary                                |
#     g. HADR setup Checks                                                     |
#    Split the following section into the pacemaker roles-sap/5.7db2-pacemaker |
#     c. Pacemaker setup on both nodes                                         |
#     d. Stonith Checks                                                        |
#     e. Changes to SAP profile                                                |
# -------------------------------------+--------------------------------------*/
- hosts:                               "{{ sap_sid| upper }}_DB"
  name:                                "DB HA Installation - SAP DB2"
  remote_user:                         "{{ orchestration_ansible_user }}"
  gather_facts:                        false
  vars_files:
    - vars/ansible-input-api.yaml             # API Input template with defaults

  # -----------------------------------+---------------------------------------8
  #
  # Build the list of tasks to be executed in order here.
  #
  # -----------------------------------+---------------------------------------8
  tasks:
    - name:                            "Backward Compatibility - Check required Database HA variables"
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"
      when:
        - db_high_availability is defined
        - database_high_availability is not defined

    - name:                            "DB2 HA - Install SAP DB2 on both nodes"
      become:                          true
      become_user:                     root
      block:
        - name:                        "DB2 HA - Setting the DB facts"
          ansible.builtin.set_fact:
            tier:                      db2             # Actions for DB2 Servers
            primary_instance_name :    "{{ ansible_play_hosts_all[0] }}"
            primary_instance_ip_db:    "{{ hostvars[ansible_play_hosts_all[0]]['ansible_eth0']['ipv4']['address'] }}"
            secondary_instance_name:   "{{ ansible_play_hosts_all[1] }}"
            secondary_instance_ip_db:  "{{ hostvars[ansible_play_hosts_all[1]]['ansible_eth0']['ipv4']['address'] }}"
            main_password:             "{{ hostvars.localhost.sap_password }}"
          tags:
            - always

        - name:                        "DB2 HA - Install Pacemaker (base)"
          when:
            - db_high_availability
            - node_tier == 'db2'
          block:

            - name:                    "DB2 HA - Setting the facts"
              ansible.builtin.set_fact:
                tier:                  ha
                main_password:             "{{ hostvars.localhost.sap_password }}"
                password_ha_db_cluster:    "{{ hostvars.localhost.db_cluster_password }}"
                primary_instance_name:     "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                secondary_instance_name:   "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                fencing_spn_client_id:     "{% if not use_msi_for_clusters %}{{ hostvars.localhost.sap_fencing_spn_client_id }}{% endif %}"
                fencing_spn_client_pwd:    "{% if not use_msi_for_clusters %}{{ hostvars.localhost.sap_fencing_spn_pwd }}{% endif %}"
                fencing_spn_tenant_id:     "{% if not use_msi_for_clusters %}{{ hostvars.localhost.sap_fencing_spn_tenant_id }}{% endif %}"

            - name:                    "DB2 HA - repository check"
              block:
                - name:                "DB2 HA - Ensure the correct repositories are set"
                  ansible.builtin.include_role:
                    name:              roles-os/1.3-repository
              tags:
                - 1.3-repository

            - name:                    "DB2 HA - package check"
              block:
                - name:                "DB2 HA - Ensure the correct packages are installed"
                  ansible.builtin.include_role:
                    name:              roles-os/1.4-packages
              tags:
                - 1.4-packages

            - name:                    "DB2 HA - set kernel parameters"
              block:
              # Setup the kernel parameters required for pacemaker cluster VMs
                - name:                "DB2 HA - Ensure the correct kernel parameters are set"
                  ansible.builtin.include_role:
                    name:              roles-os/1.9-kernelparameters
              tags:
                - 1.9-kernelparameters

            - name:                    "DB2 HA - services check"
              block:
                - name:                "DB2 HA - Ensure the needed services are enabled/disabled"
                  ansible.builtin.include_role:
                    name:              roles-os/1.16-services
              tags:
                - 1.16-services

            - name:                    "DB2 HA - accounts check"
              block:
                - name:                "DB2 HA - Ensure the accounts are present"
                  ansible.builtin.include_role:
                    name:                roles-os/1.11-accounts
              tags:
                - 1.11-accounts

            - name:                    "DB2 HA - Generic pacemaker Configuration"
              ansible.builtin.include_role:
                name:                  roles-os/1.17-generic-pacemaker                         # Configures the Pacemaker cluster with Azure fence agent
                apply:
                  tags:
                    - 1.17-generic-pacemaker
                  become:              true
                  become_user:         root
              tags:
                - 1.17-generic-pacemaker

            - name:                    "DB2 HA - Create pacemaker-install-done flag"
              delegate_to:             localhost
              become:                  false
              ansible.builtin.file:
                path:                  "{{ _workspace_directory }}/.progress/pacemaker-install-done"
                state:                 touch
                mode:                  0755

        - name:                        "DB2 HA - Install DB2 Pacemaker resources"
          block:
            - name:                    "DB2 HA - Pacemaker - Initialize facts"
              ansible.builtin.set_fact:
                tier:                            ha
                password_ha_db_cluster:          "{{ hostvars.localhost.db_cluster_password }}"
                main_password:                   "{{ hostvars.localhost.sap_password }}"
                primary_instance_name:           "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                primary_instance_ip_db:          "{{ hostvars[ansible_play_hosts_all[0]]['ansible_eth0']['ipv4']['address'] }}"
                secondary_instance_name:         "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                secondary_instance_ip_db:        "{{ hostvars[ansible_play_hosts_all[1]]['ansible_eth0']['ipv4']['address'] }}"
                # sap_hana_fencing_spn_client_id:  "{{ hostvars.localhost.sap_fencing_spn_client_id }}"
                # sap_hana_fencing_spn_client_pwd: "{{ hostvars.localhost.sap_fencing_spn_pwd }}"
                # sap_hana_fencing_spn_tenant_id:  "{{ hostvars.localhost.sap_fencing_spn_tenant_id }}"
              tags:
                - always

            - name:                    "DB2 HA - Pacemaker - Run the Pacemaker setup"
              ansible.builtin.include_role:
                name:                  roles-sap/5.7-db2-pacemaker
              tags:
                - 5.7-db2-pacemaker

      when:
        - database_high_availability
        - platform == 'DB2'

# # /*-------------------------------------------------------------------------8
# # |                                                                          |
# # |           Playbook for SQLServer DB   -  High Availability               |
# # |                                                                          |
# # +------------------------------------4------------------------------------*/

- hosts:                               "{{ sap_sid| upper }}_DB"
  name:                                "DB HA Installation - SAP SQLServer"
  remote_user:                         "{{ orchestration_ansible_user }}"
  gather_facts:                        true
  vars_files:
    - vars/ansible-input-api.yaml             # API Input template with defaults

  # -----------------------------------+---------------------------------------8
  #
  # Build the list of tasks to be executed in order here.
  #
  # -----------------------------------+---------------------------------------8
  tasks:
    - name:                            "Backward Compatibility - Check required Database HA variables"
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"
      when:
        - db_high_availability is defined
        - database_high_availability is not defined

    - name:                            "SQLServer HA - Install SQL4SAP on both nodes"
      when:
        - database_high_availability
        - platform | upper == 'SQLSERVER'
        - node_tier == 'sqlserver'
      become:                          true
      become_user:                     "{{ orchestration_ansible_user }}"
      block:
        - name:                        "SQLServer HA - Setting the DB facts"
          ansible.builtin.set_fact:
            primary_node:              "{{ ansible_play_hosts_all[0] }}"
            secondary_node:            "{{ ansible_play_hosts_all[1] }}"
            domain_user_password:      "{{ hostvars.localhost.winadm_password }}"
          tags:
            - always

        - name:                                "SQLServer HA - Create run flag directory"
          ansible.windows.win_file:
            path:                              '{{ sap_deployment_automation }}\{{ sap_sid | upper }}'
            state:                             directory

        - name:                                "SQLServer HA - check if SQL Server service is started"
          ansible.windows.win_service_info:
            name:                              MSSQLServer
          register:                            sql_service

        - name:                                "WIN-SQL: Print SQL Server service status"
          ansible.builtin.debug:
            msg:
              - "SQL Server Service status: {{ sql_service }}"

        - name:                        "SQLServer HA - Install cluster (base)"
          when:
            - sql_service.exists
            - db_high_availability
            - node_tier == 'sqlserver'
          block:
            - name:                    "SQLServer HA - Set 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:                    "SQLServer HA -  Set the server facts"
              ansible.builtin.set_fact:
                scs_server:            "{{ hostvars[scs_server_temp | first]['virtual_host'] }}"

            - name:                    "SQLServer HA - Setting the facts"
              ansible.builtin.set_fact:
                tier:                   ha
                primary_node:           "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                secondary_node:         "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                domain_user_password:   "{{ hostvars.localhost.winadm_password }}"
                sap_cluster_ip_address: "{{ database_cluster_ip }}"
                sql_svc_account:        "{% if hostvars.localhost.sqlsvc_account is defined %}{{ hostvars.localhost.sqlsvc_account }}{% else %}{{ win_sql_svc_account }}{% endif %}"
                sql_svc_password:       "{% if hostvars.localhost.sqlsvc_account_password is defined %}{{ hostvars.localhost.sqlsvc_account_password }}{% else %}{{ sql_svc_password | default('') }}{% endif %}"
                sql_agent_account:      "{% if hostvars.localhost.sqlagent_account is defined %}{{ hostvars.localhost.sqlagent_account }}{% else %}{{ win_sqlagent_svc_account }}{% endif %}"
                sql_agent_password:     "{% if hostvars.localhost.sqlagent_account_password is defined %}{{ hostvars.localhost.sqlagent_account_password }}{% else %}{{ sql_agent_password | default('') }}{% endif %}"

            - name:                           "SQL Server HA config: Base cluster tasks"
              ansible.builtin.include_role:
                name:                         roles-os/windows/1.17-generic-wincluster
              register:                       cluster_output
              vars:
                sap_cluster_ip_address:       "{{ database_cluster_ip }}"
                primary_node:                 "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                secondary_node:               "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                domain_user_password:         "{{ hostvars.localhost.winadm_password }}"
                svc_password:                 "{{ hostvars.localhost.svcadm_password }}"
                domain_service_account:       "{{ hostvars.localhost.adsvc_account }}"
                domain_service_password:      "{{ hostvars.localhost.adsvc_password }}"
                witness_storage_account_name: "{{ hostvars.localhost.witness_storage_account_name }}"
                witness_storage_account_key:  "{{ hostvars.localhost.witness_storage_account_key }}"
                win_sql_svc_account:          '{{ sql_svc_account }}'
                win_sql_svc_password:         '{{ sql_svc_password }}'
                win_sqlagent_svc_account:     '{{ sql_agent_account }}'
                win_sqlagent_svc_password:    '{{ sql_agent_password }}'

            - name:                           "SQL Server HA config: Always On tasks"
              ansible.builtin.include_role:
                name:                         roles-db/windows/4.4.0-mssql-install
              register:                       alwayson_output
              vars:
                config_tier:                  "sqlserverha"
                sap_cluster_ip_address:       "{{ database_cluster_ip }}"
                primary_node:                 "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                secondary_node:               "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                mssql_primary_node:           "{{ ansible_play_hosts_all[0] }}"         # Setting up Primary Instance Name
                mssql_secondary_node:         "{{ ansible_play_hosts_all[1] }}"         # Setting up Secondary Instance Name
                domain_user_password:         "{{ hostvars.localhost.winadm_password }}"
                svc_password:                 "{{ hostvars.localhost.svcadm_password }}"
                domain_service_account:       "{{ hostvars.localhost.adsvc_account }}"
                domain_service_password:      "{{ hostvars.localhost.adsvc_password }}"
                witness_storage_account_name: "{{ hostvars.localhost.witness_storage_account_name }}"
                witness_storage_account_key:  "{{ hostvars.localhost.witness_storage_account_key }}"
                win_sql_svc_account:          "{{ sql_svc_account }}"
                win_sql_svc_password:         "{{ sql_svc_password }}"
                win_sqlagent_svc_account:     "{{ sql_agent_account }}"
                win_sqlagent_svc_password:    "{{ sql_agent_password }}"
                scs_server_name:              "{{ custom_scs_virtual_hostname | default(scs_server, true) }}"
              tags:
                - 4.4.1.0-mssql-alwayson

            - name:                           "SQLServer HA - Create alwayson-config-done flag"
              ansible.windows.win_file:
                path:                         '{{ sap_deployment_automation }}\{{ sap_sid | upper }}\alwayson-config-done.txt'
                state:                        touch
                force:                        true

# # /*-------------------------------------------------------------------------8
# # |                                                                          |
# # |         Playbook for SQLServer DB   -  High Availability - END           |
# # |                                                                          |
# # +------------------------------------4------------------------------------*/

# If the DB HA is configured successfully, we need to create the db-ha-done file
- hosts:                               localhost
  name:                                "Log results"
  vars_files:
    - vars/ansible-input-api.yaml      # API Input template with defaults
  tasks:
    - name:                            "Backward Compatibility - Check required Database HA variables"
      ansible.builtin.set_fact:
        database_high_availability:    "{{ db_high_availability | default(false) }}"
      when:
        - db_high_availability is defined
        - database_high_availability is not defined

    - name:                            Ensure db-ha-done file exists
      ansible.builtin.file:
        path:                          "{{ _workspace_directory }}/.progress/db-ha-done"
        state:                         touch
        mode:                          0755
      when:                            database_high_availability

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