| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: intersight_fibre_channel_zone_policy |
| 16 |
short_description: Manage Fibre Channel Zone Policies for Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete Fibre Channel Zone Policies on Cisco Intersight. |
| 19 |
- Manage FC target members (zones) within FC zone policies. |
| 20 |
- FC Zone policies define zoning configurations for Fibre Channel networks. |
| 21 |
- Supports SIST (Single Initiator Single Target), SIMT (Single Initiator Multiple Target), and None zoning types. |
| 22 |
- When zoning type is None, targets cannot be added and existing targets will be disabled. |
| 23 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/fabric/FcZonePolicies/get/). |
| 24 |
extends_documentation_fragment: intersight |
| 25 |
options: |
| 26 |
state: |
| 27 |
description: |
| 28 |
- If C(present), will verify the resource is present and will create if needed. |
| 29 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 30 |
type: str |
| 31 |
choices: [present, absent] |
| 32 |
default: present |
| 33 |
organization: |
| 34 |
description: |
| 35 |
- The name of the Organization this resource is assigned to. |
| 36 |
- Policies created within a Custom Organization are applicable only to devices in the same Organization. |
| 37 |
- Use 'default' for the default organization. |
| 38 |
type: str |
| 39 |
default: default |
| 40 |
name: |
| 41 |
description: |
| 42 |
- The name assigned to the Fibre Channel Zone Policy. |
| 43 |
- Must be unique within the organization. |
| 44 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
description: |
| 48 |
description: |
| 49 |
- The user-defined description for the Fibre Channel Zone Policy. |
| 50 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 51 |
type: str |
| 52 |
aliases: [descr] |
| 53 |
tags: |
| 54 |
description: |
| 55 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
fc_target_zoning_type: |
| 59 |
description: |
| 60 |
- Type of FC zoning configuration. |
| 61 |
- C(SIST) - Single Initiator Single Target zoning. |
| 62 |
- C(SIMT) - Single Initiator Multiple Target zoning. |
| 63 |
- C(None) - No zoning. When None is selected, targets cannot be added and existing targets will be disabled. |
| 64 |
type: str |
| 65 |
choices: [SIST, SIMT, None] |
| 66 |
default: None |
| 67 |
fc_target_members: |
| 68 |
description: |
| 69 |
- List of FC target members (zones) to be created within the FC zone policy. |
| 70 |
- Each target defines a WWPN that is a member of the FC zone. |
| 71 |
- Leave empty when fc_target_zoning_type is None. |
| 72 |
type: list |
| 73 |
elements: dict |
| 74 |
suboptions: |
| 75 |
name: |
| 76 |
description: |
| 77 |
- Name identifier for the FC target member. |
| 78 |
- Must be unique within the policy. |
| 79 |
type: str |
| 80 |
required: true |
| 81 |
wwpn: |
| 82 |
description: |
| 83 |
- World Wide Port Name (WWPN) that is a member of the FC zone. |
| 84 |
- Format should be colon-separated hex values (e.g., 21:00:00:e0:8b:05:05:04). |
| 85 |
- Must be a valid WWPN format. |
| 86 |
type: str |
| 87 |
required: true |
| 88 |
switch_id: |
| 89 |
description: |
| 90 |
- Unique identifier for the Fabric object. |
| 91 |
- Specifies which fabric interconnect the target is associated with. |
| 92 |
type: str |
| 93 |
choices: [A, B] |
| 94 |
default: A |
| 95 |
vsan_id: |
| 96 |
description: |
| 97 |
- VSAN ID with scope defined as Storage in the VSAN policy. |
| 98 |
- Must be between 1 and 4093. |
| 99 |
type: int |
| 100 |
required: true |
| 101 |
author: |
| 102 |
- Ron Gershburg (@rgershbu) |
| 103 |
''' |
| 104 |
|
| 105 |
EXAMPLES = r''' |
| 106 |
- name: Create FC Zone Policy with SIMT zoning and multiple targets |
| 107 |
cisco.intersight.intersight_fibre_channel_zone_policy: |
| 108 |
api_private_key: "{{ api_private_key }}" |
| 109 |
api_key_id: "{{ api_key_id }}" |
| 110 |
organization: "default" |
| 111 |
name: "fc-zone-policy-simt" |
| 112 |
description: "FC Zone policy with SIMT zoning" |
| 113 |
fc_target_zoning_type: SIMT |
| 114 |
fc_target_members: |
| 115 |
- name: "target1" |
| 116 |
wwpn: "21:00:00:e0:8b:05:05:04" |
| 117 |
switch_id: A |
| 118 |
vsan_id: 100 |
| 119 |
- name: "target2" |
| 120 |
wwpn: "21:00:00:e0:8b:05:05:03" |
| 121 |
switch_id: B |
| 122 |
vsan_id: 100 |
| 123 |
- name: "target3" |
| 124 |
wwpn: "21:00:00:e0:8b:05:05:09" |
| 125 |
switch_id: A |
| 126 |
vsan_id: 200 |
| 127 |
tags: |
| 128 |
- Key: Environment |
| 129 |
Value: Production |
| 130 |
state: present |
| 131 |
|
| 132 |
- name: Create FC Zone Policy with SIST zoning |
| 133 |
cisco.intersight.intersight_fibre_channel_zone_policy: |
| 134 |
api_private_key: "{{ api_private_key }}" |
| 135 |
api_key_id: "{{ api_key_id }}" |
| 136 |
organization: "default" |
| 137 |
name: "fc-zone-policy-sist" |
| 138 |
description: "FC Zone policy with SIST zoning" |
| 139 |
fc_target_zoning_type: SIST |
| 140 |
fc_target_members: |
| 141 |
- name: "single-target" |
| 142 |
wwpn: "21:00:00:e0:8b:05:05:01" |
| 143 |
switch_id: A |
| 144 |
vsan_id: 100 |
| 145 |
state: present |
| 146 |
|
| 147 |
- name: Create FC Zone Policy with no zoning (None type) |
| 148 |
cisco.intersight.intersight_fibre_channel_zone_policy: |
| 149 |
api_private_key: "{{ api_private_key }}" |
| 150 |
api_key_id: "{{ api_key_id }}" |
| 151 |
organization: "default" |
| 152 |
name: "fc-zone-policy-none" |
| 153 |
description: "FC Zone policy with no zoning" |
| 154 |
fc_target_zoning_type: None |
| 155 |
state: present |
| 156 |
|
| 157 |
- name: Update FC Zone Policy - add new target |
| 158 |
cisco.intersight.intersight_fibre_channel_zone_policy: |
| 159 |
api_private_key: "{{ api_private_key }}" |
| 160 |
api_key_id: "{{ api_key_id }}" |
| 161 |
organization: "default" |
| 162 |
name: "fc-zone-policy-simt" |
| 163 |
description: "Updated FC Zone policy" |
| 164 |
fc_target_zoning_type: SIMT |
| 165 |
fc_target_members: |
| 166 |
- name: "target1" |
| 167 |
wwpn: "21:00:00:e0:8b:05:05:04" |
| 168 |
switch_id: A |
| 169 |
vsan_id: 100 |
| 170 |
- name: "target2" |
| 171 |
wwpn: "21:00:00:e0:8b:05:05:03" |
| 172 |
switch_id: B |
| 173 |
vsan_id: 100 |
| 174 |
- name: "target3" |
| 175 |
wwpn: "21:00:00:e0:8b:05:05:09" |
| 176 |
switch_id: A |
| 177 |
vsan_id: 200 |
| 178 |
- name: "target4" |
| 179 |
wwpn: "21:00:00:e0:8b:05:05:10" |
| 180 |
switch_id: B |
| 181 |
vsan_id: 200 |
| 182 |
state: present |
| 183 |
|
| 184 |
- name: Update FC Zone Policy - remove a target (only specify remaining targets) |
| 185 |
cisco.intersight.intersight_fibre_channel_zone_policy: |
| 186 |
api_private_key: "{{ api_private_key }}" |
| 187 |
api_key_id: "{{ api_key_id }}" |
| 188 |
organization: "default" |
| 189 |
name: "fc-zone-policy-simt" |
| 190 |
fc_target_zoning_type: SIMT |
| 191 |
fc_target_members: |
| 192 |
- name: "target1" |
| 193 |
wwpn: "21:00:00:e0:8b:05:05:04" |
| 194 |
switch_id: A |
| 195 |
vsan_id: 100 |
| 196 |
- name: "target3" |
| 197 |
wwpn: "21:00:00:e0:8b:05:05:09" |
| 198 |
switch_id: A |
| 199 |
vsan_id: 200 |
| 200 |
state: present |
| 201 |
|
| 202 |
- name: Delete FC Zone Policy |
| 203 |
cisco.intersight.intersight_fibre_channel_zone_policy: |
| 204 |
api_private_key: "{{ api_private_key }}" |
| 205 |
api_key_id: "{{ api_key_id }}" |
| 206 |
organization: "default" |
| 207 |
name: "fc-zone-policy-simt" |
| 208 |
state: absent |
| 209 |
''' |
| 210 |
|
| 211 |
RETURN = r''' |
| 212 |
api_response: |
| 213 |
description: The API response output returned by the specified resource. |
| 214 |
returned: always |
| 215 |
type: dict |
| 216 |
sample: |
| 217 |
"api_response": { |
| 218 |
"Name": "fc-zone-policy-simt", |
| 219 |
"ObjectType": "fabric.FcZonePolicy", |
| 220 |
"Moid": "1234567890abcdef12345678", |
| 221 |
"Description": "FC Zone policy with SIMT zoning", |
| 222 |
"FcTargetZoningType": "SIMT", |
| 223 |
"FcTargetMembers": [ |
| 224 |
{ |
| 225 |
"Name": "target1", |
| 226 |
"Wwpn": "21:00:00:e0:8b:05:05:04", |
| 227 |
"SwitchId": "A", |
| 228 |
"VsanId": 100 |
| 229 |
}, |
| 230 |
{ |
| 231 |
"Name": "target2", |
| 232 |
"Wwpn": "21:00:00:e0:8b:05:05:03", |
| 233 |
"SwitchId": "B", |
| 234 |
"VsanId": 100 |
| 235 |
} |
| 236 |
], |
| 237 |
"Organization": { |
| 238 |
"Moid": "abcdef1234567890abcdef12", |
| 239 |
"ObjectType": "organization.Organization" |
| 240 |
}, |
| 241 |
"Tags": [ |
| 242 |
{ |
| 243 |
"Key": "Environment", |
| 244 |
"Value": "Production" |
| 245 |
} |
| 246 |
] |
| 247 |
} |
| 248 |
''' |
| 249 |
|
| 250 |
|
| 251 |
from ansible.module_utils.basic import AnsibleModule |
| 252 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 253 |
import re |
| 254 |
|
| 255 |
|
| 256 |
def validate_wwpn(wwpn): |
| 257 |
""" |
| 258 |
Validate WWPN format. |
| 259 |
""" |
| 260 |
wwpn_pattern = r'^([0-9a-fA-F]{2}:){7}[0-9a-fA-F]{2}$' |
| 261 |
if not re.match(wwpn_pattern, wwpn): |
| 262 |
raise ValueError(f"Invalid WWPN format '{wwpn}'. Expected format: XX:XX:XX:XX:XX:XX:XX:XX (hex values)") |
| 263 |
return True |
| 264 |
|
| 265 |
|
| 266 |
def validate_vsan_id(vsan_id): |
| 267 |
""" |
| 268 |
Validate VSAN ID is within acceptable range (1-4093). |
| 269 |
""" |
| 270 |
if vsan_id < 1 or vsan_id > 4093: |
| 271 |
raise ValueError(f"VSAN ID {vsan_id} is out of valid range (1-4093)") |
| 272 |
return True |
| 273 |
|
| 274 |
|
| 275 |
def validate_parameters(module): |
| 276 |
""" |
| 277 |
Validate module parameters for FC Zone policy configuration. |
| 278 |
""" |
| 279 |
if module.params['state'] != 'present': |
| 280 |
return |
| 281 |
fc_target_zoning_type = module.params.get('fc_target_zoning_type') |
| 282 |
fc_target_members = module.params.get('fc_target_members', []) |
| 283 |
# Validate that if zoning type is None, no targets should be provided |
| 284 |
if fc_target_zoning_type == 'None' and fc_target_members: |
| 285 |
module.fail_json(msg="When fc_target_zoning_type is 'None', fc_target_members should be empty. Targets cannot be added when zoning type is None.") |
| 286 |
# Validate fc_target_members if provided |
| 287 |
if fc_target_members: |
| 288 |
for target in fc_target_members: |
| 289 |
# Validate WWPN format |
| 290 |
wwpn = target.get('wwpn') |
| 291 |
if wwpn: |
| 292 |
try: |
| 293 |
validate_wwpn(wwpn) |
| 294 |
except ValueError as e: |
| 295 |
module.fail_json(msg=str(e)) |
| 296 |
# Validate VSAN ID range |
| 297 |
vsan_id = target.get('vsan_id') |
| 298 |
if vsan_id: |
| 299 |
try: |
| 300 |
validate_vsan_id(vsan_id) |
| 301 |
except ValueError as e: |
| 302 |
module.fail_json(msg=str(e)) |
| 303 |
|
| 304 |
|
| 305 |
def main(): |
| 306 |
argument_spec = intersight_argument_spec.copy() |
| 307 |
argument_spec.update( |
| 308 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 309 |
organization=dict(type='str', default='default'), |
| 310 |
name=dict(type='str', required=True), |
| 311 |
description=dict(type='str', aliases=['descr']), |
| 312 |
tags=dict(type='list', elements='dict'), |
| 313 |
fc_target_zoning_type=dict(type='str', choices=['SIST', 'SIMT', 'None'], default='None'), |
| 314 |
fc_target_members=dict(type='list', elements='dict', options=dict( |
| 315 |
name=dict(type='str', required=True), |
| 316 |
wwpn=dict(type='str', required=True), |
| 317 |
switch_id=dict(type='str', choices=['A', 'B'], default='A'), |
| 318 |
vsan_id=dict(type='int', required=True) |
| 319 |
)) |
| 320 |
) |
| 321 |
module = AnsibleModule( |
| 322 |
argument_spec, |
| 323 |
supports_check_mode=True, |
| 324 |
) |
| 325 |
intersight = IntersightModule(module) |
| 326 |
|
| 327 |
intersight.result['api_response'] = {} |
| 328 |
intersight.result['trace_id'] = '' |
| 329 |
|
| 330 |
# Validate module parameters |
| 331 |
validate_parameters(module) |
| 332 |
|
| 333 |
# Resource path used to configure policy |
| 334 |
resource_path = '/fabric/FcZonePolicies' |
| 335 |
|
| 336 |
# Define API body used in compares or create |
| 337 |
intersight.api_body = { |
| 338 |
'Organization': { |
| 339 |
'Name': module.params['organization'], |
| 340 |
}, |
| 341 |
'Name': module.params['name'] |
| 342 |
} |
| 343 |
|
| 344 |
if module.params['state'] == 'present': |
| 345 |
intersight.api_body['FcTargetZoningType'] = module.params['fc_target_zoning_type'] |
| 346 |
# Build FcTargetMembers list |
| 347 |
fc_target_members = [] |
| 348 |
if module.params.get('fc_target_members'): |
| 349 |
for target in module.params['fc_target_members']: |
| 350 |
fc_target_members.append({ |
| 351 |
'Name': target['name'], |
| 352 |
'Wwpn': target['wwpn'], |
| 353 |
'SwitchId': target['switch_id'], |
| 354 |
'VsanId': target['vsan_id'] |
| 355 |
}) |
| 356 |
intersight.api_body['FcTargetMembers'] = fc_target_members |
| 357 |
intersight.set_tags_and_description() |
| 358 |
|
| 359 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 360 |
|
| 361 |
module.exit_json(**intersight.result) |
| 362 |
|
| 363 |
|
| 364 |
if __name__ == '__main__': |
| 365 |
main() |