| 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_vsan_policy |
| 16 |
short_description: Manage VSAN Policies and VSANs for Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete VSAN (Virtual Storage Area Network) Policies on Cisco Intersight. |
| 19 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/fabric/FcNetworkPolicy/get/). |
| 20 |
extends_documentation_fragment: intersight |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- If C(present), will verify the resource is present and will create if needed. |
| 25 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 26 |
type: str |
| 27 |
choices: [present, absent] |
| 28 |
default: present |
| 29 |
organization: |
| 30 |
description: |
| 31 |
- The name of the Organization this resource is assigned to. |
| 32 |
- Policies created within a Custom Organization are applicable only to devices in the same Organization. |
| 33 |
- Use 'default' for the default organization. |
| 34 |
type: str |
| 35 |
default: default |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name assigned to the VSAN Policy. |
| 39 |
- Must be unique within the organization. |
| 40 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
description: |
| 44 |
description: |
| 45 |
- The user-defined description for the VSAN Policy. |
| 46 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 47 |
type: str |
| 48 |
aliases: [descr] |
| 49 |
tags: |
| 50 |
description: |
| 51 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
enable_trunking: |
| 55 |
description: |
| 56 |
- Enable or disable VSAN trunking on the policy. |
| 57 |
- When enabled, allows multiple VSANs to be carried over a single physical link. |
| 58 |
type: bool |
| 59 |
default: false |
| 60 |
vsans: |
| 61 |
description: |
| 62 |
- List of VSANs to be created and attached to the VSAN policy. |
| 63 |
- Each VSAN represents a virtual SAN segment. |
| 64 |
- Leave empty to create a policy without VSANs for manual configuration later. |
| 65 |
type: list |
| 66 |
elements: dict |
| 67 |
suboptions: |
| 68 |
name: |
| 69 |
description: |
| 70 |
- User-defined name for the VSAN configuration. |
| 71 |
- Must be unique within the VSAN policy. |
| 72 |
type: str |
| 73 |
required: true |
| 74 |
vsan_id: |
| 75 |
description: |
| 76 |
- Virtual SAN Identifier in the switch. |
| 77 |
- Valid range is typically 1-4094. |
| 78 |
- Must be unique within the fabric interconnect domain. |
| 79 |
type: int |
| 80 |
required: true |
| 81 |
fcoe_vlan_id: |
| 82 |
description: |
| 83 |
- FCoE VLAN ID associated with the VSAN configuration. |
| 84 |
- Must be between 2 and 4093. |
| 85 |
- VLAN IDs from 4043-4047, 4094, and 4095 are reserved for system use. |
| 86 |
- Required when state is present. |
| 87 |
type: int |
| 88 |
vsan_scope: |
| 89 |
description: |
| 90 |
- Indicates whether the VSAN ID is defined for storage or uplink or both traffics in FI. |
| 91 |
- Required when state is present. |
| 92 |
type: str |
| 93 |
choices: ['uplink', 'storage', 'common'] |
| 94 |
state: |
| 95 |
description: |
| 96 |
- Whether to create/update or delete the VSAN. |
| 97 |
type: str |
| 98 |
choices: ['present', 'absent'] |
| 99 |
default: present |
| 100 |
author: |
| 101 |
- Ron Gershburg (@rgershbu) |
| 102 |
''' |
| 103 |
|
| 104 |
EXAMPLES = r''' |
| 105 |
- name: Create a VSAN Policy with multiple VSANs |
| 106 |
cisco.intersight.intersight_vsan_policy: |
| 107 |
api_private_key: "{{ api_private_key }}" |
| 108 |
api_key_id: "{{ api_key_id }}" |
| 109 |
organization: "default" |
| 110 |
name: "datacenter-vsan-policy" |
| 111 |
description: "VSAN policy for datacenter SAN infrastructure" |
| 112 |
enable_trunking: false |
| 113 |
tags: |
| 114 |
- Key: "Environment" |
| 115 |
Value: "Production" |
| 116 |
- Key: "Site" |
| 117 |
Value: "DataCenter-A" |
| 118 |
vsans: |
| 119 |
- name: "vsan_uplink_100" |
| 120 |
vsan_id: 100 |
| 121 |
fcoe_vlan_id: 100 |
| 122 |
vsan_scope: "uplink" |
| 123 |
- name: "vsan_storage_200" |
| 124 |
vsan_id: 200 |
| 125 |
fcoe_vlan_id: 200 |
| 126 |
vsan_scope: "storage" |
| 127 |
- name: "vsan_common_300" |
| 128 |
vsan_id: 300 |
| 129 |
fcoe_vlan_id: 300 |
| 130 |
vsan_scope: "common" |
| 131 |
state: present |
| 132 |
|
| 133 |
- name: Create a VSAN Policy with trunking enabled |
| 134 |
cisco.intersight.intersight_vsan_policy: |
| 135 |
api_private_key: "{{ api_private_key }}" |
| 136 |
api_key_id: "{{ api_key_id }}" |
| 137 |
organization: "default" |
| 138 |
name: "trunked-vsan-policy" |
| 139 |
description: "VSAN policy with trunking enabled" |
| 140 |
enable_trunking: true |
| 141 |
vsans: |
| 142 |
- name: "vsan_fabric_a" |
| 143 |
vsan_id: 10 |
| 144 |
fcoe_vlan_id: 10 |
| 145 |
vsan_scope: "common" |
| 146 |
- name: "vsan_fabric_b" |
| 147 |
vsan_id: 20 |
| 148 |
fcoe_vlan_id: 20 |
| 149 |
vsan_scope: "common" |
| 150 |
state: present |
| 151 |
|
| 152 |
- name: Create a VSAN Policy without VSANs (for manual configuration) |
| 153 |
cisco.intersight.intersight_vsan_policy: |
| 154 |
api_private_key: "{{ api_private_key }}" |
| 155 |
api_key_id: "{{ api_key_id }}" |
| 156 |
organization: "Engineering" |
| 157 |
name: "empty-vsan-policy" |
| 158 |
description: "Empty policy for manual VSAN configuration" |
| 159 |
enable_trunking: false |
| 160 |
state: present |
| 161 |
|
| 162 |
- name: Update an existing VSAN Policy |
| 163 |
cisco.intersight.intersight_vsan_policy: |
| 164 |
api_private_key: "{{ api_private_key }}" |
| 165 |
api_key_id: "{{ api_key_id }}" |
| 166 |
name: "datacenter-vsan-policy" |
| 167 |
description: "Updated description for datacenter SAN infrastructure" |
| 168 |
enable_trunking: true |
| 169 |
tags: |
| 170 |
- Key: "Environment" |
| 171 |
Value: "Production" |
| 172 |
- Key: "Site" |
| 173 |
Value: "DataCenter-A" |
| 174 |
- Key: "Updated" |
| 175 |
Value: "2024-01-01" |
| 176 |
state: present |
| 177 |
|
| 178 |
- name: Delete a VSAN from a policy |
| 179 |
cisco.intersight.intersight_vsan_policy: |
| 180 |
api_private_key: "{{ api_private_key }}" |
| 181 |
api_key_id: "{{ api_key_id }}" |
| 182 |
name: "datacenter-vsan-policy" |
| 183 |
vsans: |
| 184 |
- name: "vsan_uplink_100" |
| 185 |
vsan_id: 100 |
| 186 |
fcoe_vlan_id: 100 |
| 187 |
vsan_scope: "uplink" |
| 188 |
- name: "vsan_storage_200" |
| 189 |
vsan_id: 200 |
| 190 |
fcoe_vlan_id: 200 |
| 191 |
vsan_scope: "storage" |
| 192 |
state: absent |
| 193 |
state: present |
| 194 |
|
| 195 |
- name: Delete a VSAN Policy |
| 196 |
cisco.intersight.intersight_vsan_policy: |
| 197 |
api_private_key: "{{ api_private_key }}" |
| 198 |
api_key_id: "{{ api_key_id }}" |
| 199 |
name: "datacenter-vsan-policy" |
| 200 |
state: absent |
| 201 |
''' |
| 202 |
|
| 203 |
RETURN = r''' |
| 204 |
api_response: |
| 205 |
description: The API response output returned by the specified resource. |
| 206 |
returned: always |
| 207 |
type: dict |
| 208 |
sample: |
| 209 |
"api_response": { |
| 210 |
"Name": "datacenter-vsan-policy", |
| 211 |
"ObjectType": "fabric.FcNetworkPolicy", |
| 212 |
"EnableTrunking": false, |
| 213 |
"Tags": [ |
| 214 |
{ |
| 215 |
"Key": "Site", |
| 216 |
"Value": "DataCenter-A" |
| 217 |
} |
| 218 |
], |
| 219 |
"vsans": [ |
| 220 |
{ |
| 221 |
"Name": "vsan_uplink_100", |
| 222 |
"ObjectType": "fabric.Vsan", |
| 223 |
"VsanId": 100, |
| 224 |
"FcoeVlan": 100, |
| 225 |
"VsanScope": "Uplink" |
| 226 |
} |
| 227 |
] |
| 228 |
} |
| 229 |
''' |
| 230 |
|
| 231 |
|
| 232 |
from ansible.module_utils.basic import AnsibleModule |
| 233 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 234 |
|
| 235 |
|
| 236 |
def main(): |
| 237 |
argument_spec = intersight_argument_spec.copy() |
| 238 |
argument_spec.update( |
| 239 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 240 |
organization=dict(type='str', default='default'), |
| 241 |
name=dict(type='str', required=True), |
| 242 |
description=dict(type='str', aliases=['descr']), |
| 243 |
tags=dict(type='list', elements='dict'), |
| 244 |
enable_trunking=dict(type='bool', default=False), |
| 245 |
vsans=dict(type='list', elements='dict', options=dict( |
| 246 |
name=dict(type='str', required=True), |
| 247 |
vsan_id=dict(type='int', required=True), |
| 248 |
fcoe_vlan_id=dict(type='int'), |
| 249 |
vsan_scope=dict(type='str', choices=['uplink', 'storage', 'common']), |
| 250 |
state=dict(type='str', choices=['present', 'absent'], default='present') |
| 251 |
)) |
| 252 |
) |
| 253 |
module = AnsibleModule( |
| 254 |
argument_spec, |
| 255 |
supports_check_mode=True, |
| 256 |
) |
| 257 |
|
| 258 |
intersight = IntersightModule(module) |
| 259 |
intersight.result['api_response'] = {} |
| 260 |
intersight.result['trace_id'] = '' |
| 261 |
|
| 262 |
# Resource path used to configure policy |
| 263 |
resource_path = '/fabric/FcNetworkPolicies' |
| 264 |
# Define API body used in compares or create |
| 265 |
intersight.api_body = { |
| 266 |
'Organization': { |
| 267 |
'Name': intersight.module.params['organization'], |
| 268 |
}, |
| 269 |
'Name': intersight.module.params['name'] |
| 270 |
} |
| 271 |
if intersight.module.params['state'] == 'present': |
| 272 |
intersight.api_body['EnableTrunking'] = intersight.module.params['enable_trunking'] |
| 273 |
intersight.set_tags_and_description() |
| 274 |
|
| 275 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 276 |
|
| 277 |
# Store the VSAN policy response |
| 278 |
vsan_policy_response = intersight.result['api_response'] |
| 279 |
|
| 280 |
vsan_policy_moid = None |
| 281 |
if intersight.module.params['state'] == 'present' and vsan_policy_response: |
| 282 |
vsan_policy_moid = vsan_policy_response.get('Moid') |
| 283 |
|
| 284 |
# Process VSANs if provided |
| 285 |
vsan_responses = [] |
| 286 |
if intersight.module.params['state'] == 'present' and intersight.module.params.get('vsans'): |
| 287 |
for vsan_config in intersight.module.params['vsans']: |
| 288 |
# Validate VSAN configuration |
| 289 |
vsan_name = vsan_config['name'] |
| 290 |
vsan_state = vsan_config.get('state', 'present') |
| 291 |
|
| 292 |
# If VSAN state is present, require fcoe_vlan_id and vsan_scope |
| 293 |
if vsan_state == 'present': |
| 294 |
if not vsan_config.get('fcoe_vlan_id'): |
| 295 |
module.fail_json(msg=f"fcoe_vlan_id is required for VSAN '{vsan_name}' when state is present") |
| 296 |
if not vsan_config.get('vsan_scope'): |
| 297 |
module.fail_json(msg=f"vsan_scope is required for VSAN '{vsan_name}' when state is present") |
| 298 |
|
| 299 |
vsan_id = vsan_config.get('vsan_id') |
| 300 |
fcoe_vlan_id = vsan_config.get('fcoe_vlan_id') |
| 301 |
vsan_scope = vsan_config.get('vsan_scope') |
| 302 |
|
| 303 |
# Only build API body and validate if VSAN state is present |
| 304 |
if vsan_state == 'present': |
| 305 |
# Validate FCoE VLAN ID range |
| 306 |
if fcoe_vlan_id < 2 or fcoe_vlan_id > 4093 or (fcoe_vlan_id >= 4043 and fcoe_vlan_id <= 4047): |
| 307 |
module.fail_json(msg=f"FCoE VLAN ID {fcoe_vlan_id} is invalid. Must be between 2 and 4093, excluding 4043-4047, 4094, and 4095.") |
| 308 |
|
| 309 |
# Build VSAN API body |
| 310 |
vsan_api_body = { |
| 311 |
'Name': vsan_name, |
| 312 |
'VsanId': vsan_id, |
| 313 |
'FcoeVlan': fcoe_vlan_id, |
| 314 |
'VsanScope': vsan_scope.capitalize(), |
| 315 |
'FcNetworkPolicy': vsan_policy_moid |
| 316 |
} |
| 317 |
intersight.api_body = vsan_api_body |
| 318 |
|
| 319 |
# Create or delete the VSAN |
| 320 |
resource_path = '/fabric/Vsans' |
| 321 |
# Filter by both VSAN ID AND FcNetworkPolicy |
| 322 |
custom_filter = f"VsanId eq {vsan_id} and FcNetworkPolicy.Moid eq '{vsan_policy_moid}'" |
| 323 |
intersight.configure_secondary_resource( |
| 324 |
resource_path=resource_path, |
| 325 |
state=vsan_state, |
| 326 |
custom_filter=custom_filter |
| 327 |
) |
| 328 |
|
| 329 |
# Store the VSAN response |
| 330 |
if intersight.result.get('api_response'): |
| 331 |
vsan_responses.append(intersight.result['api_response']) |
| 332 |
|
| 333 |
# Combine VSAN policy and VSANs in the main response |
| 334 |
if vsan_policy_response: |
| 335 |
if vsan_responses: |
| 336 |
vsan_policy_response['vsans'] = vsan_responses |
| 337 |
intersight.result['api_response'] = vsan_policy_response |
| 338 |
|
| 339 |
module.exit_json(**intersight.result) |
| 340 |
|
| 341 |
|
| 342 |
if __name__ == '__main__': |
| 343 |
main() |