---
- name: Using the IBM Storage Virtualize collection For PBR configuration
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    ha_partition_name: "{{ clusters[0].partition_name | default(ptn_dr_0) }}"
    dr_partition_name: "{{ clusters[1].partition_name | default(ptn_dr_0) }}"
  vars_files:
    - inventory.ini
  tasks:

    - name: Get the pool info
      register: pool_results
      ibm.storage_virtualize.ibm_svc_info:
        clustername: "{{ clusters[0].clustername }}"
        username: "{{ clusters[0].username }}"
        password: "{{ clusters[0].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        gather_subset: "pool"
        objectname: "{{ clusters[0].pool_name }}"

    - name: Set replicationpoollinkuid on DR site
      ibm.storage_virtualize.ibm_svc_mdiskgrp:
        clustername: "{{ clusters[1].clustername }}"
        username: "{{ clusters[1].username }}"
        password: "{{ clusters[1].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        name: "{{ clusters[1].pool_name }}"
        state: "present"
        replicationpoollinkuid: "{{ pool_results.Pool.replication_pool_link_uid }}"
        replication_partner_clusterid: "{{ clusters[0].name }}"

    - name: Create Storage Partition in DR site
      ibm.storage_virtualize.ibm_sv_manage_storage_partition:
        clustername: "{{ clusters[1].clustername }}"
        username: "{{ clusters[1].username }}"
        password: "{{ clusters[1].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        name: "{{ dr_partition_name }}"
        state: "present"

    - name: Get dr partition info
      register: dr_partition_results
      ibm.storage_virtualize.ibm_svc_info:
        clustername: "{{ clusters[1].clustername }}"
        username: "{{ clusters[1].username }}"
        password: "{{ clusters[1].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        gather_subset: "partition"
        objectname: "{{ dr_partition_name }}"

    - name: Add dr-link to HA1 Storage Partition
      ibm.storage_virtualize.ibm_sv_manage_storage_partition:
        clustername: "{{ clusters[0].clustername }}"
        username: "{{ clusters[0].username }}"
        password: "{{ clusters[0].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        name: "{{ ha_partition_name }}"
        state: "present"
        drlink_partition_uuid: "{{ dr_partition_results.Partition.uuid }}"
        remotesystem: "{{ clusters[1].name }}"

    - name: Create async-dr replication policy in HA1 stoarge
      ibm.storage_virtualize.ibm_sv_manage_replication_policy:
        clustername: "{{ clusters[0].clustername }}"
        username: "{{ clusters[0].username }}"
        password: "{{ clusters[0].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        name: "{{ dr_policy_name }}"
        topology: async-dr
        partition: "{{ ha_partition_name }}"
        rpoalert: 60
        state: present

    - name: Add dr replicationpolicy to existing volumegroups.
      ibm.storage_virtualize.ibm_svc_manage_volumegroup:
        clustername: "{{ clusters[0].clustername }}"
        username: "{{ clusters[0].username }}"
        password: "{{ clusters[0].password }}"
        log_path: "{{ log_path | default('/tmp/ansiblePB.debug') }}"
        name: "{{ item }}"
        state: "present"
        replicationpolicy: "{{ dr_policy_name }}"
      loop: "{{ volume_group_name }}"
