Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Gaspard Micol (@gmicol) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18 DOCUMENTATION = r"""
19 ---
20 module: ndo_virtual_port_channel_interface
21 short_description: Manage Virtual Port Channel Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
22 description:
23 - Manage Virtual Port Channel Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
24 - This module is only supported on ND v3.2 (NDO v4.4) and later.
25 author:
26 - Gaspard Micol (@gmicol)
27 options:
28 template:
29 description:
30 - The name of the Fabric Resource template.
31 - This parameter or O(template_id) is required.
32 type: str
33 aliases: [ fabric_resource_template ]
34 template_id:
35 description:
36 - The ID of the Fabric Policy template.
37 - This parameter or O(template) is required.
38 type: str
39 aliases: [ fabric_resource_template_id ]
40 name:
41 description:
42 - The name of the Virtual Port Channel Interface.
43 type: str
44 aliases: [ virtual_port_channel_interface, virtual_port_channel, vpc ]
45 uuid:
46 description:
47 - The UUID of the Virtual Port Channel Interface.
48 - This parameter can be used instead of O(virtual_port_channel_interface)
49 when an existing Virtual Port Channel Interface is updated.
50 - This parameter is required when parameter O(name) is updated.
51 type: str
52 aliases: [ virtual_port_channel_interface_uuid, virtual_port_channel_uuid, vpc_uuid ]
53 description:
54 description:
55 - The description of the Virtual Port Channel Interface.
56 type: str
57 node_1:
58 description:
59 - The first node ID.
60 type: str
61 node_2:
62 description:
63 - The second node ID.
64 type: str
65 interfaces_node_1:
66 description:
67 - The list of used Interface IDs for the first node.
68 - Ranges of Interface IDs can be used.
69 - This parameter is required when creating a new Virtual Port Channel Interface.
70 type: list
71 elements: str
72 aliases: [ interfaces_1, members_1 ]
73 interfaces_node_2:
74 description:
75 - The list of used Interface IDs for the second node.
76 - Ranges of Interface IDs can be used.
77 - This parameter is required when creating a new Virtual Port Channel Interface.
78 - If O(interfaces_node_2=[mirror]) and O(interfaces_node_1) is clearly defined,
79 the interfaces of O(interfaces_node_1) will be mirrored in O(interfaces_node_2).
80 type: list
81 elements: str
82 aliases: [ interfaces_2, members_2 ]
83 interface_policy_group_uuid:
84 description:
85 - The UUID of the Port Channel Interface Policy Group.
86 - This parameter is required when creating a new Virtual Port Channel Interface.
87 type: str
88 aliases: [ policy_uuid, interface_policy_uuid, interface_setting_uuid ]
89 interface_policy_group:
90 description:
91 - The Port Channel Interface Policy Group.
92 - This parameter can be used instead of O(interface_policy_group_uuid).
93 type: dict
94 suboptions:
95 name:
96 description:
97 - The name of the Interface Policy Group.
98 type: str
99 required: true
100 template:
101 description:
102 - The name of the template in which the Interface Policy Group has been created.
103 type: str
104 required: true
105 aliases: [ policy, interface_policy, interface_setting ]
106 interface_descriptions:
107 description:
108 - The list of interface descriptions of the Virtual Port Channel Interface.
109 - Providing a new list of O(interface_descriptions) will completely
110 replace an existing one from the Virtual Port Channel Interface.
111 - Providing an empty list will remove the O(interface_descriptions=[])
112 from the Virtual Port Channel Interface.
113 type: list
114 elements: dict
115 suboptions:
116 node:
117 description:
118 - The node ID.
119 type: str
120 required: true
121 interface_id:
122 description:
123 - The interface ID or a range of interface IDs.
124 - Using a range of interface IDs will
125 apply the same O(interface_descriptions.description) for every ID in range.
126 type: str
127 required: true
128 description:
129 description:
130 - The description of the interface or group of interfaces.
131 type: str
132 state:
133 description:
134 - Use C(absent) for removing.
135 - Use C(query) for listing an object or multiple objects.
136 - Use C(present) for creating or updating.
137 type: str
138 choices: [ absent, query, present ]
139 default: query
140 extends_documentation_fragment: cisco.mso.modules
141 notes:
142 - The O(template) must exist before using this module in your playbook.
143 Use M(cisco.mso.ndo_template) to create the Fabric Resource template.
144 - The O(interface_policy_group) must exist before using this module in your playbook.
145 Use M(cisco.mso.ndo_interface_setting) to create the Interface Policy Group of type Port Channel.
146 seealso:
147 - module: cisco.mso.ndo_template
148 - module: cisco.mso.ndo_interface_setting
149 """
150
151 EXAMPLES = r"""
152 - name: Create a new Virtual Port Channel Interface
153 cisco.mso.ndo_virtual_port_channel_interface:
154 host: mso_host
155 username: admin
156 password: SomeSecretPassword
157 template: ansible_fabric_resource_template
158 description: My Ansible Port Channel
159 name: ansible_virtual_port_channel_interface
160 node_1: 101
161 node_2: 102
162 interfaces_node_1:
163 - 1/1
164 - 1/10-11
165 interfaces_node_2:
166 - 1/2
167 interface_policy_group:
168 name: ansible_policy_group
169 template: ansible_fabric_policy_template
170 interface_descriptions:
171 - node: 101
172 interface_id: 1/1
173 description: My single Ansible Interface for first node
174 - node: 101
175 interface_id: 1/10-11
176 description: My group of Ansible Interface for first node
177 - node: 102
178 interface_id: 1/2
179 description: My single Ansible Interface for second node
180 state: present
181 register: virtual_port_channel_interface_1
182
183 - name: Update a Virtual Port Channel Interface's interfaces and their descriptions
184 cisco.mso.ndo_virtual_port_channel_interface:
185 host: mso_host
186 username: admin
187 password: SomeSecretPassword
188 description: My Ansible Port Channel
189 name: ansible_virtual_port_channel_interface
190 node_1: 101
191 node_2: 102
192 interfaces_node_1:
193 - 1/1
194 - 1/5-7
195 interfaces_node_2:
196 - 1/1-2
197 interface_descriptions:
198 - node: 101
199 interface_id: 1/1
200 description: My single unchanged Ansible Interface for first node
201 - node: 101
202 interface_id: 1/5-7
203 description: My new group of Ansible Interface for first node
204 - node: 102
205 interface_id: 1/1-2
206 description: My new group of Ansible Interfaces for second node
207 state: present
208
209 - name: Update a Virtual Port Channel Interface by mirroring node 1 and node 2 interfaces
210 cisco.mso.ndo_virtual_port_channel_interface:
211 host: mso_host
212 username: admin
213 password: SomeSecretPassword
214 description: My Ansible Port Channel
215 name: ansible_virtual_port_channel_interface
216 node_1: 101
217 node_2: 102
218 interfaces_node_1:
219 - 1/1-9
220 - 1/11-15
221 interfaces_node_2: mirror
222 state: present
223
224 - name: Update a Virtual Port Channel Interface's name with UUID
225 cisco.mso.ndo_virtual_port_channel_interface:
226 host: mso_host
227 username: admin
228 password: SomeSecretPassword
229 template: ansible_fabric_resource_template
230 name: ansible_virtual_port_channel_interface_changed
231 uuid: "{{ virtual_port_channel_interface_1.current.uuid }}"
232 state: present
233
234 - name: Query a Virtual Port Channel Interface with name
235 cisco.mso.ndo_virtual_port_channel_interface:
236 host: mso_host
237 username: admin
238 password: SomeSecretPassword
239 template: ansible_fabric_resource_template
240 name: ansible_virtual_port_channel_interface_changed
241 state: query
242 register: query_name
243
244 - name: Query a Virtual Port Channel Interface with UUID
245 cisco.mso.ndo_virtual_port_channel_interface:
246 host: mso_host
247 username: admin
248 password: SomeSecretPassword
249 template: ansible_fabric_resource_template
250 uuid: "{{ virtual_port_channel_interface_1.current.uuid }}"
251 state: query
252 register: query_uuid
253
254 - name: Query all Virtual Port Channel Interfaces in a Fabric Resource Template
255 cisco.mso.ndo_virtual_port_channel_interface:
256 host: mso_host
257 username: admin
258 password: SomeSecretPassword
259 template: ansible_fabric_resource_template
260 state: query
261 register: query_all
262
263 - name: Delete a Virtual Port Channel Interface with name
264 cisco.mso.ndo_virtual_port_channel_interface:
265 host: mso_host
266 username: admin
267 password: SomeSecretPassword
268 template: ansible_fabric_resource_template
269 name: ansible_virtual_port_channel_interface_changed
270 state: absent
271
272 - name: Delete a Virtual Port Channel Interface with UUID
273 cisco.mso.ndo_virtual_port_channel_interface:
274 host: mso_host
275 username: admin
276 password: SomeSecretPassword
277 template: ansible_fabric_resource_template
278 uuid: "{{ virtual_port_channel_interface_1.current.uuid }}"
279 state: absent
280 """
281
282 RETURN = r"""
283 """
284
285 import copy
286 from ansible.module_utils.basic import AnsibleModule
287 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
288 MSOModule,
289 mso_argument_spec,
290 format_interface_descriptions,
291 )
292 from ansible_collections.cisco.mso.plugins.module_utils.template import (
293 MSOTemplate,
294 KVPair,
295 )
296 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
297
298
299 def main():
300 argument_spec = mso_argument_spec()
301 argument_spec.update(
302 template=dict(type="str", aliases=["fabric_resource_template"]),
303 template_id=dict(type="str", aliases=["fabric_resource_template_id"]),
304 name=dict(type="str", aliases=["virtual_port_channel_interface", "virtual_port_channel", "vpc"]),
305 uuid=dict(type="str", aliases=["virtual_port_channel_interface_uuid", "virtual_port_channel_uuid", "vpc_uuid"]),
306 description=dict(type="str"),
307 node_1=dict(type="str"),
308 node_2=dict(type="str"),
309 interfaces_node_1=dict(type="list", elements="str", aliases=["interfaces_1", "members_1"]),
310 interfaces_node_2=dict(type="list", elements="str", aliases=["interfaces_2", "members_2"]),
311 interface_policy_group=dict(
312 type="dict",
313 options=dict(
314 name=dict(type="str", required=True),
315 template=dict(type="str", required=True),
316 ),
317 aliases=["policy", "interface_policy", "interface_setting"],
318 ),
319 interface_policy_group_uuid=dict(type="str", aliases=["policy_uuid", "interface_policy_uuid", "interface_setting_uuid"]),
320 interface_descriptions=dict(
321 type="list",
322 elements="dict",
323 options=dict(
324 node=dict(type="str", required=True),
325 interface_id=dict(type="str", required=True),
326 description=dict(type="str"),
327 ),
328 ),
329 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
330 )
331
332 module = AnsibleModule(
333 argument_spec=argument_spec,
334 supports_check_mode=True,
335 required_if=[
336 ["state", "absent", ["name", "uuid"], True],
337 ["state", "present", ["name", "uuid"], True],
338 ],
339 required_one_of=[
340 ("template", "template_id"),
341 ],
342 mutually_exclusive=[
343 ("template", "template_id"),
344 ("interface_policy_group", "interface_policy_group_uuid"),
345 ],
346 )
347
348 mso = MSOModule(module)
349
350 template = module.params.get("template")
351 template_id = module.params.get("template_id")
352 name = module.params.get("name")
353 uuid = module.params.get("uuid")
354 description = module.params.get("description")
355 node_1 = module.params.get("node_1")
356 node_2 = module.params.get("node_2")
357 interfaces_node_1 = module.params.get("interfaces_node_1")
358 if isinstance(interfaces_node_1, list):
359 interfaces_node_1 = ",".join(interfaces_node_1)
360 interfaces_node_2 = module.params.get("interfaces_node_2")
361 if isinstance(interfaces_node_2, list):
362 if interfaces_node_2[0] == "mirror" and interfaces_node_1:
363 interfaces_node_2 = interfaces_node_1
364 else:
365 interfaces_node_2 = ",".join(interfaces_node_2)
366 interface_policy_group = module.params.get("interface_policy_group")
367 interface_policy_group_uuid = module.params.get("interface_policy_group_uuid")
368 interface_descriptions = module.params.get("interface_descriptions")
369 state = module.params.get("state")
370
371 match = None
372 ops = []
373
374 mso_template = MSOTemplate(mso, "fabric_resource", template, template_id)
375 mso_template.validate_template("fabricResource")
376
377 existing_virtual_port_channel_interfaces = mso_template.template.get("fabricResourceTemplate", {}).get("template", {}).get("virtualPortChannels", [])
378 object_description = "Virtual Port Channel Interface"
379
380 if state in ["query", "absent"] and not existing_virtual_port_channel_interfaces:
381 mso.exit_json()
382 elif state == "query" and not (name or uuid):
383 mso.existing = existing_virtual_port_channel_interfaces
384 elif existing_virtual_port_channel_interfaces and (name or uuid):
385 match = mso_template.get_object_by_key_value_pairs(
386 object_description,
387 existing_virtual_port_channel_interfaces,
388 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
389 )
390 if match:
391 mso.existing = mso.previous = copy.deepcopy(match.details)
392
393 if state != "query":
394 virtual_port_channel_attrs_path = "/fabricResourceTemplate/template/virtualPortChannels/{0}".format(match.index if match else "-")
395
396 if state == "present":
397 if uuid and not mso.existing:
398 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
399
400 if interface_policy_group:
401 fabric_policy_template = MSOTemplate(mso, "fabric_policy", interface_policy_group.get("template"))
402 fabric_policy_template.validate_template("fabricPolicy")
403 interface_policy_group_uuid = fabric_policy_template.get_interface_policy_group_uuid(interface_policy_group.get("name"))
404
405 if match:
406 mso_values = {
407 "name": name,
408 "description": description,
409 ("node1Details", "node"): node_1,
410 ("node1Details", "memberInterfaces"): interfaces_node_1,
411 ("node2Details", "node"): node_2,
412 ("node2Details", "memberInterfaces"): interfaces_node_2,
413 "policy": interface_policy_group_uuid,
414 "interfaceDescriptions": format_interface_descriptions(mso, interface_descriptions),
415 }
416 append_update_ops_data(ops, match.details, virtual_port_channel_attrs_path, mso_values)
417 mso.sanitize(match.details, collate=True)
418
419 else:
420 mso_values = {
421 "name": name,
422 "description": description,
423 "node1Details": {
424 "node": node_1,
425 "memberInterfaces": interfaces_node_1,
426 },
427 "node2Details": {
428 "node": node_2,
429 "memberInterfaces": interfaces_node_2,
430 },
431 "policy": interface_policy_group_uuid,
432 "interfaceDescriptions": format_interface_descriptions(mso, interface_descriptions),
433 }
434 mso.sanitize(mso_values)
435 ops.append(dict(op="add", path=virtual_port_channel_attrs_path, value=mso.sent))
436
437 elif state == "absent":
438 if match:
439 ops.append(dict(op="remove", path=virtual_port_channel_attrs_path))
440
441 if not module.check_mode and ops:
442 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
443 virtual_port_channel_interfaces = response.get("fabricResourceTemplate", {}).get("template", {}).get("virtualPortChannels", []) or []
444 match = mso_template.get_object_by_key_value_pairs(
445 object_description,
446 virtual_port_channel_interfaces,
447 [KVPair("uuid", uuid) if uuid else KVPair("name", name)],
448 )
449 if match:
450 mso.existing = match.details
451 else:
452 mso.existing = {}
453 elif module.check_mode and state != "query":
454 mso.existing = mso.proposed if state == "present" else {}
455
456 mso.exit_json()
457
458
459 if __name__ == "__main__":
460 main()