Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_vsan_policy_info.py

raw
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_info
16 short_description: Gather information about VSAN Policies and their VSANs in Cisco Intersight
17 description:
18 - Retrieve information about VSAN (Virtual Storage Area Network) Policies and their associated VSANs from L(Cisco Intersight,https://intersight.com).
19 - Query policies by organization, policy name, or filter VSANs by name patterns.
20 - Returns structured data combining policy metadata with detailed VSAN configurations.
21 - Supports filtering by organization, policy name, and VSAN name.
22 - If no filters are provided, all VSAN Policies and their VSANs will be returned.
23 - Returns structured data with both policy information and associated VSAN details.
24 extends_documentation_fragment: intersight
25 options:
26 organization:
27 description:
28 - The name of the organization to filter VSAN 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 VSAN Policy to retrieve information from.
35 - When specified, only the matching policy and its VSANs will be returned.
36 - Can be combined with vsan_name filter to get specific VSANs from a specific policy.
37 type: str
38 vsan_name:
39 description:
40 - Filter VSANs by name within the policies.
41 - Can be used with or without policy name filtering.
42 - Supports exact matching (e.g., "vsan_uplink_100" will match only "vsan_uplink_100").
43 - When combined with policy name, filters VSANs 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 VSAN Policies and their VSANs from all organizations
52 cisco.intersight.intersight_vsan_policy_info:
53 api_private_key: "{{ api_private_key }}"
54 api_key_id: "{{ api_key_id }}"
55 register: all_vsan_policies
56
57 # Organization-specific Examples
58 - name: Fetch all VSAN Policies from the default organization
59 cisco.intersight.intersight_vsan_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 VSAN Policies from a custom organization
66 cisco.intersight.intersight_vsan_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 VSAN Policy by name with all its VSANs
74 cisco.intersight.intersight_vsan_policy_info:
75 api_private_key: "{{ api_private_key }}"
76 api_key_id: "{{ api_key_id }}"
77 name: "datacenter-vsan-policy"
78 register: specific_policy
79
80 # VSAN Filtering Examples
81 - name: Find all policies containing a specific VSAN
82 cisco.intersight.intersight_vsan_policy_info:
83 api_private_key: "{{ api_private_key }}"
84 api_key_id: "{{ api_key_id }}"
85 vsan_name: "vsan_uplink_100"
86 register: policies_with_vsan_100
87
88 - name: Find specific VSAN in a specific policy
89 cisco.intersight.intersight_vsan_policy_info:
90 api_private_key: "{{ api_private_key }}"
91 api_key_id: "{{ api_key_id }}"
92 name: "datacenter-vsan-policy"
93 vsan_name: "vsan_storage_200"
94 register: specific_vsan_in_policy
95 '''
96
97 RETURN = r'''
98 api_response:
99 description:
100 - The API response containing policy and VSAN information.
101 - Returns a dictionary when querying a single policy or no policies found.
102 - Returns a list when multiple policies are found.
103 returned: always
104 type: dict
105 sample:
106 # Single policy response (when name parameter is used or only one policy found)
107 Name: "datacenter-vsan-policy"
108 ObjectType: "fabric.FcNetworkPolicy"
109 Moid: "12345678901234567890abcd"
110 Description: "VSAN policy for datacenter SAN infrastructure"
111 EnableTrunking: false
112 Organization:
113 Name: "default"
114 ObjectType: "organization.Organization"
115 Tags:
116 - Key: "Site"
117 Value: "DataCenter-A"
118 - Key: "Environment"
119 Value: "Production"
120 vsans:
121 - Name: "vsan_uplink_100"
122 ObjectType: "fabric.Vsan"
123 Moid: "vsan12345678901234567890"
124 VsanId: 100
125 FcoeVlan: 100
126 VsanScope: "Uplink"
127 FcNetworkPolicy:
128 Moid: "12345678901234567890abcd"
129 ObjectType: "fabric.FcNetworkPolicy"
130 - Name: "vsan_storage_200"
131 ObjectType: "fabric.Vsan"
132 Moid: "vsan09876543210987654321"
133 VsanId: 200
134 FcoeVlan: 200
135 VsanScope: "Storage"
136 FcNetworkPolicy:
137 Moid: "12345678901234567890abcd"
138 ObjectType: "fabric.FcNetworkPolicy"
139 '''
140
141
142 from ansible.module_utils.basic import AnsibleModule
143 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
144
145
146 def get_vsans_for_policy(intersight, policy_moid, vsan_name_filter=None):
147 """
148 Get all VSANs associated with a specific policy.
149
150 Args:
151 intersight: IntersightModule instance
152 policy_moid: MOID of the policy to get VSANs for
153 vsan_name_filter: Optional filter for VSAN names
154
155 Returns:
156 List of VSAN objects associated with the policy
157 """
158 # Build filter for VSANs associated with this policy
159 filter_conditions = [f"FcNetworkPolicy.Moid eq '{policy_moid}'"]
160
161 # Add VSAN name filter if provided (exact match only)
162 if vsan_name_filter:
163 filter_conditions.append(f"Name eq '{vsan_name_filter}'")
164
165 query_params = {
166 '$filter': ' and '.join(filter_conditions)
167 }
168
169 # Reset api_response before the API call to avoid previous responses
170 intersight.result['api_response'] = {}
171
172 # Get VSANs for this policy
173 intersight.get_resource(
174 resource_path='/fabric/Vsans',
175 query_params=query_params,
176 return_list=True
177 )
178
179 # Capture the response immediately before it gets overwritten
180 vsans_response = intersight.result.get('api_response', [])
181
182 # Ensure we return a list
183 if isinstance(vsans_response, list):
184 return vsans_response
185 elif isinstance(vsans_response, dict):
186 return [vsans_response]
187 else:
188 return []
189
190
191 def main():
192 argument_spec = intersight_argument_spec.copy()
193 argument_spec.update(
194 organization=dict(type='str'),
195 name=dict(type='str'),
196 vsan_name=dict(type='str')
197 )
198 module = AnsibleModule(
199 argument_spec,
200 supports_check_mode=True,
201 )
202
203 intersight = IntersightModule(module)
204
205 # Resource path used to fetch policy info
206 resource_path = '/fabric/FcNetworkPolicies'
207
208 # Get query parameters for policies
209 query_params = intersight.set_query_params()
210
211 # Reset api_response before the API call to avoid previous responses
212 intersight.result['api_response'] = {}
213
214 # Get VSAN policies
215 intersight.get_resource(
216 resource_path=resource_path,
217 query_params=query_params,
218 return_list=True
219 )
220
221 policies = intersight.result.get('api_response', [])
222 vsan_name_filter = module.params.get('vsan_name')
223
224 # Build structured response
225 structured_results = []
226
227 # Ensure policies is always a list for iteration, even if a single dict is returned
228 if isinstance(policies, dict):
229 policies = [policies]
230 elif not isinstance(policies, list):
231 policies = []
232
233 for policy in policies:
234 policy_moid = policy.get('Moid')
235 if not policy_moid:
236 continue
237
238 # Get VSANs for this policy
239 vsans = get_vsans_for_policy(intersight, policy_moid, vsan_name_filter)
240
241 # Embed VSANs in the policy response
242 if vsans:
243 policy['vsans'] = vsans
244
245 structured_results.append(policy)
246
247 # Create final response structure
248 final_api_response = None
249
250 # Set final response based on number of policies found
251 if len(structured_results) == 1:
252 # Single policy - return as dict
253 final_api_response = structured_results[0]
254 elif len(structured_results) > 1:
255 # Multiple policies - return as list
256 final_api_response = structured_results
257 else:
258 # No policies found - return empty structure
259 final_api_response = {}
260
261 # Use intersight.result and update api_response directly
262 intersight.result['api_response'] = final_api_response
263 final_result = intersight.result
264
265 module.exit_json(**final_result)
266
267
268 if __name__ == '__main__':
269 main()