Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Akini Ross (@akinross) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
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_l3out_node_static_route
18 version_added: "2.11.0"
19 short_description: Manage L3Out Node Static Routes on Cisco Nexus Dashboard Orchestrator (NDO).
20 description:
21 - Manage L3Out Node Static Routes on Cisco Nexus Dashboard Orchestrator (NDO).
22 - This module is only supported on ND v3.1 (NDO v4.3) and later.
23 author:
24 - Akini Ross (@akinross)
25 options:
26 template:
27 description:
28 - The name of the template.
29 - The template must be an L3Out template.
30 - This parameter or O(template_id) is required.
31 type: str
32 aliases: [ l3out_template ]
33 template_id:
34 description:
35 - The ID of the L3Out template.
36 - This parameter or O(template) is required.
37 type: str
38 aliases: [ l3out_template_id ]
39 l3out:
40 description:
41 - The name of the L3Out.
42 - This parameter or O(l3out_uuid) is required.
43 type: str
44 aliases: [ l3out_name ]
45 l3out_uuid:
46 description:
47 - The UUID of the L3Out.
48 - This parameter or O(l3out) is required.
49 type: str
50 pod_id:
51 description:
52 - The pod ID of the node (border leaf switch).
53 type: str
54 required: true
55 aliases: [ pod ]
56 node_id:
57 description:
58 - The ID of the node.
59 type: str
60 required: true
61 aliases: [ node, border_leaf ]
62 prefix:
63 description:
64 - The prefix of the static route.
65 type: str
66 description:
67 description:
68 - The description of the static route.
69 - Providing an empty string O(description="") will remove the description from the static route.
70 type: str
71 administrative_distance:
72 description:
73 - The administrative distance of the static route.
74 - The value must be in the range 1 - 255.
75 - Defaults to O(administrative_distance=1) when unset during creation.
76 type: int
77 aliases: [ admin_distance ]
78 bfd_tracking:
79 description:
80 - Enable BFD tracking for the static route.
81 - Defaults to O(bfd_tracking=false) when unset during creation.
82 type: bool
83 next_hop_null:
84 description:
85 - Create a static route to Null0 interface.
86 - This is used to drop traffic for the specified prefix.
87 - Defaults to O(next_hop_null=false) when unset during creation.
88 type: bool
89 track_policy_uuid:
90 description:
91 - The UUID of the track policy to be used.
92 - Providing an empty string O(track_policy="") will remove the track policy from the static route.
93 type: str
94 aliases: [ ipsla_track_list_uuid ]
95 track_policy:
96 description:
97 - The track policy to be used.
98 - Providing an empty dictionary O(track_policy={}) will remove the track policy from the static route.
99 type: dict
100 aliases: [ ipsla_track_list ]
101 suboptions:
102 name:
103 description:
104 - The name of the track policy.
105 type: str
106 template:
107 description:
108 - The name of the template that contains the track policy.
109 - This parameter or O(track_policy.template_id) is required.
110 type: str
111 template_id:
112 description:
113 - The ID of the template that contains the track policy.
114 - This parameter or O(track_policy.template) is required.
115 type: str
116 state:
117 description:
118 - Determines the desired state of the resource.
119 - Use C(absent) to remove the resource.
120 - Use C(query) to list the resource.
121 - Use C(present) to create or update the resource.
122 type: str
123 choices: [ absent, query, present ]
124 default: query
125 notes:
126 - The O(template) or O(template_id) must exist before using this module in your playbook.
127 The M(cisco.mso.ndo_template) module can be used for this.
128 - The O(l3out) or O(l3out_uuid) must exist before using this module in your playbook.
129 The M(cisco.mso.ndo_l3out_template) module can be used for this.
130 - The O(track_policy) or O(track_policy_uuid) must exist before using this module in your playbook.
131 The M(cisco.mso.ndo_ipsla_track_list) module can be used for this.
132 seealso:
133 - module: cisco.mso.ndo_template
134 - module: cisco.mso.ndo_l3out_template
135 - module: cisco.mso.ndo_ipsla_track_list
136 extends_documentation_fragment: cisco.mso.modules
137 """
138
139 EXAMPLES = r"""
140 - name: Create a L3Out Node Static Route
141 cisco.mso.ndo_l3out_node_static_route:
142 host: mso_host
143 username: admin
144 password: SomeSecretPassword
145 template: l3out_template
146 l3out: l3out_name
147 pod_id: 1
148 node_id: 101
149 prefix: 10.0.0.0/24
150 state: present
151
152 - name: Update an existing L3Out Node Static Route
153 cisco.mso.ndo_l3out_node_static_route:
154 host: mso_host
155 username: admin
156 password: SomeSecretPassword
157 template_id: "{{ l3out_template.current.templateId }}"
158 l3out_uuid: "{{ l3out.current.uuid }}"
159 pod_id: 1
160 node_id: 101
161 prefix: 10.0.0.0/24
162 description: "Static route for 10.0.0.0/24"
163 administrative_distance: 10
164 bfd_tracking: true
165 next_hop_null: true
166 track_policy:
167 name: "track_policy_name"
168 template: "track_policy_template"
169 state: present
170
171 - name: Query an existing L3Out Node Static Route
172 cisco.mso.ndo_l3out_node_static_route:
173 host: mso_host
174 username: admin
175 password: SomeSecretPassword
176 template: l3out_template
177 l3out: l3out_name
178 pod_id: 1
179 node_id: 101
180 prefix: 10.0.0.0/24
181 state: query
182 register: query_with_name
183
184 - name: Query all existing L3Out Node Static Routes in a L3Out Node
185 cisco.mso.ndo_l3out_node_static_route:
186 host: mso_host
187 username: admin
188 password: SomeSecretPassword
189 template: l3out_template
190 l3out: l3out_name
191 pod_id: 1
192 node_id: 101
193 state: query
194
195 - name: Delete an existing L3Out Node Static Route
196 cisco.mso.ndo_l3out_node_static_route:
197 host: mso_host
198 username: admin
199 password: SomeSecretPassword
200 template: l3out_template
201 l3out: l3out_name
202 pod_id: 1
203 node_id: 101
204 prefix: 10.0.0.0/24
205 state: absent
206 """
207
208 RETURN = r"""
209 """
210
211 from ansible.module_utils.basic import AnsibleModule
212 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
213 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
214 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, check_if_all_elements_are_none
215 import copy
216
217
218 def main():
219 argument_spec = mso_argument_spec()
220 argument_spec.update(
221 template=dict(type="str", aliases=["l3out_template"]),
222 template_id=dict(type="str", aliases=["l3out_template_id"]),
223 l3out=dict(type="str", aliases=["l3out_name"]),
224 l3out_uuid=dict(type="str"),
225 pod_id=dict(type="str", aliases=["pod"], required=True),
226 node_id=dict(type="str", aliases=["node", "border_leaf"], required=True),
227 prefix=dict(type="str"),
228 description=dict(type="str"),
229 administrative_distance=dict(type="int", aliases=["admin_distance"]),
230 bfd_tracking=dict(type="bool"),
231 next_hop_null=dict(type="bool"),
232 track_policy_uuid=dict(type="str", aliases=["ipsla_track_list_uuid"]),
233 track_policy=dict(
234 type="dict",
235 aliases=["ipsla_track_list"],
236 options=dict(
237 name=dict(type="str"),
238 template=dict(type="str"),
239 template_id=dict(type="str"),
240 ),
241 required_by={
242 "template": "name",
243 "template_id": "name",
244 },
245 mutually_exclusive=[
246 ["template", "template_id"],
247 ],
248 ),
249 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
250 )
251
252 module = AnsibleModule(
253 argument_spec=argument_spec,
254 supports_check_mode=True,
255 required_if=[
256 ["state", "present", ["prefix"]],
257 ["state", "absent", ["prefix"]],
258 ],
259 required_one_of=[
260 ["template", "template_id"],
261 ["l3out", "l3out_uuid"],
262 ],
263 mutually_exclusive=[
264 ["template", "template_id"],
265 ["l3out", "l3out_uuid"],
266 ["track_policy_uuid", "track_policy"],
267 ],
268 )
269
270 mso = MSOModule(module)
271 mso_templates = MSOTemplates(mso)
272
273 template_name = mso.params.get("template")
274 template_id = mso.params.get("template_id")
275 l3out = mso.params.get("l3out")
276 l3out_uuid = mso.params.get("l3out_uuid")
277 pod_id = mso.params.get("pod_id")
278 node_id = mso.params.get("node_id")
279 prefix = mso.params.get("prefix")
280 description = mso.params.get("description")
281 administrative_distance = mso.params.get("administrative_distance")
282 bfd_tracking = mso.params.get("bfd_tracking")
283 next_hop_null = mso.params.get("next_hop_null")
284 track_policy_uuid = mso.params.get("track_policy_uuid")
285 track_policy = mso.params.get("track_policy")
286 if track_policy is not None and check_if_all_elements_are_none(track_policy.values()):
287 track_policy = {}
288 if track_policy and not (track_policy.get("name") and (track_policy.get("template") or track_policy.get("template_id"))):
289 mso.fail_json(msg="track_policy.name and one of the following are required: track_policy.template, track_policy.template_id")
290
291 state = mso.params.get("state")
292
293 mso_template = mso_templates.get_template("l3out", template_name, template_id)
294 mso_template.validate_template("l3out")
295 l3out_object = mso_template.get_l3out_object(l3out_uuid, l3out, True)
296 node_object = mso_template.get_l3out_node(l3out_object.details, pod_id, node_id, True)
297
298 if track_policy or track_policy_uuid:
299 if track_policy_uuid:
300 track_policy_match = mso_template.get_template_object_by_uuid("ipslaTrackList", track_policy_uuid, True)
301 else:
302 track_policy_mso_template = mso_templates.get_template(
303 "tenant",
304 track_policy.get("template"),
305 track_policy.get("template_id"),
306 fail_module=True,
307 )
308 track_policy_match = track_policy_mso_template.get_ipsla_track_list(
309 uuid=track_policy_uuid,
310 name=track_policy.get("name"),
311 fail_module=True,
312 )
313 track_policy_uuid = track_policy_match.details.get("uuid")
314
315 match = mso_template.get_l3out_node_static_route(node_object.details, prefix)
316 if prefix and match:
317 set_node_static_route_details(mso_template, match.details)
318 mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object
319 elif match:
320 mso.existing = [set_node_static_route_details(mso_template, obj) for obj in match]
321
322 l3out_node_static_route_path = "/l3outTemplate/l3outs/{0}/nodes/{1}/staticRoutes/{2}".format(
323 l3out_object.index, node_object.index, match.index if match else "-"
324 )
325
326 ops = []
327
328 if state == "present":
329 mso_values = {
330 "prefix": prefix,
331 "description": description if description else None,
332 "fallbackPref": administrative_distance,
333 "enableBFDTracking": bfd_tracking,
334 "trackPolicyRef": track_policy_uuid if track_policy_uuid else None,
335 "nullNextHop": next_hop_null,
336 }
337
338 if match:
339 remove_data = []
340 unwanted = []
341 if description == "" and match.details.get("description"):
342 remove_data.append("description")
343 unwanted.append("description")
344 if track_policy == {} or track_policy_uuid == "":
345 remove_data.extend(["trackPolicyRef"])
346 unwanted.extend(["trackPolicyRef", "trackPolicyName", "trackPolicyTemplateName", "trackPolicyTemplateId"])
347 append_update_ops_data(ops, match.details, l3out_node_static_route_path, mso_values, remove_data)
348 mso.sanitize(mso_values, collate=True, unwanted=unwanted)
349 else:
350 mso.sanitize(mso_values)
351 ops.append(dict(op="add", path=l3out_node_static_route_path, value=mso.sent))
352
353 set_node_static_route_details(mso_template, mso.proposed)
354
355 elif state == "absent":
356 if match:
357 ops.append(dict(op="remove", path=l3out_node_static_route_path))
358
359 if not mso.module.check_mode and ops:
360 response = mso.request(mso_template.template_path, method="PATCH", data=ops)
361 l3out_object = mso_template.get_l3out_object(l3out_uuid, l3out, True, search_object=response)
362 node_object = mso_template.get_l3out_node(l3out_object.details, pod_id, node_id, True)
363 match = mso_template.get_l3out_node_static_route(node_object.details, prefix)
364 if match:
365 set_node_static_route_details(mso_template, match.details)
366 mso.existing = match.details # When the state is present
367 else:
368 mso.existing = {} # When the state is absent
369 elif mso.module.check_mode and state != "query": # When the state is present/absent with check mode
370 set_node_static_route_details(mso_template, mso.proposed)
371 mso.existing = mso.proposed if state == "present" else {}
372 mso.exit_json()
373
374
375 def set_node_static_route_details(mso_template, node_static_route):
376 if node_static_route:
377 reference_details = None
378 if node_static_route.get("trackPolicyRef"):
379 reference_details = {
380 "track_list_reference": {
381 "name": "trackPolicyName",
382 "reference": "trackPolicyRef",
383 "type": "ipslaTrackList",
384 "template": "trackPolicyTemplateName",
385 "templateId": "trackPolicyTemplateId",
386 }
387 }
388 mso_template.update_config_with_template_and_references(
389 node_static_route,
390 reference_details,
391 True,
392 )
393
394 return node_static_route
395
396
397 if __name__ == "__main__":
398 main()