| 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_network_policy |
| 16 |
short_description: Manage Fibre Channel Network Policies for Cisco Intersight |
| 17 |
description: |
| 18 |
- Create, update, and delete Fibre Channel Network Policies on Cisco Intersight. |
| 19 |
- Fibre Channel Network policies configure VSAN settings for Fibre Channel virtual interfaces. |
| 20 |
- These policies control the default VLAN ID and VSAN ID assignments for FC networks. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/vnic/FcNetworkPolicies/get/). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
state: |
| 25 |
description: |
| 26 |
- If C(present), will verify the resource is present and will create if needed. |
| 27 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 28 |
type: str |
| 29 |
choices: [present, absent] |
| 30 |
default: present |
| 31 |
organization: |
| 32 |
description: |
| 33 |
- The name of the Organization this resource is assigned to. |
| 34 |
- Policies created within a Custom Organization are applicable only to devices in the same Organization. |
| 35 |
- Use 'default' for the default organization. |
| 36 |
type: str |
| 37 |
default: default |
| 38 |
name: |
| 39 |
description: |
| 40 |
- The name assigned to the Fibre Channel Network Policy. |
| 41 |
- Must be unique within the organization. |
| 42 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
description: |
| 46 |
description: |
| 47 |
- The user-defined description for the Fibre Channel Network Policy. |
| 48 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 49 |
type: str |
| 50 |
aliases: [descr] |
| 51 |
tags: |
| 52 |
description: |
| 53 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
default_vlan: |
| 57 |
description: |
| 58 |
- The default VLAN ID for the Fibre Channel network. |
| 59 |
- Valid range is 0 to 4094. |
| 60 |
- A value of 0 means the VLAN is not configured. |
| 61 |
type: int |
| 62 |
default: 0 |
| 63 |
vsan_id: |
| 64 |
description: |
| 65 |
- The VSAN (Virtual Storage Area Network) ID for the Fibre Channel network. |
| 66 |
- Valid range is 1 to 4094. |
| 67 |
type: int |
| 68 |
default: 1 |
| 69 |
author: |
| 70 |
- Ron Gershburg (@rgershbu) |
| 71 |
''' |
| 72 |
|
| 73 |
EXAMPLES = r''' |
| 74 |
- name: Create Fibre Channel Network Policy with default settings |
| 75 |
cisco.intersight.intersight_fibre_channel_network_policy: |
| 76 |
api_private_key: "{{ api_private_key }}" |
| 77 |
api_key_id: "{{ api_key_id }}" |
| 78 |
organization: "default" |
| 79 |
name: "fc-network-default" |
| 80 |
description: "Fibre Channel Network policy with default values" |
| 81 |
state: present |
| 82 |
|
| 83 |
- name: Create Fibre Channel Network Policy with custom VSAN settings |
| 84 |
cisco.intersight.intersight_fibre_channel_network_policy: |
| 85 |
api_private_key: "{{ api_private_key }}" |
| 86 |
api_key_id: "{{ api_key_id }}" |
| 87 |
organization: "default" |
| 88 |
name: "fc-network-vsan100" |
| 89 |
description: "Fibre Channel Network policy for VSAN 100" |
| 90 |
default_vlan: 100 |
| 91 |
vsan_id: 100 |
| 92 |
tags: |
| 93 |
- Key: Environment |
| 94 |
Value: Production |
| 95 |
state: present |
| 96 |
|
| 97 |
- name: Create Fibre Channel Network Policy with VLAN and different VSAN |
| 98 |
cisco.intersight.intersight_fibre_channel_network_policy: |
| 99 |
api_private_key: "{{ api_private_key }}" |
| 100 |
api_key_id: "{{ api_key_id }}" |
| 101 |
organization: "Engineering" |
| 102 |
name: "fc-network-custom" |
| 103 |
description: "Custom Fibre Channel Network policy" |
| 104 |
default_vlan: 200 |
| 105 |
vsan_id: 300 |
| 106 |
state: present |
| 107 |
|
| 108 |
- name: Create Fibre Channel Network Policy with maximum VSAN ID |
| 109 |
cisco.intersight.intersight_fibre_channel_network_policy: |
| 110 |
api_private_key: "{{ api_private_key }}" |
| 111 |
api_key_id: "{{ api_key_id }}" |
| 112 |
organization: "default" |
| 113 |
name: "fc-network-max-vsan" |
| 114 |
description: "Fibre Channel Network policy with maximum VSAN ID" |
| 115 |
default_vlan: 4094 |
| 116 |
vsan_id: 4094 |
| 117 |
state: present |
| 118 |
|
| 119 |
- name: Update Fibre Channel Network Policy description |
| 120 |
cisco.intersight.intersight_fibre_channel_network_policy: |
| 121 |
api_private_key: "{{ api_private_key }}" |
| 122 |
api_key_id: "{{ api_key_id }}" |
| 123 |
organization: "default" |
| 124 |
name: "fc-network-default" |
| 125 |
description: "Updated Fibre Channel Network policy description" |
| 126 |
state: present |
| 127 |
|
| 128 |
- name: Delete Fibre Channel Network Policy |
| 129 |
cisco.intersight.intersight_fibre_channel_network_policy: |
| 130 |
api_private_key: "{{ api_private_key }}" |
| 131 |
api_key_id: "{{ api_key_id }}" |
| 132 |
organization: "default" |
| 133 |
name: "fc-network-default" |
| 134 |
state: absent |
| 135 |
''' |
| 136 |
|
| 137 |
RETURN = r''' |
| 138 |
api_response: |
| 139 |
description: The API response output returned by the specified resource. |
| 140 |
returned: always |
| 141 |
type: dict |
| 142 |
sample: |
| 143 |
"api_response": { |
| 144 |
"Name": "fc-network-vsan100", |
| 145 |
"ObjectType": "vnic.FcNetworkPolicy", |
| 146 |
"Moid": "1234567890abcdef12345678", |
| 147 |
"Description": "Fibre Channel Network policy for VSAN 100", |
| 148 |
"VsanSettings": { |
| 149 |
"DefaultVlanId": 100, |
| 150 |
"Id": 100 |
| 151 |
}, |
| 152 |
"Organization": { |
| 153 |
"Moid": "abcdef1234567890abcdef12", |
| 154 |
"ObjectType": "organization.Organization" |
| 155 |
}, |
| 156 |
"Tags": [ |
| 157 |
{ |
| 158 |
"Key": "Environment", |
| 159 |
"Value": "Production" |
| 160 |
} |
| 161 |
] |
| 162 |
} |
| 163 |
''' |
| 164 |
|
| 165 |
|
| 166 |
from ansible.module_utils.basic import AnsibleModule |
| 167 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 168 |
|
| 169 |
|
| 170 |
def validate_parameters(module): |
| 171 |
""" |
| 172 |
Validate module parameters for Fibre Channel Network policy configuration. |
| 173 |
""" |
| 174 |
if module.params['state'] != 'present': |
| 175 |
return |
| 176 |
# Validate default_vlan range (0-4094) |
| 177 |
default_vlan = module.params.get('default_vlan') |
| 178 |
if default_vlan is not None and (default_vlan < 0 or default_vlan > 4094): |
| 179 |
module.fail_json(msg="Parameter 'default_vlan' must be between 0 and 4094") |
| 180 |
# Validate vsan_id range (1-4094) |
| 181 |
vsan_id = module.params.get('vsan_id') |
| 182 |
if vsan_id is not None and (vsan_id < 1 or vsan_id > 4094): |
| 183 |
module.fail_json(msg="Parameter 'vsan_id' must be between 1 and 4094") |
| 184 |
|
| 185 |
|
| 186 |
def main(): |
| 187 |
argument_spec = intersight_argument_spec.copy() |
| 188 |
argument_spec.update( |
| 189 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 190 |
organization=dict(type='str', default='default'), |
| 191 |
name=dict(type='str', required=True), |
| 192 |
description=dict(type='str', aliases=['descr']), |
| 193 |
tags=dict(type='list', elements='dict'), |
| 194 |
default_vlan=dict(type='int', default=0), |
| 195 |
vsan_id=dict(type='int', default=1), |
| 196 |
) |
| 197 |
module = AnsibleModule( |
| 198 |
argument_spec, |
| 199 |
supports_check_mode=True, |
| 200 |
) |
| 201 |
intersight = IntersightModule(module) |
| 202 |
intersight.result['api_response'] = {} |
| 203 |
intersight.result['trace_id'] = '' |
| 204 |
# Validate module parameters |
| 205 |
validate_parameters(module) |
| 206 |
# Resource path used to configure policy |
| 207 |
resource_path = '/vnic/FcNetworkPolicies' |
| 208 |
# Define API body used in compares or create |
| 209 |
intersight.api_body = { |
| 210 |
'Organization': { |
| 211 |
'Name': module.params['organization'], |
| 212 |
}, |
| 213 |
'Name': module.params['name'], |
| 214 |
} |
| 215 |
if module.params['state'] == 'present': |
| 216 |
intersight.api_body['VsanSettings'] = { |
| 217 |
'DefaultVlanId': module.params['default_vlan'], |
| 218 |
'Id': module.params['vsan_id'], |
| 219 |
} |
| 220 |
intersight.set_tags_and_description() |
| 221 |
intersight.configure_policy_or_profile(resource_path=resource_path) |
| 222 |
module.exit_json(**intersight.result) |
| 223 |
|
| 224 |
|
| 225 |
if __name__ == '__main__': |
| 226 |
main() |