Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_physical_interface.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Anvitha Jain (@anvjain) <[email protected]>
5 # Copyright: (c) 2025, Samita Bhattacharjee (@samiib) <[email protected]>
6
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: ndo_physical_interface
18 short_description: Manage Physical Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
19 description:
20 - Manage Physical Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
21 - This module is only supported on ND v3.2 (NDO v4.4) and later.
22 author:
23 - Anvitha Jain (@anvjain)
24 options:
25 template:
26 description:
27 - The name of the template.
28 - The template must be a fabric resource policy template.
29 type: str
30 required: true
31 name:
32 description:
33 - The name of the Physical Interface.
34 type: str
35 aliases: [ physical_interface ]
36 uuid:
37 description:
38 - The UUID of the Physical Interface.
39 - This parameter is required when the O(name) needs to be updated.
40 type: str
41 aliases: [ physical_interface_uuid ]
42 description:
43 description:
44 - The description of the Physical Interface.
45 type: str
46 nodes:
47 description:
48 - The node IDs where the Physical Interface policy will be deployed.
49 - Each element can either be a single node ID or a range of IDs.
50 type: list
51 elements: str
52 interfaces:
53 description:
54 - The interface names where the policy will be deployed.
55 - The old O(interfaces) will be replaced with the new O(interfaces) during an update.
56 type: list
57 elements: str
58 physical_interface_type:
59 description:
60 - The type of the interface policy group.
61 type: str
62 choices: [ physical, breakout ]
63 physical_policy_uuid:
64 description:
65 - The UUID of the Interface Setting Policy.
66 - This is only required when creating a new Interface Setting Policy.
67 - This parameter is required when O(physical_interface_type) is C(physical).
68 - This parameter can be used instead of O(physical_policy).
69 type: str
70 aliases: [ policy_uuid, interface_policy_uuid , interface_policy_group_uuid, interface_setting_uuid]
71 physical_policy:
72 description:
73 - The interface group policy required for physical Interface Setting Policy.
74 - This parameter is required when O(physical_interface_type) is C(physical).
75 - This parameter can be used instead of O(physical_policy_uuid).
76 type: dict
77 suboptions:
78 name:
79 description:
80 - The name of the Interface Setting Policy.
81 type: str
82 template:
83 description:
84 - The name of the template in which is referred the Interface Setting Policy.
85 type: str
86 aliases: [ policy, interface_policy, interface_policy_group, interface_setting ]
87 breakout_mode:
88 description:
89 - Breakout mode enables breaking down an ethernet port into multiple low-speed ports.
90 - This parameter is available only when O(physical_interface_type) is C(breakout).
91 - The default value is C(4x10G).
92 type: str
93 choices: [ 4x10G, 4x25G, 4x100G ]
94 interface_descriptions:
95 description:
96 - The interface settings defined in the interface settings policy will be applied to the interfaces on the node IDs configured in C(nodes).
97 - This parameter when set to an empty list during an update will clear all the existing interface descriptions.
98 - The API will trigger an error when there are duplicate interface IDs in the list.
99 type: list
100 elements: dict
101 suboptions:
102 interface_id:
103 description:
104 - The interface ID.
105 type: str
106 description:
107 description:
108 - The description of the interface.
109 type: str
110 state:
111 description:
112 - Use C(absent) for removing.
113 - Use C(query) for listing an object or multiple objects.
114 - Use C(present) for creating or updating.
115 type: str
116 choices: [ absent, query, present ]
117 default: query
118 notes:
119 - The O(template) must exist before using this module in your playbook.
120 Use M(cisco.mso.ndo_template) to create the Tenant template.
121 - The O(physical_policy) must exist before using this module in your playbook.
122 Use M(cisco.mso.ndo_interface_setting) to create the Interface Setting Policy.
123 - The O(physical_policy_uuid) must exist before using this module in your playbook.
124 Use M(cisco.mso.ndo_interface_setting) to create the Interface Setting Policy UUID.
125 seealso:
126 - module: cisco.mso.ndo_template
127 extends_documentation_fragment: cisco.mso.modules
128 """
129
130 EXAMPLES = r"""
131 - name: Create a Physical Interface with physical_interface_type set to physical
132 cisco.mso.ndo_physical_interface:
133 host: mso_host
134 username: admin
135 password: SomeSecretPassword
136 template: ansible_test_template
137 name: ansible_test_physical_interface_physical
138 description: "Physical Interface for Ansible Test"
139 nodes: [101, 102, 103-105]
140 interfaces: "1/1"
141 physical_interface_type: physical
142 physical_policy: ansible_test_interface_setting_policy_uuid
143 state: present
144 register: create_physical_interface_type_physical
145
146 - name: Create a Physical Interface with physical_interface_type set to breakout
147 cisco.mso.ndo_physical_interface:
148 host: mso_host
149 username: admin
150 password: SomeSecretPassword
151 template: ansible_test_template
152 name: ansible_test_physical_interface_breakout
153 description: "breakout interface for Ansible Test"
154 nodes: [101, 102, 103-105]
155 interfaces: "1/1"
156 physical_interface_type: breakout
157 breakout_mode: 4x25G
158 interface_descriptions:
159 - interface_id: "1/1"
160 description: "Interface description for 1/1"
161 state: present
162 register: create_physical_interface_type_breakout
163
164 - name: Query all physical interfaces
165 cisco.mso.ndo_physical_interface:
166 host: mso_host
167 username: admin
168 password: SomeSecretPassword
169 template: ansible_test_template
170 state: query
171 register: query_all
172
173 - name: Query a specific Physical Interface with name
174 cisco.mso.ndo_physical_interface:
175 host: mso_host
176 username: admin
177 password: SomeSecretPassword
178 template: ansible_test_template
179 name: ansible_test_physical_interface_physical
180 state: query
181 register: query_one_name
182
183 - name: Query a specific Physical Interface with UUID
184 cisco.mso.ndo_physical_interface:
185 host: mso_host
186 username: admin
187 password: SomeSecretPassword
188 template: ansible_test_template
189 uuid: "{{ create_physical_interface_type_breakout.current.uuid }}"
190 state: query
191 register: query_one_uuid
192
193 - name: Delete a Physical Interface with name
194 cisco.mso.ndo_physical_interface:
195 host: mso_host
196 username: admin
197 password: SomeSecretPassword
198 template: ansible_test_template
199 name: ansible_test_physical_interface_physical
200 state: absent
201
202 - name: Delete a Physical Interface with UUID
203 cisco.mso.ndo_physical_interface:
204 host: mso_host
205 username: admin
206 password: SomeSecretPassword
207 template: ansible_test_template
208 uuid: "{{ query_one_uuid.current.uuid }}"
209 state: absent
210 """
211
212 RETURN = r"""
213 """
214
215
216 from ansible.module_utils.basic import AnsibleModule
217 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
218 MSOModule,
219 mso_argument_spec,
220 format_interface_descriptions,
221 )
222 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
223 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
224 import copy
225
226
227 def main():
228 argument_spec = mso_argument_spec()
229 argument_spec.update(
230 dict(
231 template=dict(type="str", required=True),
232 name=dict(type="str", aliases=["physical_interface"]),
233 uuid=dict(type="str", aliases=["physical_interface_uuid"]),
234 description=dict(type="str"),
235 nodes=dict(type="list", elements="str"),
236 interfaces=dict(type="list", elements="str"),
237 physical_interface_type=dict(type="str", choices=["physical", "breakout"]),
238 physical_policy_uuid=dict(type="str", aliases=["policy_uuid", "interface_policy_uuid", "interface_policy_group_uuid", "interface_setting_uuid"]),
239 physical_policy=dict(
240 type="dict",
241 options=dict(
242 name=dict(type="str"),
243 template=dict(type="str"),
244 ),
245 aliases=["policy", "interface_policy", "interface_policy_group", "interface_setting"],
246 ),
247 breakout_mode=dict(type="str", choices=["4x10G", "4x25G", "4x100G"]),
248 interface_descriptions=dict(
249 type="list",
250 elements="dict",
251 options=dict(
252 interface_id=dict(type="str"),
253 description=dict(type="str"),
254 ),
255 ),
256 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
257 )
258 )
259
260 module = AnsibleModule(
261 argument_spec=argument_spec,
262 supports_check_mode=True,
263 required_if=[
264 ["state", "present", ["name", "uuid"], True],
265 ["state", "absent", ["name", "uuid"], True],
266 ],
267 mutually_exclusive=[("physical_policy", "breakout_mode"), ("physical_policy", "physical_policy_uuid")],
268 )
269
270 mso = MSOModule(module)
271
272 template = module.params.get("template")
273 name = module.params.get("name")
274 uuid = module.params.get("uuid")
275 description = module.params.get("description")
276 nodes = module.params.get("nodes")
277 interfaces = module.params.get("interfaces")
278 if interfaces:
279 interfaces = ",".join(interfaces)
280 physical_interface_type = module.params.get("physical_interface_type")
281 physical_policy_uuid = module.params.get("physical_policy_uuid")
282 physical_policy = module.params.get("physical_policy")
283 breakout_mode = module.params.get("breakout_mode")
284 interface_descriptions = module.params.get("interface_descriptions")
285 state = module.params.get("state")
286
287 ops = []
288 match = None
289 physical_interface_attrs_path = None
290
291 mso_template = MSOTemplate(mso, "fabric_resource", template)
292 mso_template.validate_template("fabricResource")
293 object_description = "Physical Interface Profile"
294 path = "/fabricResourceTemplate/template/interfaceProfiles"
295
296 existing_physical_interfaces = mso_template.template.get("fabricResourceTemplate", {}).get("template", {}).get("interfaceProfiles") or []
297
298 if name or uuid:
299 match = mso_template.get_object_by_key_value_pairs(
300 object_description,
301 existing_physical_interfaces,
302 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
303 )
304 if match:
305 physical_interface_attrs_path = "{0}/{1}".format(path, match.index)
306 mso.existing = mso.previous = copy.deepcopy(match.details)
307 else:
308 mso.existing = mso.previous = existing_physical_interfaces
309
310 if state == "present":
311 if uuid and not mso.existing:
312 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
313
314 if physical_policy and not physical_policy_uuid:
315 fabric_policy_template = MSOTemplate(mso, "fabric_policy", physical_policy.get("template"))
316 fabric_policy_template.validate_template("fabricPolicy")
317 physical_policy_uuid = fabric_policy_template.get_interface_policy_group_uuid(physical_policy.get("name"))
318
319 mso_values = dict(
320 name=name,
321 description=description,
322 nodes=nodes,
323 interfaces=interfaces,
324 policyGroupType=physical_interface_type,
325 )
326
327 if physical_interface_type == "physical" and physical_policy_uuid:
328 mso_values["policy"] = physical_policy_uuid
329
330 if physical_interface_type == "breakout" and breakout_mode:
331 mso_values["breakoutMode"] = breakout_mode
332
333 if interface_descriptions:
334 mso_values["interfaceDescriptions"] = format_interface_descriptions(mso, interface_descriptions, "")
335
336 if mso.existing and match:
337 proposed_payload = copy.deepcopy(mso.existing)
338 mso_values_remove = list()
339
340 if physical_interface_type and match.details.get("policyGroupType") != physical_interface_type:
341 mso.fail_json(msg="ERROR: Physical Interface type cannot be changed.")
342
343 if interface_descriptions == [] and proposed_payload.get("interfaceDescriptions"):
344 mso_values_remove.append("interfaceDescriptions")
345
346 append_update_ops_data(ops, match.details, physical_interface_attrs_path, mso_values, mso_values_remove)
347 mso.sanitize(match.details, collate=True)
348
349 else:
350 if not nodes:
351 mso.fail_json(msg=("ERROR: Missing 'nodes' for creating a Physical Interface."))
352
353 if not physical_interface_type:
354 mso.fail_json(msg=("ERROR: Missing Physical Interface type for creating a Physical Interface."))
355
356 mso.sanitize(mso_values)
357 ops.append(dict(op="add", path="{0}/-".format(path), value=mso.sent))
358
359 elif state == "absent":
360 if match:
361 ops.append(dict(op="remove", path=physical_interface_attrs_path))
362
363 if not module.check_mode and ops:
364 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
365 physical_interfaces = response.get("fabricResourceTemplate", {}).get("template", {}).get("interfaceProfiles") or []
366 match = mso_template.get_object_by_key_value_pairs(object_description, physical_interfaces, [KVPair("uuid", uuid) if uuid else KVPair("name", name)])
367 if match:
368 mso.existing = match.details # When the state is present
369 else:
370 mso.existing = {} # When the state is absent
371 elif module.check_mode and state != "query": # When the state is present/absent with check mode
372 mso.existing = mso.proposed if state == "present" else {}
373
374 mso.exit_json()
375
376
377 if __name__ == "__main__":
378 main()