- name: Check if all nodes are in STANDALONE state
  cisco.ise.personas_check_standalone:
    ip: "{{ item.ip }}"
    username: "{{ personas_deployment_ise_username }}"
    password: "{{ personas_deployment_ise_password }}"
    hostname: "{{ item.hostname }}"
  loop:
    # ISE Server 1
    - ip: "{{ personas_deployment_pan1_ip }}"
      hostname: "{{ personas_deployment_ise_base_hostname | lower }}-server-1"
    # ISE Server 2
    - ip: "{{ personas_deployment_pan2_ip }}"
      hostname: "{{ personas_deployment_ise_base_hostname | lower }}-server-2"

- name: Export trusted certificates into primary node
  cisco.ise.personas_export_certs:
    primary_ip: "{{ personas_deployment_pan1_ip }}"
    primary_username: "{{ personas_deployment_ise_username }}"
    primary_password: "{{ personas_deployment_ise_password }}"
    name: "{{ item.name }}"
    ip: "{{ item.ip }}"
    hostname: "{{ item.hostname }}"
    username: "{{ personas_deployment_ise_username }}"
    password: "{{ personas_deployment_ise_password }}"
  loop:
    - name: ISE Server 2
      ip: "{{ personas_deployment_pan2_ip }}"
      hostname: "{{ personas_deployment_ise_base_hostname | lower }}-server-2"

- name: Promote primary node
  cisco.ise.personas_promote_primary:
    ip: "{{ personas_deployment_pan1_ip }}"
    username: "{{ personas_deployment_ise_username }}"
    password: "{{ personas_deployment_ise_password }}"

- name: Register the secondary PAN to the cluster
  cisco.ise.personas_register_node:
    primary_ip: "{{ personas_deployment_pan1_ip }}"
    primary_username: "{{ personas_deployment_ise_username }}"
    primary_password: "{{ personas_deployment_ise_password }}"
    fqdn: "{{ item.fqdn }}"
    username: "{{ personas_deployment_ise_username }}"
    password: "{{ personas_deployment_ise_password }}"
    roles: "{{ item.roles }}"
    services: "{{ item.services }}"
  loop:
    # ISE Server 2
    - fqdn: "{{ personas_deployment_ise_base_hostname | lower }}-server-2.{{ personas_deployment_ise_domain }}"
      roles:
        - SecondaryAdmin
        - SecondaryMonitoring
      services:
        - Session
        - Profiler
