---
- name: Using IBM Spectrum Virtualize collection to create rc consistency group
  hosts: localhost
  collections:
    - ibm.spectrum_virtualize
  gather_facts: no
  vars: 
    - auxcluster: x.x.x.x
    - ausername: ausername 
    - apassword: apassword
    - clustername: clustername
    - username: username
    - password: password
    - cgname: Group_cg11
    - remotecluster: Cluster_x.x.x.x
    - masterpool: site1pool1
    - mastervol: master
    - relname: scopy5
    - auxvol: auxvol
  connection: local
  tasks:
    - name: Fetch authorization token for aux
      register: auth
      ibm_svc_auth:
        clustername: "{{auxcluster}}"
        username: "{{ausername}}"
        password: "{{apassword}}"
    - name: create target volume
      ibm_svc_vdisk:
        clustername: "{{ auxcluster }}"
        token: "{{auth.token}}"
        mdiskgrp: "{{auxpool}}"
        name: "{{auxvol}}"
        size: 10
        unit: "gb"
        state: present
    - name: Fetch authorization token for master
      register: results
      ibm_svc_auth:
        clustername: "{{clustername}}"
        username: "{{username}}"
        password: "{{password}}"
    - name: create remote copy cg
      ibm_svc_manage_replicationgroup:
        name: "{{cgname}}"
        clustername: "{{clustername}}"
        token: "{{results.token}}"
        state: present
        remotecluster: "{{remotecluster}}"
    - name: Create source volume
      ibm_svc_vdisk:
        clustername: "{{ clustername }}"
        token: "{{results.token}}"
        mdiskgrp: "{{masterpool}}"
        name: "{{mastervol}}"
        size: 1
        unit: "gb"
        state: present
    - name: create MM remote copy
      ibm_svc_manage_replication:
        name: "{{relname}}"
        clustername: "{{ clustername }}"
        token: "{{results.token}}"
        state: present
        remotecluster: "{{remotecluster}}"
        master: "{{mastervol}}"
        aux: "{{auxvol}}"
        copytype: metro
        sync: true
        consistgrp: "{{cgname}}"
    - name: remove the remote copy from CG
      ibm_svc_manage_replication:
        name: "{{relname}}"
        clustername: "{{ clustername }}"
        token: "{{results.token}}"
        state: present
        remotecluster: "{{remotecluster}}"
        master: "{{mastervol}}"
        aux: "{{auxvol}}"
        copytype: metro
        noconsistgrp: true
    - name: Convert MM to GM
      ibm_svc_manage_replication:
        name: "{{relname}}"
        clustername: "{{ clustername }}"
        token: "{{results.token}}"
        state: present
        remotecluster: "{{remotecluster}}"
        master: "{{mastervol}}"
        aux: "{{auxvol}}"
        copytype: global
    - name: Convert GM to GMCV
      ibm_svc_manage_replication:
        name: "{{relname}}"
        clustername: "{{clustername}}"
        token: "{{results.token}}"
        state: present
        remotecluster: "{{remotecluster}}"
        master: "{{mastervol}}"
        aux: "{{auxvol}}"
        copytype: GMCV
        consistgrp: "{{cgname}}"
    - name: Create/attach master change volume
      ibm_svc_manage_cv:
        clustername: "{{ clustername }}"
        token: "{{results.token}}"
        state: present
        rname: "{{relname}}"
        cvname: "{{ mastervolcv }}"
        basevolume: "{{ mastervol }}"
    - name: Create/attach aux change volume
      ibm_svc_manage_cv:
        clustername: "{{ auxcluster }}"
        token: "{{auth.token}}"
        state: present
        rname: "{{relname}}"
        cvname: "{{ auxvolcv }}"
        basevolume: "{{ auxvol }}"
        ismaster: false
