---
####################################################################
# Example : iSCSI Target Playbook
####################################################################
- name: ISCSI Target Module
  hosts: localhost
  gather_facts: false

  vars_files:
    - ../ansible_vault_vars/ansible_vault_storage_var.yml

  vars:
    # Common connection info for all tasks
    connection_info:
      address: "{{ storage_address }}"
      username: "{{ vault_storage_username }}"
      password: "{{ vault_storage_secret }}"

  tasks:
    ####################################################################
    # Task Name : Create iscsi target
    ####################################################################
    - name: Create iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          name: "iscsi-target-server-1"
          port: "CL4-C"
          iqn_initiators:
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254a"
              nick_name: "iscsi-target-server-1"
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254b"
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254c"
              nick_name: "iscsi-target-server-2"
          ldevs: [100, 200]
          chap_users:
            - chap_user_name: "user1"
              chap_secret: "TopSecretForMyChap1"
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Update iscsi target host mode and host mode options
    ####################################################################
    - name: Update iscsi target host mode and host mode options
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          name: "iscsi-target-server-1"
          port: "CL4-C"
          host_mode: "LINUX"
          host_mode_options: [54, 63]
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Add chap users to iscsi target
    ####################################################################
    - name: Add chap users to iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "add_chap_user"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          chap_users:
            - chap_user_name: "user1"
              chap_secret: "TopSecretForMyChap1"
            - chap_user_name: "user2"
              chap_secret: "TopSecretForMyChap2"
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Remove chap user from iscsi target
    ####################################################################
    - name: Remove chap user from iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "remove_chap_user"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          chap_users:
            - chap_user_name: "user2"
              chap_secret: "TopSecretForMyChap2"
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Add iqn initiators to iscsi target
    ####################################################################
    - name: Add iqn initiators to iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "add_iscsi_initiator"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          iqn_initiators:
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254b"
              nick_name: "iscsi-target-server-1"

      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Remove iqn initiators from iscsi target
    ####################################################################
    - name: Remove iqn initiators from iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "remove_iscsi_initiator"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          iqn_initiators:
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254b"
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Attach ldevs to iscsi target
    ####################################################################
    - name: Attach ldevs to iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "attach_ldev"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          ldevs: [300, 400]
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Detach ldevs from iscsi target
    ####################################################################
    - name: Detach ldevs from iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "detach_ldev"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          ldevs: [300, 400]
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Delete iscsi target
    ####################################################################
    - name: Delete iscsi target
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "absent"
        spec:
          name: "iscsi-target-server-1"
          port: "CL4-C"
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result
    ####################################################################
    # Task Name : Rename or unset nick name of a iqn initiator (existing iqn initiator)
    ####################################################################
    - name: Rename or unset nick name of a iqn initiator (existing iqn initiator)
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          state: "add_iscsi_initiator"
          name: "iscsi-target-server-1"
          port: "CL4-C"
          iqn_initiators:
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254b"
              nick_name: "new_nick_name"
            - iqn: "iqn.1993-08.org.debian.iscsi:01:107dc7e4254b"
              nick_name: "" # unset nick name

      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Release host reserve status of the LU mapped to all LU mapped path.
    ####################################################################
    - name: Release host reserve status of the LU mapped to all LU mapped path.
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          name: "iscsi-target-server-1"
          port: "CL4-C"
          should_release_host_reserve: true
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result

    ####################################################################
    # Task Name : Release host reserve status of the LU mapped to a specified LU mapped path.
    ####################################################################
    - name: Release host reserve status of the LU mapped to a specified LU mapped path.
      hitachivantara.vspone_block.vsp.hv_iscsi_target:
        connection_info: "{{ connection_info }}"
        state: "present"
        spec:
          name: "iscsi-target-server-1"
          port: "CL4-C"
          should_release_host_reserve: true
          lun: 100
      register: result

    - name: Debug the result variable
      ansible.builtin.debug:
        var: result
