| 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_vhba_template |
| 16 |
short_description: Manage vHBA Templates for Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete vHBA Templates on Cisco Intersight. |
| 19 |
- vHBA Templates define fibre channel interface configurations that can be used by SAN Connectivity policies. |
| 20 |
- Templates provide a standardized way to configure vHBAs with consistent fibre channel policies. |
| 21 |
- vHBA Templates are only applicable for FI-Attached (Fabric Interconnect) deployments. |
| 22 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/vnic/VhbaTemplate/get/). |
| 23 |
extends_documentation_fragment: intersight |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- If C(present), will verify the resource is present and will create if needed. |
| 28 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 29 |
type: str |
| 30 |
choices: [present, absent] |
| 31 |
default: present |
| 32 |
organization: |
| 33 |
description: |
| 34 |
- The name of the Organization this resource is assigned to. |
| 35 |
- Policies created within a Custom Organization are applicable only to devices in the same Organization. |
| 36 |
- Use 'default' for the default organization. |
| 37 |
type: str |
| 38 |
default: default |
| 39 |
name: |
| 40 |
description: |
| 41 |
- The name assigned to the vHBA Template. |
| 42 |
- Must be unique within the organization. |
| 43 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 44 |
type: str |
| 45 |
required: true |
| 46 |
description: |
| 47 |
description: |
| 48 |
- The user-defined description for the vHBA Template. |
| 49 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 50 |
type: str |
| 51 |
aliases: [descr] |
| 52 |
tags: |
| 53 |
description: |
| 54 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 55 |
type: list |
| 56 |
elements: dict |
| 57 |
enable_override: |
| 58 |
description: |
| 59 |
- When enabled, the configuration of the derived instances may override the template configuration. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
vhba_type: |
| 63 |
description: |
| 64 |
- vHBA Type configuration for the template. |
| 65 |
- This configuration is supported only on Cisco VIC 14XX series and higher series of adapters. |
| 66 |
- Required when state is present. |
| 67 |
type: str |
| 68 |
choices: ['fc-initiator', 'fc-nvme-initiator', 'fc-nvme-target', 'fc-target'] |
| 69 |
switch_id: |
| 70 |
description: |
| 71 |
- The fabric port to which the vHBA will be associated. |
| 72 |
type: str |
| 73 |
choices: ['A', 'B'] |
| 74 |
default: 'A' |
| 75 |
persistent_bindings: |
| 76 |
description: |
| 77 |
- Enables retention of LUN ID associations in memory until they are manually cleared. |
| 78 |
type: bool |
| 79 |
default: false |
| 80 |
wwpn_pool_name: |
| 81 |
description: |
| 82 |
- The WWPN pool that is assigned to the vHBA Template. |
| 83 |
- Required when state is present. |
| 84 |
type: str |
| 85 |
fibre_channel_network_policy_name: |
| 86 |
description: |
| 87 |
- Name of the Fibre Channel Network Policy. |
| 88 |
- Required when state is present. |
| 89 |
type: str |
| 90 |
fibre_channel_qos_policy_name: |
| 91 |
description: |
| 92 |
- Name of the Fibre Channel QoS Policy. |
| 93 |
- Required when state is present. |
| 94 |
type: str |
| 95 |
fibre_channel_adapter_policy_name: |
| 96 |
description: |
| 97 |
- Name of the Fibre Channel Adapter Policy. |
| 98 |
- Required when state is present. |
| 99 |
type: str |
| 100 |
fibre_channel_zone_policy_names: |
| 101 |
description: |
| 102 |
- List of Fibre Channel Zone Policy names. |
| 103 |
- Relationship to the FC Zone policies to configure Zones on the switch. |
| 104 |
type: list |
| 105 |
elements: str |
| 106 |
pin_group_name: |
| 107 |
description: |
| 108 |
- Pingroup name associated to vHBA for static pinning. |
| 109 |
- SCP deploy will resolve pingroup name and fetches the corresponding uplink port/port channel to pin the vHBA traffic. |
| 110 |
type: str |
| 111 |
author: |
| 112 |
- Ron Gershburg (@rgershbu) |
| 113 |
''' |
| 114 |
|
| 115 |
EXAMPLES = r''' |
| 116 |
- name: Create a basic vHBA Template with WWPN pool |
| 117 |
cisco.intersight.intersight_vhba_template: |
| 118 |
api_private_key: "{{ api_private_key }}" |
| 119 |
api_key_id: "{{ api_key_id }}" |
| 120 |
organization: "default" |
| 121 |
name: "basic-vhba-template" |
| 122 |
description: "Basic vHBA template for production servers" |
| 123 |
enable_override: false |
| 124 |
vhba_type: "fc-initiator" |
| 125 |
switch_id: "A" |
| 126 |
persistent_bindings: false |
| 127 |
wwpn_pool_name: "default-wwpn-pool" |
| 128 |
fibre_channel_network_policy_name: "fc-network-policy" |
| 129 |
fibre_channel_qos_policy_name: "fc-qos-policy" |
| 130 |
fibre_channel_adapter_policy_name: "fc-adapter-policy" |
| 131 |
tags: |
| 132 |
- Key: "Environment" |
| 133 |
Value: "Production" |
| 134 |
- Key: "Site" |
| 135 |
Value: "DataCenter-A" |
| 136 |
state: present |
| 137 |
|
| 138 |
- name: Create a vHBA Template with override enabled |
| 139 |
cisco.intersight.intersight_vhba_template: |
| 140 |
api_private_key: "{{ api_private_key }}" |
| 141 |
api_key_id: "{{ api_key_id }}" |
| 142 |
organization: "default" |
| 143 |
name: "override-vhba-template" |
| 144 |
description: "vHBA template with override enabled" |
| 145 |
enable_override: true |
| 146 |
vhba_type: "fc-target" |
| 147 |
switch_id: "B" |
| 148 |
persistent_bindings: true |
| 149 |
wwpn_pool_name: "default-wwpn-pool" |
| 150 |
fibre_channel_network_policy_name: "fc-network-policy" |
| 151 |
fibre_channel_qos_policy_name: "fc-qos-policy" |
| 152 |
fibre_channel_adapter_policy_name: "fc-adapter-policy" |
| 153 |
state: present |
| 154 |
|
| 155 |
- name: Create a vHBA Template with FC Zone policies |
| 156 |
cisco.intersight.intersight_vhba_template: |
| 157 |
api_private_key: "{{ api_private_key }}" |
| 158 |
api_key_id: "{{ api_key_id }}" |
| 159 |
organization: "default" |
| 160 |
name: "vhba-with-zones" |
| 161 |
description: "vHBA template with FC zone policies" |
| 162 |
enable_override: false |
| 163 |
vhba_type: "fc-initiator" |
| 164 |
switch_id: "A" |
| 165 |
persistent_bindings: false |
| 166 |
wwpn_pool_name: "default-wwpn-pool" |
| 167 |
fibre_channel_network_policy_name: "fc-network-policy" |
| 168 |
fibre_channel_qos_policy_name: "fc-qos-policy" |
| 169 |
fibre_channel_adapter_policy_name: "fc-adapter-policy" |
| 170 |
fibre_channel_zone_policy_names: |
| 171 |
- "fc-zone-policy-1" |
| 172 |
- "fc-zone-policy-2" |
| 173 |
state: present |
| 174 |
|
| 175 |
- name: Create a vHBA Template with pin group |
| 176 |
cisco.intersight.intersight_vhba_template: |
| 177 |
api_private_key: "{{ api_private_key }}" |
| 178 |
api_key_id: "{{ api_key_id }}" |
| 179 |
organization: "default" |
| 180 |
name: "vhba-pinned-template" |
| 181 |
description: "vHBA template with static pinning" |
| 182 |
enable_override: false |
| 183 |
vhba_type: "fc-nvme-initiator" |
| 184 |
switch_id: "A" |
| 185 |
persistent_bindings: true |
| 186 |
wwpn_address_type: "pool" |
| 187 |
wwpn_pool_name: "nvme-wwpn-pool" |
| 188 |
fibre_channel_network_policy_name: "fc-network-policy" |
| 189 |
fibre_channel_qos_policy_name: "fc-qos-policy" |
| 190 |
fibre_channel_adapter_policy_name: "fc-adapter-policy" |
| 191 |
pin_group_name: "pingroup-a" |
| 192 |
state: present |
| 193 |
|
| 194 |
- name: Delete a vHBA Template |
| 195 |
cisco.intersight.intersight_vhba_template: |
| 196 |
api_private_key: "{{ api_private_key }}" |
| 197 |
api_key_id: "{{ api_key_id }}" |
| 198 |
name: "old-vhba-template" |
| 199 |
state: absent |
| 200 |
''' |
| 201 |
|
| 202 |
RETURN = r''' |
| 203 |
api_response: |
| 204 |
description: The API response output returned by the specified resource. |
| 205 |
returned: always |
| 206 |
type: dict |
| 207 |
sample: |
| 208 |
"api_response": { |
| 209 |
"Name": "test-vhba-template", |
| 210 |
"ObjectType": "vnic.VhbaTemplate", |
| 211 |
"EnableOverride": false, |
| 212 |
"Type": "fc-initiator", |
| 213 |
"SwitchId": "A", |
| 214 |
"PersistentBindings": false, |
| 215 |
"WwpnAddressType": "POOL", |
| 216 |
"WwpnPool": { |
| 217 |
"Name": "default-wwpn-pool", |
| 218 |
"ObjectType": "fcpool.Pool" |
| 219 |
}, |
| 220 |
"FcNetworkPolicy": { |
| 221 |
"Name": "fc-network-policy", |
| 222 |
"ObjectType": "vnic.FcNetworkPolicy" |
| 223 |
}, |
| 224 |
"Tags": [ |
| 225 |
{ |
| 226 |
"Key": "Environment", |
| 227 |
"Value": "Production" |
| 228 |
} |
| 229 |
] |
| 230 |
} |
| 231 |
''' |
| 232 |
|
| 233 |
|
| 234 |
from ansible.module_utils.basic import AnsibleModule |
| 235 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 236 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight_vhba_utils import ( |
| 237 |
get_policy_moid_with_org, resolve_policy_moids_from_mappings, resolve_fc_zone_policies, |
| 238 |
get_vhba_template_policy_mappings |
| 239 |
) |
| 240 |
|
| 241 |
|
| 242 |
def validate_input(module): |
| 243 |
""" |
| 244 |
Validate module input parameters. |
| 245 |
""" |
| 246 |
if module.params['state'] == 'present': |
| 247 |
# Validate required fields for vHBA Template creation |
| 248 |
required_fields = [ |
| 249 |
'vhba_type', |
| 250 |
'wwpn_pool_name', |
| 251 |
'fibre_channel_network_policy_name', |
| 252 |
'fibre_channel_qos_policy_name', |
| 253 |
'fibre_channel_adapter_policy_name' |
| 254 |
] |
| 255 |
for field in required_fields: |
| 256 |
if not module.params.get(field): |
| 257 |
module.fail_json(msg=f"{field} is required when state is 'present'") |
| 258 |
|
| 259 |
|
| 260 |
def main(): |
| 261 |
argument_spec = intersight_argument_spec.copy() |
| 262 |
argument_spec.update( |
| 263 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 264 |
organization=dict(type='str', default='default'), |
| 265 |
name=dict(type='str', required=True), |
| 266 |
description=dict(type='str', aliases=['descr']), |
| 267 |
tags=dict(type='list', elements='dict'), |
| 268 |
enable_override=dict(type='bool', default=False), |
| 269 |
vhba_type=dict(type='str', choices=['fc-initiator', 'fc-nvme-initiator', 'fc-nvme-target', 'fc-target']), |
| 270 |
switch_id=dict(type='str', choices=['A', 'B'], default='A'), |
| 271 |
persistent_bindings=dict(type='bool', default=False), |
| 272 |
wwpn_pool_name=dict(type='str'), |
| 273 |
fibre_channel_network_policy_name=dict(type='str'), |
| 274 |
fibre_channel_qos_policy_name=dict(type='str'), |
| 275 |
fibre_channel_adapter_policy_name=dict(type='str'), |
| 276 |
fibre_channel_zone_policy_names=dict(type='list', elements='str'), |
| 277 |
pin_group_name=dict(type='str') |
| 278 |
) |
| 279 |
module = AnsibleModule( |
| 280 |
argument_spec, |
| 281 |
supports_check_mode=True, |
| 282 |
) |
| 283 |
if module.params['state'] == 'present': |
| 284 |
validate_input(module) |
| 285 |
|
| 286 |
intersight = IntersightModule(module) |
| 287 |
intersight.result['api_response'] = {} |
| 288 |
intersight.result['trace_id'] = '' |
| 289 |
|
| 290 |
# Resource path used to configure vHBA Template |
| 291 |
resource_path = '/vnic/VhbaTemplates' |
| 292 |
|
| 293 |
# Define API body used in compares or create |
| 294 |
intersight.api_body = { |
| 295 |
'Organization': { |
| 296 |
'Name': intersight.module.params['organization'], |
| 297 |
}, |
| 298 |
'Name': intersight.module.params['name'] |
| 299 |
} |
| 300 |
|
| 301 |
if intersight.module.params['state'] == 'present': |
| 302 |
intersight.set_tags_and_description() |
| 303 |
|
| 304 |
# Add vHBA Template specific parameters |
| 305 |
intersight.api_body['EnableOverride'] = intersight.module.params['enable_override'] |
| 306 |
intersight.api_body['Type'] = intersight.module.params['vhba_type'] |
| 307 |
intersight.api_body['SwitchId'] = intersight.module.params['switch_id'] |
| 308 |
intersight.api_body['PersistentBindings'] = intersight.module.params['persistent_bindings'] |
| 309 |
|
| 310 |
# Cache for policy MOIDs to avoid redundant API calls |
| 311 |
policy_cache = {} |
| 312 |
organization_name = intersight.module.params['organization'] |
| 313 |
|
| 314 |
# Resolve WWPN Pool MOID |
| 315 |
wwpn_pool_moid = get_policy_moid_with_org( |
| 316 |
intersight, policy_cache, module, '/fcpool/Pools', |
| 317 |
intersight.module.params['wwpn_pool_name'], organization_name, 'WWPN Pool' |
| 318 |
) |
| 319 |
intersight.api_body['WwpnPool'] = wwpn_pool_moid |
| 320 |
|
| 321 |
# Resolve FC policy MOIDs |
| 322 |
policy_mappings = get_vhba_template_policy_mappings() |
| 323 |
policy_moids = resolve_policy_moids_from_mappings(intersight, policy_cache, module, intersight.module.params, policy_mappings, organization_name) |
| 324 |
intersight.api_body.update(policy_moids) |
| 325 |
|
| 326 |
# Resolve Fibre Channel Zone Policies (optional) |
| 327 |
fc_zone_policy_names = intersight.module.params.get('fibre_channel_zone_policy_names') |
| 328 |
fc_zone_policy_moids = resolve_fc_zone_policies(intersight, policy_cache, module, fc_zone_policy_names, organization_name) |
| 329 |
intersight.api_body['FcZonePolicies'] = fc_zone_policy_moids |
| 330 |
|
| 331 |
# Add pin group name if specified |
| 332 |
if intersight.module.params.get('pin_group_name'): |
| 333 |
intersight.api_body['PinGroupName'] = intersight.module.params['pin_group_name'] |
| 334 |
|
| 335 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 336 |
|
| 337 |
module.exit_json(**intersight.result) |
| 338 |
|
| 339 |
|
| 340 |
if __name__ == '__main__': |
| 341 |
main() |