Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_l3out_logical_node.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Marcel Zehnder (@maercu)
5 # Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]>
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 = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_l3out_logical_node
17 short_description: Manage Layer 3 Outside (L3Out) logical node profile nodes (l3ext:RsNodeL3OutAtt)
18 description:
19 - Bind nodes to node profiles on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - Name of an existing tenant.
24 type: str
25 aliases: [ tenant_name ]
26 l3out:
27 description:
28 - Name of an existing L3Out.
29 type: str
30 aliases: [ l3out_name ]
31 node_profile:
32 description:
33 - Name of the node profile.
34 type: str
35 aliases: [ node_profile_name, logical_node ]
36 pod_id:
37 description:
38 - Existing podId.
39 type: int
40 node_id:
41 description:
42 - Existing nodeId.
43 type: int
44 router_id:
45 description:
46 - Router ID in dotted decimal notation.
47 type: str
48 router_id_as_loopback:
49 description:
50 - Configure the router ID as a loopback IP.
51 type: str
52 choices: [ 'yes', 'no' ]
53 default: 'yes'
54 loopback_address:
55 description:
56 - The loopback IP address.
57 - The BGP-EVPN loopback IP address for Infra SR-MPLS L3Outs.
58 - A configured loopback address can be removed by passing an empty string (see Examples).
59 type: str
60 aliases: [ loopback ]
61 mpls_transport_loopback_address:
62 description:
63 - The MPLS transport loopback IP address for Infra SR-MPLS L3Outs.
64 type: str
65 aliases: [ mpls_transport_loopback ]
66 sid:
67 description:
68 - The Segment ID (SID) Index for Infra SR-MPLS L3Outs.
69 type: str
70 aliases: [ segment_id ]
71 state:
72 description:
73 - Use C(present) or C(absent) for adding or removing.
74 - Use C(query) for listing an object or multiple objects.
75 type: str
76 choices: [ absent, present, query ]
77 default: present
78 extends_documentation_fragment:
79 - cisco.aci.aci
80 - cisco.aci.annotation
81
82 seealso:
83 - module: cisco.aci.aci_l3out
84 - module: cisco.aci.aci_l3out_logical_node_profile
85 - name: APIC Management Information Model reference
86 description: More information about the internal APIC classes B(l3ext:RsNodeL3OutAtt)
87 link: https://developer.cisco.com/docs/apic-mim-ref/
88 author:
89 - Marcel Zehnder (@maercu)
90 - Akini Ross (@akinross)
91 """
92
93 EXAMPLES = r"""
94 - name: Add a new node to a node profile
95 cisco.aci.aci_l3out_logical_node:
96 host: apic
97 username: admin
98 password: SomeSecretPassword
99 tenant: my_tenant
100 l3out: my_l3out
101 node_profile: my_node_profile
102 pod_id: 1
103 node_id: 111
104 router_id: 111.111.111.111
105 loopback_address: 111.111.111.112
106 state: present
107 delegate_to: localhost
108
109 - name: Add a node to a infra SR-MPLS l3out node profile
110 cisco.aci.aci_l3out_logical_node: &aci_infra_node_profile_node
111 host: apic
112 username: admin
113 password: SomeSecretPassword
114 tenant: infra
115 l3out: ansible_infra_sr_mpls_l3out
116 node_profile: ansible_infra_sr_mpls_l3out_node_profile
117 pod_id: 1
118 node_id: 113
119 router_id_as_loopback: 'no'
120 loopback_address: 50.0.0.1
121 mpls_transport_loopback_address: 51.0.0.1
122 sid: 500
123 delegate_to: localhost
124
125 - name: Remove a loopback address from a node in node profile
126 cisco.aci.aci_l3out_logical_node:
127 host: apic
128 username: admin
129 password: SomeSecretPassword
130 tenant: my_tenant
131 l3out: my_l3out
132 node_profile: my_node_profile
133 pod_id: 1
134 node_id: 111
135 loopback_address: ""
136 delegate_to: localhost
137
138 - name: Delete a node from a node profile
139 cisco.aci.aci_l3out_logical_node:
140 host: apic
141 username: admin
142 password: SomeSecretPassword
143 tenant: my_tenant
144 l3out: my_l3out
145 node_profile: my_node_profile
146 pod_id: 1
147 node_id: 111
148 state: absent
149 delegate_to: localhost
150
151 - name: Query a node
152 cisco.aci.aci_l3out_logical_node:
153 host: apic
154 username: admin
155 password: SomeSecretPassword
156 tenant: my_tenant
157 l3out: my_l3out
158 node_profile: my_node_profile
159 pod_id: 1
160 node_id: 111
161 state: query
162 delegate_to: localhost
163 register: query_result
164
165 - name: Query all nodes
166 cisco.aci.aci_l3out_logical_node:
167 host: apic
168 username: admin
169 password: SomeSecretPassword
170 state: query
171 delegate_to: localhost
172 register: query_result
173 """
174
175 RETURN = r"""
176 current:
177 description: The existing configuration from the APIC after the module has finished
178 returned: success
179 type: list
180 sample:
181 [
182 {
183 "fvTenant": {
184 "attributes": {
185 "descr": "Production environment",
186 "dn": "uni/tn-production",
187 "name": "production",
188 "nameAlias": "",
189 "ownerKey": "",
190 "ownerTag": ""
191 }
192 }
193 }
194 ]
195 error:
196 description: The error information as returned from the APIC
197 returned: failure
198 type: dict
199 sample:
200 {
201 "code": "122",
202 "text": "unknown managed object class foo"
203 }
204 raw:
205 description: The raw output returned by the APIC REST API (xml or json)
206 returned: parse error
207 type: str
208 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
209 sent:
210 description: The actual/minimal configuration pushed to the APIC
211 returned: info
212 type: list
213 sample:
214 {
215 "fvTenant": {
216 "attributes": {
217 "descr": "Production environment"
218 }
219 }
220 }
221 previous:
222 description: The original configuration from the APIC before the module has started
223 returned: info
224 type: list
225 sample:
226 [
227 {
228 "fvTenant": {
229 "attributes": {
230 "descr": "Production",
231 "dn": "uni/tn-production",
232 "name": "production",
233 "nameAlias": "",
234 "ownerKey": "",
235 "ownerTag": ""
236 }
237 }
238 }
239 ]
240 proposed:
241 description: The assembled configuration from the user-provided parameters
242 returned: info
243 type: dict
244 sample:
245 {
246 "fvTenant": {
247 "attributes": {
248 "descr": "Production environment",
249 "name": "production"
250 }
251 }
252 }
253 filter_string:
254 description: The filter string used for the request
255 returned: failure or debug
256 type: str
257 sample: ?rsp-prop-include=config-only
258 method:
259 description: The HTTP method used for the request to the APIC
260 returned: failure or debug
261 type: str
262 sample: POST
263 response:
264 description: The HTTP response from the APIC
265 returned: failure or debug
266 type: str
267 sample: OK (30 bytes)
268 status:
269 description: The HTTP status from the APIC
270 returned: failure or debug
271 type: int
272 sample: 200
273 url:
274 description: The HTTP url used for the request to the APIC
275 returned: failure or debug
276 type: str
277 sample: https://10.11.12.13/api/mo/uni/tn-production.json
278 """
279
280
281 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
282 from ansible.module_utils.basic import AnsibleModule
283
284
285 def main():
286 argument_spec = aci_argument_spec()
287 argument_spec.update(aci_annotation_spec())
288 argument_spec.update(
289 tenant=dict(type="str", aliases=["tenant_name"]),
290 l3out=dict(type="str", aliases=["l3out_name"]),
291 node_profile=dict(type="str", aliases=["node_profile_name", "logical_node"]),
292 pod_id=dict(type="int"),
293 node_id=dict(type="int"),
294 router_id=dict(type="str"),
295 router_id_as_loopback=dict(type="str", default="yes", choices=["yes", "no"]),
296 loopback_address=dict(type="str", aliases=["loopback"]),
297 mpls_transport_loopback_address=dict(type="str", aliases=["mpls_transport_loopback"]),
298 sid=dict(type="str", aliases=["segment_id"]),
299 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
300 )
301
302 module = AnsibleModule(
303 argument_spec=argument_spec,
304 supports_check_mode=True,
305 required_if=[
306 ["state", "absent", ["tenant", "l3out", "node_profile", "pod_id", "node_id"]],
307 ["state", "present", ["tenant", "l3out", "node_profile", "pod_id", "node_id"]],
308 ],
309 required_by={"mpls_transport_loopback_address": "loopback_address"},
310 required_together=[("mpls_transport_loopback_address", "sid")],
311 )
312
313 tenant = module.params.get("tenant")
314 l3out = module.params.get("l3out")
315 node_profile = module.params.get("node_profile")
316 pod_id = module.params.get("pod_id")
317 node_id = module.params.get("node_id")
318 router_id = module.params.get("router_id")
319 router_id_as_loopback = module.params.get("router_id_as_loopback")
320 loopback_address = module.params.get("loopback_address")
321 mpls_transport_loopback_address = module.params.get("mpls_transport_loopback_address")
322 sid = module.params.get("sid")
323 state = module.params.get("state")
324
325 tdn = None
326 if pod_id is not None and node_id is not None:
327 tdn = "topology/pod-{0}/node-{1}".format(pod_id, node_id)
328
329 aci = ACIModule(module)
330
331 child_classes = ["l3extLoopBackIfP"]
332
333 if mpls_transport_loopback_address is not None:
334 child_classes.append("mplsNodeSidP")
335
336 child_configs = []
337
338 aci.construct_url(
339 root_class=dict(
340 aci_class="fvTenant",
341 aci_rn="tn-{0}".format(tenant),
342 module_object=tenant,
343 target_filter={"name": tenant},
344 ),
345 subclass_1=dict(
346 aci_class="l3extOut",
347 aci_rn="out-{0}".format(l3out),
348 module_object=l3out,
349 target_filter={"name": l3out},
350 ),
351 subclass_2=dict(
352 aci_class="l3extLNodeP",
353 aci_rn="lnodep-{0}".format(node_profile),
354 module_object=node_profile,
355 target_filter={"name": node_profile},
356 ),
357 subclass_3=dict(
358 aci_class="l3extRsNodeL3OutAtt",
359 aci_rn="rsnodeL3OutAtt-[{0}]".format(tdn),
360 module_object=tdn,
361 target_filter={"name": tdn},
362 ),
363 child_classes=child_classes,
364 )
365
366 aci.get_existing()
367
368 if state == "present":
369 if loopback_address is not None:
370 if loopback_address == "" and isinstance(aci.existing, list) and len(aci.existing) > 0:
371 for child in aci.existing[0].get("l3extRsNodeL3OutAtt", {}).get("children", []):
372 previous_loopback_address = child.get("l3extLoopBackIfP", {}).get("attributes", {}).get("addr")
373 child_configs.append(dict(l3extLoopBackIfP=dict(attributes=dict(addr=previous_loopback_address, status="deleted"))))
374 elif loopback_address:
375 loopback_address_config = dict(l3extLoopBackIfP=dict(attributes=dict(addr=loopback_address), children=[]))
376 if mpls_transport_loopback_address:
377 loopback_address_config["l3extLoopBackIfP"]["children"].append(
378 dict(mplsNodeSidP=dict(attributes=dict(loopbackAddr=mpls_transport_loopback_address, sidoffset=sid)))
379 )
380 child_configs.append(loopback_address_config)
381
382 aci.payload(
383 aci_class="l3extRsNodeL3OutAtt",
384 class_config=dict(rtrId=router_id, rtrIdLoopBack=router_id_as_loopback, tDn=tdn),
385 child_configs=child_configs,
386 )
387
388 aci.get_diff(aci_class="l3extRsNodeL3OutAtt")
389
390 aci.post_config()
391
392 elif state == "absent":
393 aci.delete_config()
394
395 aci.exit_json()
396
397
398 if __name__ == "__main__":
399 main()