| 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_port_policy_info |
| 16 |
short_description: Gather information about Cisco Intersight Port Policies |
| 17 |
description: |
| 18 |
- Gathers information about Port Policies and their associated resources on Cisco Intersight. |
| 19 |
- Retrieves comprehensive information including breakout ports, server ports, uplink port channels, pin groups, and all other port configurations. |
| 20 |
- Supports filtering by policy name and organization. |
| 21 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs/fabric/PortPolicies/get/). |
| 22 |
extends_documentation_fragment: intersight |
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the Port Policy to retrieve information for. |
| 27 |
- If not provided, information for all Port Policies will be returned. |
| 28 |
type: str |
| 29 |
organization: |
| 30 |
description: |
| 31 |
- The name of the Organization to filter policies by. |
| 32 |
- If not provided, policies from all accessible organizations will be returned. |
| 33 |
type: str |
| 34 |
author: |
| 35 |
- Ron Gershburg (@rgershbu) |
| 36 |
''' |
| 37 |
|
| 38 |
EXAMPLES = r''' |
| 39 |
- name: Get information about all Port Policies |
| 40 |
cisco.intersight.intersight_port_policy_info: |
| 41 |
api_private_key: "{{ api_private_key }}" |
| 42 |
api_key_id: "{{ api_key_id }}" |
| 43 |
register: all_port_policies |
| 44 |
|
| 45 |
- name: Get information about a specific Port Policy |
| 46 |
cisco.intersight.intersight_port_policy_info: |
| 47 |
api_private_key: "{{ api_private_key }}" |
| 48 |
api_key_id: "{{ api_key_id }}" |
| 49 |
name: "port-policy-example" |
| 50 |
register: specific_policy |
| 51 |
|
| 52 |
- name: Get information about Port Policies in a specific organization |
| 53 |
cisco.intersight.intersight_port_policy_info: |
| 54 |
api_private_key: "{{ api_private_key }}" |
| 55 |
api_key_id: "{{ api_key_id }}" |
| 56 |
organization: "production" |
| 57 |
register: org_policies |
| 58 |
|
| 59 |
- name: Get information about a specific Port Policy in a specific organization |
| 60 |
cisco.intersight.intersight_port_policy_info: |
| 61 |
api_private_key: "{{ api_private_key }}" |
| 62 |
api_key_id: "{{ api_key_id }}" |
| 63 |
name: "port-policy-example" |
| 64 |
organization: "production" |
| 65 |
register: specific_org_policy |
| 66 |
''' |
| 67 |
|
| 68 |
RETURN = r''' |
| 69 |
api_response: |
| 70 |
description: List of Port Policy information with all associated resources |
| 71 |
returned: always |
| 72 |
type: list |
| 73 |
elements: dict |
| 74 |
sample: [ |
| 75 |
{ |
| 76 |
"Name": "port-policy-example", |
| 77 |
"Moid": "5dee1d736972652d321d26b5", |
| 78 |
"ObjectType": "fabric.PortPolicy", |
| 79 |
"DeviceModel": "UCS-FI-6454", |
| 80 |
"Description": "Example port policy with various configurations", |
| 81 |
"Organization": { |
| 82 |
"Name": "default", |
| 83 |
"Moid": "5dee1d736972652d321d26b5", |
| 84 |
"ObjectType": "organization.Organization" |
| 85 |
}, |
| 86 |
"Tags": [ |
| 87 |
{ |
| 88 |
"Key": "Environment", |
| 89 |
"Value": "Production" |
| 90 |
} |
| 91 |
], |
| 92 |
"BreakoutPorts": [ |
| 93 |
{ |
| 94 |
"PortId": 49, |
| 95 |
"CustomMode": "BreakoutEthernet25G", |
| 96 |
"SlotId": 1, |
| 97 |
"Moid": "5dee1d736972652d321d26b6", |
| 98 |
"ObjectType": "fabric.PortMode" |
| 99 |
} |
| 100 |
], |
| 101 |
"FcPortModes": [ |
| 102 |
{ |
| 103 |
"PortIdStart": 1, |
| 104 |
"PortIdEnd": 16, |
| 105 |
"SlotId": 1, |
| 106 |
"CustomMode": "FibreChannel", |
| 107 |
"Moid": "5dee1d736972652d321d26b7", |
| 108 |
"ObjectType": "fabric.PortMode" |
| 109 |
} |
| 110 |
], |
| 111 |
"ServerRoles": [ |
| 112 |
{ |
| 113 |
"PortId": 1, |
| 114 |
"AggregatePortId": 49, |
| 115 |
"SlotId": 1, |
| 116 |
"Fec": "Auto", |
| 117 |
"UserLabel": "Server Port 1", |
| 118 |
"PreferredDeviceType": "Chassis", |
| 119 |
"PreferredDeviceId": 1, |
| 120 |
"Moid": "5dee1d736972652d321d26b8", |
| 121 |
"ObjectType": "fabric.ServerRole" |
| 122 |
} |
| 123 |
], |
| 124 |
"UplinkPcRoles": [ |
| 125 |
{ |
| 126 |
"PcId": 123, |
| 127 |
"AdminSpeed": "25Gbps", |
| 128 |
"Fec": "Auto", |
| 129 |
"UserLabel": "Uplink PC 123", |
| 130 |
"SlotId": 1, |
| 131 |
"Moid": "5dee1d736972652d321d26b9", |
| 132 |
"ObjectType": "fabric.UplinkPcRole", |
| 133 |
"Ports": [ |
| 134 |
{ |
| 135 |
"PortId": 1, |
| 136 |
"SlotId": 1, |
| 137 |
"AggregatePortId": 0 |
| 138 |
}, |
| 139 |
{ |
| 140 |
"PortId": 2, |
| 141 |
"SlotId": 1, |
| 142 |
"AggregatePortId": 0 |
| 143 |
} |
| 144 |
], |
| 145 |
"EthNetworkGroupPolicy": [ |
| 146 |
{ |
| 147 |
"Moid": "5dee1d736972652d321d26ba", |
| 148 |
"ObjectType": "fabric.EthNetworkGroupPolicy" |
| 149 |
} |
| 150 |
], |
| 151 |
"FlowControlPolicy": { |
| 152 |
"Moid": "5dee1d736972652d321d26bb", |
| 153 |
"ObjectType": "fabric.FlowControlPolicy" |
| 154 |
}, |
| 155 |
"LinkAggregationPolicy": { |
| 156 |
"Moid": "5dee1d736972652d321d26bc", |
| 157 |
"ObjectType": "fabric.LinkAggregationPolicy" |
| 158 |
}, |
| 159 |
"LinkControlPolicy": { |
| 160 |
"Moid": "5dee1d736972652d321d26bd", |
| 161 |
"ObjectType": "fabric.LinkControlPolicy" |
| 162 |
} |
| 163 |
} |
| 164 |
], |
| 165 |
"FcUplinkPortChannels": [ |
| 166 |
{ |
| 167 |
"PcId": 13, |
| 168 |
"AdminSpeed": "16Gbps", |
| 169 |
"VsanId": 1, |
| 170 |
"UserLabel": "FC Uplink PC 13", |
| 171 |
"SlotId": 1, |
| 172 |
"Moid": "5dee1d736972652d321d26be", |
| 173 |
"ObjectType": "fabric.FcUplinkPcRole", |
| 174 |
"Ports": [ |
| 175 |
{ |
| 176 |
"PortId": 17, |
| 177 |
"SlotId": 1, |
| 178 |
"AggregatePortId": 0 |
| 179 |
}, |
| 180 |
{ |
| 181 |
"PortId": 18, |
| 182 |
"SlotId": 1, |
| 183 |
"AggregatePortId": 0 |
| 184 |
} |
| 185 |
] |
| 186 |
} |
| 187 |
], |
| 188 |
"FcUplinkRoles": [ |
| 189 |
{ |
| 190 |
"PortId": 19, |
| 191 |
"AdminSpeed": "16Gbps", |
| 192 |
"VsanId": 1, |
| 193 |
"UserLabel": "FC Uplink 19", |
| 194 |
"SlotId": 1, |
| 195 |
"Moid": "5dee1d736972652d321d26bf", |
| 196 |
"ObjectType": "fabric.FcUplinkRole" |
| 197 |
} |
| 198 |
], |
| 199 |
"UplinkRoles": [ |
| 200 |
{ |
| 201 |
"PortId": 3, |
| 202 |
"AdminSpeed": "25Gbps", |
| 203 |
"Fec": "Auto", |
| 204 |
"UserLabel": "Ethernet Uplink 3", |
| 205 |
"SlotId": 1, |
| 206 |
"Moid": "5dee1d736972652d321d26c0", |
| 207 |
"ObjectType": "fabric.UplinkRole" |
| 208 |
} |
| 209 |
], |
| 210 |
"LanPinGroups": [ |
| 211 |
{ |
| 212 |
"Name": "pin-group-1", |
| 213 |
"Moid": "5dee1d736972652d321d26c1", |
| 214 |
"ObjectType": "fabric.LanPinGroup", |
| 215 |
"PinTargetInterfaceRole": { |
| 216 |
"Moid": "5dee1d736972652d321d26c2", |
| 217 |
"ObjectType": "fabric.UplinkPcRole" |
| 218 |
} |
| 219 |
} |
| 220 |
], |
| 221 |
"SanPinGroups": [ |
| 222 |
{ |
| 223 |
"Name": "san-pin-group-1", |
| 224 |
"Moid": "5dee1d736972652d321d26c3", |
| 225 |
"ObjectType": "fabric.SanPinGroup", |
| 226 |
"PinTargetInterfaceRole": { |
| 227 |
"Moid": "5dee1d736972652d321d26c4", |
| 228 |
"ObjectType": "fabric.FcUplinkPcRole" |
| 229 |
} |
| 230 |
} |
| 231 |
], |
| 232 |
"FcoeUplinkPcRoles": [], |
| 233 |
"AppliancePcRoles": [], |
| 234 |
"FcStorageRoles": [], |
| 235 |
"ApplianceRoles": [], |
| 236 |
"FcoeUplinkRoles": [] |
| 237 |
} |
| 238 |
] |
| 239 |
''' |
| 240 |
|
| 241 |
from ansible.module_utils.basic import AnsibleModule |
| 242 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 243 |
|
| 244 |
|
| 245 |
def get_port_policy_secondary_resources(intersight, port_policy_moid): |
| 246 |
""" |
| 247 |
Retrieve all secondary resources associated with a port policy. |
| 248 |
|
| 249 |
Args: |
| 250 |
intersight: IntersightModule instance |
| 251 |
port_policy_moid: MOID of the port policy |
| 252 |
|
| 253 |
Returns: |
| 254 |
Dictionary containing all secondary resources |
| 255 |
""" |
| 256 |
# Initialize all resource keys to ensure consistent structure |
| 257 |
secondary_resources = { |
| 258 |
'BreakoutPorts': [], |
| 259 |
'FcPortModes': [], |
| 260 |
'ServerRoles': [], |
| 261 |
'UplinkPcRoles': [], |
| 262 |
'FcUplinkPortChannels': [], |
| 263 |
'FcoeUplinkPcRoles': [], |
| 264 |
'AppliancePcRoles': [], |
| 265 |
'FcUplinkRoles': [], |
| 266 |
'FcStorageRoles': [], |
| 267 |
'ApplianceRoles': [], |
| 268 |
'UplinkRoles': [], |
| 269 |
'FcoeUplinkRoles': [], |
| 270 |
'LanPinGroups': [], |
| 271 |
'SanPinGroups': [], |
| 272 |
} |
| 273 |
|
| 274 |
# Define resource mappings based on actual endpoints used in intersight_port_policy.py |
| 275 |
resource_mappings = [ |
| 276 |
# Port Modes (FC port mode and Breakout ports) |
| 277 |
('/fabric/PortModes', 'PortModes'), |
| 278 |
|
| 279 |
# Port Channels |
| 280 |
('/fabric/UplinkPcRoles', 'UplinkPcRoles'), |
| 281 |
('/fabric/FcUplinkPcRoles', 'FcUplinkPortChannels'), |
| 282 |
('/fabric/FcoeUplinkPcRoles', 'FcoeUplinkPcRoles'), |
| 283 |
('/fabric/AppliancePcRoles', 'AppliancePcRoles'), |
| 284 |
|
| 285 |
# Individual Ports |
| 286 |
('/fabric/ServerRoles', 'ServerRoles'), |
| 287 |
('/fabric/FcUplinkRoles', 'FcUplinkRoles'), |
| 288 |
('/fabric/FcStorageRoles', 'FcStorageRoles'), |
| 289 |
('/fabric/ApplianceRoles', 'ApplianceRoles'), |
| 290 |
('/fabric/UplinkRoles', 'UplinkRoles'), |
| 291 |
('/fabric/FcoeUplinkRoles', 'FcoeUplinkRoles'), |
| 292 |
|
| 293 |
# Pin Groups |
| 294 |
('/fabric/LanPinGroups', 'LanPinGroups'), |
| 295 |
('/fabric/SanPinGroups', 'SanPinGroups'), |
| 296 |
] |
| 297 |
|
| 298 |
for api_path, result_key in resource_mappings: |
| 299 |
try: |
| 300 |
# Build query parameters |
| 301 |
query_params = { |
| 302 |
'$filter': f"PortPolicy.Moid eq '{port_policy_moid}'" |
| 303 |
} |
| 304 |
intersight.result['api_response'] = {} |
| 305 |
# Get the resources |
| 306 |
intersight.get_resource( |
| 307 |
resource_path=api_path, |
| 308 |
query_params=query_params, |
| 309 |
return_list=True |
| 310 |
) |
| 311 |
|
| 312 |
if intersight.result.get('api_response'): |
| 313 |
resources = intersight.result['api_response'] |
| 314 |
|
| 315 |
# Special handling for PortModes to separate FC port mode and breakout ports |
| 316 |
if result_key == 'PortModes': |
| 317 |
fc_port_modes = [] |
| 318 |
breakout_ports = [] |
| 319 |
|
| 320 |
for port_mode in resources: |
| 321 |
# Make a deep copy to avoid modifying original data |
| 322 |
port_mode_copy = port_mode.copy() |
| 323 |
|
| 324 |
# Breakout ports have CustomMode specified |
| 325 |
if port_mode_copy.get('CustomMode') != "FibreChannel": |
| 326 |
# Change PortIdStart and PortIdEnd to PortId to align the main module. |
| 327 |
if 'PortIdStart' in port_mode_copy: |
| 328 |
port_mode_copy['PortId'] = port_mode_copy['PortIdStart'] |
| 329 |
del port_mode_copy['PortIdStart'] |
| 330 |
if 'PortIdEnd' in port_mode_copy: |
| 331 |
del port_mode_copy['PortIdEnd'] |
| 332 |
breakout_ports.append(port_mode_copy) |
| 333 |
else: |
| 334 |
fc_port_modes.append(port_mode_copy) |
| 335 |
|
| 336 |
secondary_resources['FcPortModes'] = fc_port_modes |
| 337 |
secondary_resources['BreakoutPorts'] = breakout_ports |
| 338 |
else: |
| 339 |
secondary_resources[result_key] = resources |
| 340 |
|
| 341 |
except Exception as e: |
| 342 |
# Log the error but continue with other resources |
| 343 |
intersight.module.warn(f"Failed to retrieve {result_key} from {api_path}: {str(e)}") |
| 344 |
|
| 345 |
return secondary_resources |
| 346 |
|
| 347 |
|
| 348 |
def main(): |
| 349 |
argument_spec = intersight_argument_spec.copy() |
| 350 |
argument_spec.update( |
| 351 |
name=dict(type='str'), |
| 352 |
organization=dict(type='str') |
| 353 |
) |
| 354 |
|
| 355 |
module = AnsibleModule( |
| 356 |
argument_spec, |
| 357 |
supports_check_mode=True, |
| 358 |
) |
| 359 |
|
| 360 |
intersight = IntersightModule(module) |
| 361 |
|
| 362 |
# Build query parameters using the standard method |
| 363 |
query_params = intersight.set_query_params() |
| 364 |
|
| 365 |
try: |
| 366 |
# Get Port Policies |
| 367 |
intersight.get_resource( |
| 368 |
resource_path='/fabric/PortPolicies', |
| 369 |
query_params=query_params, |
| 370 |
return_list=True |
| 371 |
) |
| 372 |
|
| 373 |
port_policies = intersight.result.get('api_response', []) |
| 374 |
|
| 375 |
# Ensure port_policies is a list |
| 376 |
if isinstance(port_policies, dict): |
| 377 |
port_policies = [port_policies] |
| 378 |
elif not isinstance(port_policies, list): |
| 379 |
port_policies = [] |
| 380 |
|
| 381 |
# For each port policy, get all associated secondary resources |
| 382 |
for policy in port_policies: |
| 383 |
policy_moid = policy.get('Moid') |
| 384 |
if policy_moid: |
| 385 |
# Get all secondary resources for this policy |
| 386 |
secondary_resources = get_port_policy_secondary_resources(intersight, policy_moid) |
| 387 |
|
| 388 |
# Merge secondary resources directly into the policy data |
| 389 |
policy.update(secondary_resources) |
| 390 |
|
| 391 |
# Update the api_response with the enhanced policy data |
| 392 |
intersight.result['api_response'] = port_policies |
| 393 |
|
| 394 |
# Set count of policies found |
| 395 |
intersight.result['count'] = len(port_policies) |
| 396 |
|
| 397 |
except Exception as e: |
| 398 |
module.fail_json(msg=f"Failed to retrieve Port Policy information: {str(e)}") |
| 399 |
|
| 400 |
module.exit_json(**intersight.result) |
| 401 |
|
| 402 |
|
| 403 |
if __name__ == '__main__': |
| 404 |
main() |