| 1 | ################################################### |
| 2 | # Play 1: Pre-flight - Validate Switch Connectivity |
| 3 | ################################################### |
| 4 | - name: Pre-flight - Validate switch connectivity and credentials |
| 5 | hosts: switches |
| 6 | gather_facts: false |
| 7 | connection: network_cli |
| 8 | any_errors_fatal: true |
| 9 | serial: 1 |
| 10 | vars: |
| 11 | ansible_network_os: cisco.nxos.nxos |
| 12 | ansible_become: true |
| 13 | ansible_become_method: enable |
| 14 | tasks: |
| 15 | |
| 16 | - name: Add switch SSH key if missing |
| 17 | ansible.builtin.shell: | |
| 18 | ssh-keygen -F {{ ansible_host }} >/dev/null || |
| 19 | ssh-keyscan -H {{ ansible_host }} >> ~/.ssh/known_hosts |
| 20 | delegate_to: localhost |
| 21 | changed_when: false |
| 22 | |
| 23 | - name: Validate switch connectivity and gather facts |
| 24 | block: |
| 25 | - name: Collect NX-OS facts |
| 26 | cisco.nxos.nxos_facts: |
| 27 | gather_subset: |
| 28 | - min |
| 29 | |
| 30 | - name: Report switch validation result |
| 31 | ansible.builtin.debug: |
| 32 | msg: >- |
| 33 | [{{ inventory_hostname }}] ({{ ansible_host }}) - |
| 34 | Switch reachable and credentials valid. |
| 35 | Hostname: {{ ansible_net_hostname }} |
| 36 | NX-OS Version: {{ ansible_net_version }} |
| 37 | |
| 38 | rescue: |
| 39 | - name: Report validation failure |
| 40 | ansible.builtin.fail: |
| 41 | msg: | |
| 42 | ============================================================ |
| 43 | PRE-FLIGHT VALIDATION FAILED: {{ inventory_hostname }} |
| 44 | ============================================================ |
| 45 | Host : {{ ansible_host }} |
| 46 | User : {{ ansible_user }} |
| 47 | |
| 48 | Possible causes: |
| 49 | - Incorrect username or password in inventory |
| 50 | - Switch is unreachable at {{ ansible_host }} |
| 51 | |
| 52 | Fix: Check credentials in your inventory file for [switches] |
| 53 | ============================================================ |
| 54 | |
| 55 | ################################################### |
| 56 | # Main: Cisco SAN Zoning - Cluster Data Collection and Zone Sync |
| 57 | ################################################### |
| 58 | - name: Cisco SAN Zoning - Cluster Data Collection |
| 59 | hosts: clusters |
| 60 | gather_facts: false |
| 61 | connection: local |
| 62 | tasks: |
| 63 | |
| 64 | ################################################### |
| 65 | # Gather Data from FlashSystem Cluster |
| 66 | ################################################## |
| 67 | |
| 68 | - name: Fetch authorization token for cluster |
| 69 | register: cluster_auth_info |
| 70 | no_log: true |
| 71 | ibm.storage_virtualize.ibm_svc_auth: |
| 72 | clustername: "{{ ansible_host }}" |
| 73 | username: "{{ ansible_user }}" |
| 74 | password: "{{ ansible_password }}" |
| 75 | |
| 76 | - name: Fetch Host Zone, FC Port and Target Port data |
| 77 | register: cluster_info |
| 78 | ibm.storage_virtualize.ibm_svc_info: |
| 79 | clustername: "{{ ansible_host }}" |
| 80 | token: "{{ cluster_auth_info.token }}" |
| 81 | command_list: [lshostzone, lsportfc, lstargetportfc, lssystem] |
| 82 | objectname: all |
| 83 | |
| 84 | # =============================== |
| 85 | # Sample CLI Output for Reference |
| 86 | # =============================== |
| 87 | # |
| 88 | # lshostzone CLI output:- |
| 89 | # +----+---------+------------+------------------+------------------+------------+--------------+------------+------------------+--------------------+--- |
| 90 | # | id | host_id | host_name | host_wwpn | host_fabric | portset_id | portset_name | io_port_id | ioport_fabric | recommended_config | |
| 91 | # +----+---------+------------+------------------+------------------+------------+--------------+------------+------------------+--------------------+--- |
| 92 | # | 0 | 0 | host0 | 30000020FF0A1B01 | 20020010AABBCC01 | 4 | ps-1 | 6 | 20020010AABBCC01 | zone_not_needed | |
| 93 | # | 1 | 0 | host0 | 30000020FF0A1B01 | 20020010AABBCC01 | 4 | ps-1 | 7 | 20020010AABBCC01 | zone_needed | |
| 94 | # | 2 | 1 | host1 | 30000020FF0A1B02 | 20020010AABBCC01 | 4 | ps-1 | 6 | 20020010AABBCC01 | zone_needed | |
| 95 | # | 3 | 1 | host1 | 30000020FF0A1B02 | 20020010AABBCC01 | 4 | ps-1 | 7 | 20020010AABBCC01 | zone_not_needed | |
| 96 | # +----+---------+------------+------------------+------------------+------------+--------------+------------+------------------+--------------------+--- |
| 97 | # ---+-----------------+-------------+---------------------------+ |
| 98 | # | applied_config | zone_status | disruptive_change_pending | |
| 99 | # ---+-----------------+-------------+---------------------------+ |
| 100 | # | zone_needed | not_zoned | yes | |
| 101 | # | zone_needed | not_zoned | no | |
| 102 | # | zone_needed | not_zoned | no | |
| 103 | # | zone_not_needed | not_zoned | no | |
| 104 | # ---+-----------------+-------------+---------------------------+ |
| 105 | |
| 106 | |
| 107 | # lsportfc CLI output:- |
| 108 | # +----+---------------+---------+------+------------+---------+-----------+------------------+---------+--------+------------------+------+--------+--- |
| 109 | # | id | fc_io_port_id | port_id | type | port_speed | node_id | node_name | WWPN | nportid | status | switch_WWPN | fpma | vlanid | |
| 110 | # +----+---------------+---------+------+------------+---------+-----------+------------------+---------+--------+------------------+------+--------+--- |
| 111 | # | 5 | 6 | 6 | fc | 32Gb | 1 | node1 | 600507700C32AB10 | 4B022D | active | 20020010AABBCC01 | N/A | N/A | |
| 112 | # | 6 | 7 | 7 | fc | 32Gb | 1 | node1 | 600507700C33AB13 | 4B022A | active | 20020010AABBCC01 | N/A | N/A | |
| 113 | # +----+---------------+---------+------+------------+---------+-----------+------------------+---------+--------+------------------+------+--------+--- |
| 114 | # ---+---------+------------+---------------+------------------+-----------------+------------------+-----------------------------+----------------+ |
| 115 | # | fcf_MAC | attachment | cluster_use | adapter_location | adapter_port_id | fabric_WWN | fabric_auto_zone_capability | port_max_speed | |
| 116 | # ---+---------+------------+---------------+------------------+-----------------+------------------+-----------------------------+----------------+ |
| 117 | # | N/A | switch | local_partner | 3 | 2 | 200100083131C0B1 | incompatible_fabric | 32Gb | |
| 118 | # | N/A | switch | local_partner | 3 | 3 | 200100083131C0B1 | incompatible_fabric | 32Gb | |
| 119 | # ---+---------+------------+---------------+------------------+-----------------+------------------+-----------------------------+----------------+ |
| 120 | |
| 121 | |
| 122 | # lstargetportfc CLI output:- |
| 123 | # +----+------------------+------------------+---------+----------------+-----------------+---------+-------------------+-------------+----------+--- |
| 124 | # | id | WWPN | WWNN | port_id | owning_node_id | current_node_id | nportid | host_io_permitted | virtualized | protocol | |
| 125 | # +----+------------------+------------------+---------+----------------+-----------------+---------+-------------------+-------------+----------+--- |
| 126 | # | 16 | 600507700C32AB10 | 600507700C00AB10 | 6 | 1 | 1 | 4B022D | no | no | scsi | |
| 127 | # | 17 | 600507700C36AB11 | 600507700C00AB10 | 6 | 1 | 1 | 4B022E | yes | yes | scsi | |
| 128 | # | 18 | 600507700C3AAB12 | 600507700C00AB10 | 6 | 1 | 1 | 4B022F | yes | yes | nvme | |
| 129 | # | 19 | 600507700C33AB13 | 600507700C00AB10 | 7 | 1 | 1 | 4B022A | no | no | scsi | |
| 130 | # | 20 | 600507700C37AB14 | 600507700C00AB10 | 7 | 1 | 1 | 4B022B | yes | yes | scsi | |
| 131 | # | 21 | 600507700C3BAB15 | 600507700C00AB10 | 7 | 1 | 1 | 4B022C | yes | yes | nvme | |
| 132 | # | 22 | 600507700C34AB16 | 600507700C00AB10 | 8 | 1 | 1 | 010D00 | no | no | scsi | |
| 133 | # | 23 | 600507700C38AB17 | 600507700C00AB10 | 8 | 1 | 1 | 010D01 | yes | yes | scsi | |
| 134 | # | 24 | 600507700C3CAB18 | 600507700C00AB10 | 8 | 1 | 1 | 010D02 | yes | yes | nvme | |
| 135 | # +----+------------------+------------------+---------+----------------+-----------------+---------+-------------------+-------------+----------+--- |
| 136 | # ---+--------------------+---------------+------------+--------------+ |
| 137 | # | fc_ioport_id_count | portset_count | host_count | active_login | |
| 138 | # ---+--------------------+---------------+------------+--------------+ |
| 139 | # | 6 | 0 | 0 | 0 | |
| 140 | # | 6 | 2 | 2 | 1 | |
| 141 | # | 6 | 2 | 0 | 0 | |
| 142 | # | 7 | 0 | 0 | 0 | |
| 143 | # | 7 | 2 | 2 | 2 | |
| 144 | # | 7 | 2 | 0 | 0 | |
| 145 | # | 8 | 0 | 0 | 0 | |
| 146 | # | 8 | 1 | 0 | 0 | |
| 147 | # | 8 | 1 | 0 | 0 | |
| 148 | # ---+--------------------+---------------+------------+--------------+ |
| 149 | |
| 150 | - name: Initiate zone synchronization for {{ inventory_hostname }} |
| 151 | ansible.builtin.command: > |
| 152 | ansible-playbook cisco_zone_sync.yaml |
| 153 | -i {{ ansible_inventory_sources[0] }} |
| 154 | --extra-vars '{"cluster_info": {{ cluster_info | to_json }} }' |
| 155 | -e "cluster_user={{ inventory_hostname }}" |
| 156 | -vvv |
| 157 | changed_when: false |
| 158 | |
| 159 | - name: Report zone synchronization complete |
| 160 | ansible.builtin.debug: |
| 161 | msg: >- |
| 162 | [{{ inventory_hostname }}] Zone synchronization completed successfully. |