| 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_vlan_policy_info |
| 16 |
short_description: Gather information about VLAN Policies and their VLANs in Cisco Intersight |
| 17 |
description: |
| 18 |
- Retrieve comprehensive information about VLAN Policies and their associated VLANs from L(Cisco Intersight,https://intersight.com). |
| 19 |
- Query policies by organization, policy name, or filter VLANs by name patterns. |
| 20 |
- Returns structured data combining policy metadata with detailed VLAN configurations. |
| 21 |
- Supports filtering by organization, policy name, and VLAN name. |
| 22 |
- If no filters are provided, all VLAN Policies and their VLANs will be returned. |
| 23 |
- Returns structured data with both policy information and associated VLAN details. |
| 24 |
extends_documentation_fragment: intersight |
| 25 |
options: |
| 26 |
organization: |
| 27 |
description: |
| 28 |
- The name of the organization to filter VLAN Policies by. |
| 29 |
- Use 'default' for the default organization. |
| 30 |
- When specified, only policies from this organization will be returned. |
| 31 |
type: str |
| 32 |
name: |
| 33 |
description: |
| 34 |
- The exact name of the VLAN Policy to retrieve information from. |
| 35 |
- When specified, only the matching policy and its VLANs will be returned. |
| 36 |
- Can be combined with vlan_name filter to get specific VLANs from a specific policy. |
| 37 |
type: str |
| 38 |
vlan_name: |
| 39 |
description: |
| 40 |
- Filter VLANs by name within the policies. |
| 41 |
- Can be used with or without policy name filtering. |
| 42 |
- Supports exact matching (e.g., "prod_100" will match only "prod_100"). |
| 43 |
- When combined with policy name, filters VLANs within that specific policy. |
| 44 |
type: str |
| 45 |
author: |
| 46 |
- Ron Gershburg (@rgershbu) |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = r''' |
| 50 |
# Basic Usage Examples |
| 51 |
- name: Fetch all VLAN Policies and their VLANs from all organizations |
| 52 |
cisco.intersight.intersight_vlan_policy_info: |
| 53 |
api_private_key: "{{ api_private_key }}" |
| 54 |
api_key_id: "{{ api_key_id }}" |
| 55 |
register: all_vlan_policies |
| 56 |
|
| 57 |
# Organization-specific Examples |
| 58 |
- name: Fetch all VLAN Policies from the default organization |
| 59 |
cisco.intersight.intersight_vlan_policy_info: |
| 60 |
api_private_key: "{{ api_private_key }}" |
| 61 |
api_key_id: "{{ api_key_id }}" |
| 62 |
organization: "default" |
| 63 |
register: default_org_policies |
| 64 |
|
| 65 |
- name: Fetch all VLAN Policies from a custom organization |
| 66 |
cisco.intersight.intersight_vlan_policy_info: |
| 67 |
api_private_key: "{{ api_private_key }}" |
| 68 |
api_key_id: "{{ api_key_id }}" |
| 69 |
organization: "Engineering" |
| 70 |
register: engineering_policies |
| 71 |
|
| 72 |
# Specific Policy Examples |
| 73 |
- name: Fetch a specific VLAN Policy by name with all its VLANs |
| 74 |
cisco.intersight.intersight_vlan_policy_info: |
| 75 |
api_private_key: "{{ api_private_key }}" |
| 76 |
api_key_id: "{{ api_key_id }}" |
| 77 |
name: "datacenter-vlan-policy" |
| 78 |
register: specific_policy |
| 79 |
|
| 80 |
- name: Display policy details |
| 81 |
debug: |
| 82 |
msg: "Policy {{ specific_policy.api_response.policy.Name }} has {{ specific_policy.api_response.vlans | length }} VLANs" |
| 83 |
|
| 84 |
# VLAN Filtering Examples |
| 85 |
- name: Find all policies containing a specific VLAN |
| 86 |
cisco.intersight.intersight_vlan_policy_info: |
| 87 |
api_private_key: "{{ api_private_key }}" |
| 88 |
api_key_id: "{{ api_key_id }}" |
| 89 |
vlan_name: "prod_100" |
| 90 |
register: policies_with_prod_100 |
| 91 |
|
| 92 |
- name: Find specific VLAN in a specific policy |
| 93 |
cisco.intersight.intersight_vlan_policy_info: |
| 94 |
api_private_key: "{{ api_private_key }}" |
| 95 |
api_key_id: "{{ api_key_id }}" |
| 96 |
name: "datacenter-vlan-policy" |
| 97 |
vlan_name: "mgmt_300" |
| 98 |
register: specific_vlan_in_policy |
| 99 |
''' |
| 100 |
|
| 101 |
RETURN = r''' |
| 102 |
api_response: |
| 103 |
description: |
| 104 |
- The API response containing policy and VLAN information. |
| 105 |
- Returns a dictionary when querying a single policy or no policies found. |
| 106 |
- Returns a list when multiple policies are found. |
| 107 |
returned: always |
| 108 |
type: dict |
| 109 |
contains: |
| 110 |
vlan_policy: |
| 111 |
description: VLAN policy information |
| 112 |
type: dict |
| 113 |
returned: always |
| 114 |
vlans: |
| 115 |
description: List of VLANs associated with the policy |
| 116 |
type: list |
| 117 |
returned: always |
| 118 |
sample: |
| 119 |
# Single policy response (when name parameter is used or only one policy found) |
| 120 |
vlan_policy: |
| 121 |
Name: "datacenter-vlan-policy" |
| 122 |
ObjectType: "fabric.EthNetworkPolicy" |
| 123 |
Moid: "12345678901234567890abcd" |
| 124 |
Description: "VLAN policy for datacenter infrastructure" |
| 125 |
Organization: |
| 126 |
Name: "default" |
| 127 |
ObjectType: "organization.Organization" |
| 128 |
Tags: |
| 129 |
- Key: "Site" |
| 130 |
Value: "DataCenter-A" |
| 131 |
- Key: "Environment" |
| 132 |
Value: "Production" |
| 133 |
vlans: |
| 134 |
- Name: "prod_100" |
| 135 |
ObjectType: "fabric.Vlan" |
| 136 |
Moid: "vlan12345678901234567890" |
| 137 |
VlanId: 100 |
| 138 |
AutoAllowOnUplinks: true |
| 139 |
IsNative: false |
| 140 |
SharingType: "None" |
| 141 |
MulticastPolicy: |
| 142 |
Name: "default-multicast-policy" |
| 143 |
ObjectType: "fabric.MulticastPolicy" |
| 144 |
- Name: "dmz_primary_50" |
| 145 |
ObjectType: "fabric.Vlan" |
| 146 |
Moid: "vlan09876543210987654321" |
| 147 |
VlanId: 50 |
| 148 |
AutoAllowOnUplinks: true |
| 149 |
IsNative: false |
| 150 |
SharingType: "Primary" |
| 151 |
PrimaryVlanId: 0 |
| 152 |
- Name: "dmz_isolated_51" |
| 153 |
ObjectType: "fabric.Vlan" |
| 154 |
Moid: "vlanabcdef1234567890123456" |
| 155 |
VlanId: 51 |
| 156 |
AutoAllowOnUplinks: true |
| 157 |
IsNative: false |
| 158 |
SharingType: "Isolated" |
| 159 |
PrimaryVlanId: 50 |
| 160 |
''' |
| 161 |
|
| 162 |
|
| 163 |
from ansible.module_utils.basic import AnsibleModule |
| 164 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec |
| 165 |
|
| 166 |
|
| 167 |
def get_vlans_for_policy(intersight, policy_moid, vlan_name_filter=None): |
| 168 |
""" |
| 169 |
Get all VLANs associated with a specific policy. |
| 170 |
|
| 171 |
Args: |
| 172 |
intersight: IntersightModule instance |
| 173 |
policy_moid: MOID of the policy to get VLANs for |
| 174 |
vlan_name_filter: Optional filter for VLAN names |
| 175 |
|
| 176 |
Returns: |
| 177 |
List of VLAN objects associated with the policy |
| 178 |
""" |
| 179 |
# Build filter for VLANs associated with this policy |
| 180 |
filter_conditions = [f"EthNetworkPolicy.Moid eq '{policy_moid}'"] |
| 181 |
|
| 182 |
# Add VLAN name filter if provided (exact match only) |
| 183 |
if vlan_name_filter: |
| 184 |
filter_conditions.append(f"Name eq '{vlan_name_filter}'") |
| 185 |
|
| 186 |
query_params = { |
| 187 |
'$filter': ' and '.join(filter_conditions) |
| 188 |
} |
| 189 |
|
| 190 |
# Reset api_response before the API call to avoid previous responses |
| 191 |
intersight.result['api_response'] = {} |
| 192 |
|
| 193 |
# Get VLANs for this policy |
| 194 |
intersight.get_resource( |
| 195 |
resource_path='/fabric/Vlans', |
| 196 |
query_params=query_params, |
| 197 |
return_list=True |
| 198 |
) |
| 199 |
|
| 200 |
# Capture the response immediately before it gets overwritten |
| 201 |
vlans_response = intersight.result.get('api_response', []) |
| 202 |
|
| 203 |
# Ensure we return a list |
| 204 |
if isinstance(vlans_response, list): |
| 205 |
return vlans_response |
| 206 |
elif isinstance(vlans_response, dict): |
| 207 |
return [vlans_response] |
| 208 |
else: |
| 209 |
return [] |
| 210 |
|
| 211 |
|
| 212 |
def main(): |
| 213 |
argument_spec = intersight_argument_spec.copy() |
| 214 |
argument_spec.update( |
| 215 |
organization=dict(type='str'), |
| 216 |
name=dict(type='str'), |
| 217 |
vlan_name=dict(type='str') |
| 218 |
) |
| 219 |
module = AnsibleModule( |
| 220 |
argument_spec, |
| 221 |
supports_check_mode=True, |
| 222 |
) |
| 223 |
|
| 224 |
intersight = IntersightModule(module) |
| 225 |
|
| 226 |
# Resource path used to fetch policy info |
| 227 |
resource_path = '/fabric/EthNetworkPolicies' |
| 228 |
|
| 229 |
# Get query parameters for policies |
| 230 |
query_params = intersight.set_query_params() |
| 231 |
|
| 232 |
# Reset api_response before the API call to avoid previous responses |
| 233 |
intersight.result['api_response'] = {} |
| 234 |
|
| 235 |
# Get VLAN policies |
| 236 |
intersight.get_resource( |
| 237 |
resource_path=resource_path, |
| 238 |
query_params=query_params, |
| 239 |
return_list=True |
| 240 |
) |
| 241 |
|
| 242 |
policies = intersight.result.get('api_response', []) |
| 243 |
vlan_name_filter = module.params.get('vlan_name') |
| 244 |
|
| 245 |
# Build structured response |
| 246 |
structured_results = [] |
| 247 |
|
| 248 |
# Ensure policies is always a list for iteration, even if a single dict is returned |
| 249 |
if isinstance(policies, dict): |
| 250 |
policies = [policies] |
| 251 |
elif not isinstance(policies, list): |
| 252 |
policies = [] |
| 253 |
|
| 254 |
for policy in policies: |
| 255 |
policy_moid = policy.get('Moid') |
| 256 |
if not policy_moid: |
| 257 |
continue |
| 258 |
|
| 259 |
# Get VLANs for this policy |
| 260 |
vlans = get_vlans_for_policy(intersight, policy_moid, vlan_name_filter) |
| 261 |
|
| 262 |
# Create structured response for this policy |
| 263 |
policy_result = { |
| 264 |
'vlan_policy': policy, |
| 265 |
'vlans': vlans |
| 266 |
} |
| 267 |
|
| 268 |
structured_results.append(policy_result) |
| 269 |
|
| 270 |
# Create final response structure |
| 271 |
final_api_response = None |
| 272 |
|
| 273 |
# Set final response based on number of policies found |
| 274 |
if len(structured_results) == 1: |
| 275 |
# Single policy - return as dict |
| 276 |
final_api_response = structured_results[0] |
| 277 |
elif len(structured_results) > 1: |
| 278 |
# Multiple policies - return as list |
| 279 |
final_api_response = structured_results |
| 280 |
else: |
| 281 |
# No policies found - return empty structure |
| 282 |
final_api_response = { |
| 283 |
'vlan_policy': {}, |
| 284 |
'vlans': [] |
| 285 |
} |
| 286 |
|
| 287 |
# Use intersight.result and update api_response directly |
| 288 |
intersight.result['api_response'] = final_api_response |
| 289 |
final_result = intersight.result |
| 290 |
|
| 291 |
module.exit_json(**final_result) |
| 292 |
|
| 293 |
|
| 294 |
if __name__ == '__main__': |
| 295 |
main() |