Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Bruno Calogero <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_static_binding_to_epg
16 short_description: Bind static paths to EPGs (fv:RsPathAtt)
17 description:
18 - Bind static paths to EPGs on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - Name of an existing tenant.
23 type: str
24 aliases: [ tenant_name ]
25 ap:
26 description:
27 - Name of an existing application network profile, that will contain the EPGs.
28 type: str
29 aliases: [ app_profile, app_profile_name ]
30 epg:
31 description:
32 - The name of the end point group.
33 type: str
34 aliases: [ epg_name ]
35 description:
36 description:
37 - Description for the static path to EPG binding.
38 type: str
39 aliases: [ descr ]
40 encap_id:
41 description:
42 - The encapsulation ID associating the C(epg) with the interface path.
43 - This acts as the secondary C(encap_id) when using micro-segmentation.
44 - Accepted values are any valid encap ID for specified encap, currently ranges between C(1) and C(4096).
45 type: int
46 aliases: [ vlan, vlan_id ]
47 primary_encap_id:
48 description:
49 - Determines the primary encapsulation ID associating the C(epg)
50 with the interface path when using micro-segmentation.
51 - Accepted values are any valid encap ID for specified encap, currently ranges between C(1) and C(4096) and C(unknown).
52 - C(unknown) is the default value and using C(unknown) disables the Micro-Segmentation.
53 type: str
54 aliases: [ primary_vlan, primary_vlan_id ]
55 deploy_immediacy:
56 description:
57 - The Deployment Immediacy of Static EPG on PC, VPC or Interface.
58 - The APIC defaults to C(lazy) when unset during creation.
59 type: str
60 choices: [ immediate, lazy ]
61 interface_mode:
62 description:
63 - Determines how layer 2 tags will be read from and added to frames.
64 - Values C(802.1p) and C(native) are identical.
65 - Values C(access) and C(untagged) are identical.
66 - Values C(regular), C(tagged) and C(trunk) are identical.
67 - The APIC defaults to C(trunk) when unset during creation.
68 type: str
69 choices: [ 802.1p, access, native, regular, tagged, trunk, untagged ]
70 aliases: [ interface_mode_name, mode ]
71 interface_type:
72 description:
73 - The type of interface for the static EPG deployment.
74 type: str
75 choices: [ fex, port_channel, switch_port, vpc, fex_port_channel, fex_vpc ]
76 default: switch_port
77 pod_id:
78 description:
79 - The pod number part of the tDn.
80 - C(pod_id) is usually an integer below C(10).
81 type: int
82 aliases: [ pod, pod_number ]
83 leafs:
84 description:
85 - The switch ID(s) that the C(interface) belongs to.
86 - When C(interface_type) is C(switch_port), C(port_channel), or C(fex), then C(leafs) is a string of the leaf ID.
87 - When C(interface_type) is C(vpc), then C(leafs) is a list with both leaf IDs.
88 - The C(leafs) value is usually something like '101' or '101-102' depending on C(connection_type).
89 type: list
90 elements: str
91 aliases: [ leaves, nodes, paths, switches ]
92 interface:
93 description:
94 - The C(interface) string value part of the tDn.
95 - Usually a policy group like C(test-IntPolGrp) or an interface of the following format C(1/7) depending on C(interface_type).
96 type: str
97 extpaths:
98 description:
99 - The C(extpaths) integer value part of the tDn.
100 - C(extpaths) is only used if C(interface_type) is C(fex), C(fex_vpc) or C(fex_port_channel).
101 - When C(interface_type) is C(fex_vpc), then C(extpaths) is a list with both fex IDs.
102 - Usually something like C(1011).
103 type: list
104 elements: str
105 state:
106 description:
107 - Use C(present) or C(absent) for adding or removing.
108 - Use C(query) for listing an object or multiple objects.
109 type: str
110 choices: [ absent, present, query ]
111 default: present
112 extends_documentation_fragment:
113 - cisco.aci.aci
114 - cisco.aci.annotation
115
116 notes:
117 - The C(tenant), C(ap), C(epg) used must exist before using this module in your playbook.
118 The M(cisco.aci.aci_tenant), M(cisco.aci.aci_ap), M(cisco.aci.aci_epg) modules can be used for this.
119 seealso:
120 - module: cisco.aci.aci_tenant
121 - module: cisco.aci.aci_ap
122 - module: cisco.aci.aci_epg
123 - name: APIC Management Information Model reference
124 description: More information about the internal APIC class B(fv:RsPathAtt).
125 link: https://developer.cisco.com/docs/apic-mim-ref/
126 author:
127 - Bruno Calogero (@brunocalogero)
128 - Marcel Zehnder (@maercu)
129 """
130
131 EXAMPLES = r"""
132 - name: Deploy Static Path binding for given EPG
133 cisco.aci.aci_static_binding_to_epg:
134 host: apic
135 username: admin
136 password: SomeSecretPassword
137 tenant: accessport-code-cert
138 ap: accessport_code_app
139 epg: accessport_epg1
140 encap_id: 222
141 deploy_immediacy: lazy
142 interface_mode: untagged
143 interface_type: switch_port
144 pod_id: 1
145 leafs: 101
146 interface: '1/7'
147 state: present
148 delegate_to: localhost
149
150 - name: Remove Static Path binding for given EPG
151 cisco.aci.aci_static_binding_to_epg:
152 host: apic
153 username: admin
154 password: SomeSecretPassword
155 tenant: accessport-code-cert
156 ap: accessport_code_app
157 epg: accessport_epg1
158 interface_type: switch_port
159 pod: 1
160 leafs: 101
161 interface: '1/7'
162 state: absent
163 delegate_to: localhost
164
165 - name: Get specific Static Path binding for given EPG
166 cisco.aci.aci_static_binding_to_epg:
167 host: apic
168 username: admin
169 password: SomeSecretPassword
170 tenant: accessport-code-cert
171 ap: accessport_code_app
172 epg: accessport_epg1
173 interface_type: switch_port
174 pod: 1
175 leafs: 101
176 interface: '1/7'
177 state: query
178 delegate_to: localhost
179 register: query_result
180 """
181
182 RETURN = r"""
183 current:
184 description: The existing configuration from the APIC after the module has finished
185 returned: success
186 type: list
187 sample:
188 [
189 {
190 "fvTenant": {
191 "attributes": {
192 "descr": "Production environment",
193 "dn": "uni/tn-production",
194 "name": "production",
195 "nameAlias": "",
196 "ownerKey": "",
197 "ownerTag": ""
198 }
199 }
200 }
201 ]
202 error:
203 description: The error information as returned from the APIC
204 returned: failure
205 type: dict
206 sample:
207 {
208 "code": "122",
209 "text": "unknown managed object class foo"
210 }
211 raw:
212 description: The raw output returned by the APIC REST API (xml or json)
213 returned: parse error
214 type: str
215 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
216 sent:
217 description: The actual/minimal configuration pushed to the APIC
218 returned: info
219 type: list
220 sample:
221 {
222 "fvTenant": {
223 "attributes": {
224 "descr": "Production environment"
225 }
226 }
227 }
228 previous:
229 description: The original configuration from the APIC before the module has started
230 returned: info
231 type: list
232 sample:
233 [
234 {
235 "fvTenant": {
236 "attributes": {
237 "descr": "Production",
238 "dn": "uni/tn-production",
239 "name": "production",
240 "nameAlias": "",
241 "ownerKey": "",
242 "ownerTag": ""
243 }
244 }
245 }
246 ]
247 proposed:
248 description: The assembled configuration from the user-provided parameters
249 returned: info
250 type: dict
251 sample:
252 {
253 "fvTenant": {
254 "attributes": {
255 "descr": "Production environment",
256 "name": "production"
257 }
258 }
259 }
260 filter_string:
261 description: The filter string used for the request
262 returned: failure or debug
263 type: str
264 sample: ?rsp-prop-include=config-only
265 method:
266 description: The HTTP method used for the request to the APIC
267 returned: failure or debug
268 type: str
269 sample: POST
270 response:
271 description: The HTTP response from the APIC
272 returned: failure or debug
273 type: str
274 sample: OK (30 bytes)
275 status:
276 description: The HTTP status from the APIC
277 returned: failure or debug
278 type: int
279 sample: 200
280 url:
281 description: The HTTP url used for the request to the APIC
282 returned: failure or debug
283 type: str
284 sample: https://10.11.12.13/api/mo/uni/tn-production.json
285 """
286
287 from ansible.module_utils.basic import AnsibleModule
288 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
289
290 INTERFACE_MODE_MAPPING = {
291 "802.1p": "native",
292 "access": "untagged",
293 "native": "native",
294 "regular": "regular",
295 "tagged": "regular",
296 "trunk": "regular",
297 "untagged": "untagged",
298 }
299
300 INTERFACE_TYPE_MAPPING = dict(
301 fex="topology/pod-{pod_id}/paths-{leafs}/extpaths-{extpaths}/pathep-[eth{interface}]",
302 fex_port_channel="topology/pod-{pod_id}/paths-{leafs}/extpaths-{extpaths}/pathep-[{interface}]",
303 fex_vpc="topology/pod-{pod_id}/protpaths-{leafs}/extprotpaths-{extpaths}/pathep-[{interface}]",
304 port_channel="topology/pod-{pod_id}/paths-{leafs}/pathep-[{interface}]",
305 switch_port="topology/pod-{pod_id}/paths-{leafs}/pathep-[eth{interface}]",
306 vpc="topology/pod-{pod_id}/protpaths-{leafs}/pathep-[{interface}]",
307 )
308
309 # TODO: change 'deploy_immediacy' to 'resolution_immediacy' (as seen in aci_epg_to_domain)?
310
311
312 def main():
313 argument_spec = aci_argument_spec()
314 argument_spec.update(aci_annotation_spec())
315 argument_spec.update(
316 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
317 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), # Not required for querying all objects
318 epg=dict(type="str", aliases=["epg_name"]), # Not required for querying all objects
319 description=dict(type="str", aliases=["descr"]),
320 encap_id=dict(type="int", aliases=["vlan", "vlan_id"]),
321 primary_encap_id=dict(type="str", aliases=["primary_vlan", "primary_vlan_id"]),
322 deploy_immediacy=dict(type="str", choices=["immediate", "lazy"]),
323 interface_mode=dict(
324 type="str", choices=["802.1p", "access", "native", "regular", "tagged", "trunk", "untagged"], aliases=["interface_mode_name", "mode"]
325 ),
326 interface_type=dict(type="str", default="switch_port", choices=["fex", "port_channel", "switch_port", "vpc", "fex_port_channel", "fex_vpc"]),
327 pod_id=dict(type="int", aliases=["pod", "pod_number"]), # Not required for querying all objects
328 leafs=dict(type="list", elements="str", aliases=["leaves", "nodes", "paths", "switches"]), # Not required for querying all objects
329 interface=dict(type="str"), # Not required for querying all objects
330 extpaths=dict(type="list", elements="str"),
331 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
332 )
333
334 module = AnsibleModule(
335 argument_spec=argument_spec,
336 supports_check_mode=True,
337 required_if=[
338 ["interface_type", "fex", ["extpaths"]],
339 ["interface_type", "fex_vpc", ["extpaths"]],
340 ["interface_type", "fex_port_channel", ["extpaths"]],
341 ["state", "absent", ["ap", "epg", "interface", "leafs", "pod_id", "tenant"]],
342 ["state", "present", ["ap", "encap_id", "epg", "interface", "leafs", "pod_id", "tenant"]],
343 ],
344 )
345
346 tenant = module.params.get("tenant")
347 ap = module.params.get("ap")
348 epg = module.params.get("epg")
349 description = module.params.get("description")
350 encap_id = module.params.get("encap_id")
351 primary_encap_id = module.params.get("primary_encap_id")
352 deploy_immediacy = module.params.get("deploy_immediacy")
353 interface_mode = module.params.get("interface_mode")
354 interface_type = module.params.get("interface_type")
355 pod_id = module.params.get("pod_id")
356 leafs = module.params.get("leafs")
357 interface = module.params.get("interface")
358 extpaths = module.params.get("extpaths")
359 state = module.params.get("state")
360
361 aci = ACIModule(module)
362
363 if leafs is not None:
364 # Process leafs, and support dash-delimited leafs
365 leafs = []
366 for leaf in module.params.get("leafs"):
367 # Users are likely to use integers for leaf IDs, which would raise an exception when using the join method
368 leafs.extend(str(leaf).split("-"))
369 if len(leafs) == 1:
370 if interface_type in ["vpc", "fex_vpc"]:
371 aci.fail_json(msg='A interface_type of "vpc" requires 2 leafs')
372 leafs = leafs[0]
373 elif len(leafs) == 2:
374 if interface_type not in ["vpc", "fex_vpc"]:
375 aci.fail_json(
376 msg='The interface_types "switch_port", "port_channel", and "fex" \
377 do not support using multiple leafs for a single binding'
378 )
379 leafs = "-".join(leafs)
380 else:
381 aci.fail_json(msg='The "leafs" parameter must not have more than 2 entries')
382
383 if extpaths is not None:
384 # Process extpaths, and support dash-delimited extpaths
385 extpaths = []
386 for extpath in module.params.get("extpaths"):
387 # Users are likely to use integers for extpaths IDs, which would raise an exception when using the join method
388 extpaths.extend(str(extpath).split("-"))
389 if len(extpaths) == 1:
390 if interface_type == "fex_vpc":
391 aci.fail_json(msg='A interface_type of "fex_vpc" requires 2 extpaths')
392 extpaths = extpaths[0]
393 elif len(extpaths) == 2:
394 if interface_type != "fex_vpc":
395 aci.fail_json(
396 msg='The interface_types "fex" \
397 and "fex_port_channel" do not support using multiple extpaths for a single binding'
398 )
399 extpaths = "-".join(extpaths)
400 else:
401 aci.fail_json(msg='The "extpaths" parameter must not have more than 2 entries')
402
403 if encap_id is not None:
404 if encap_id not in range(1, 4097):
405 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096")
406 encap_id = "vlan-{0}".format(encap_id)
407
408 if primary_encap_id is not None:
409 try:
410 primary_encap_id = int(primary_encap_id)
411 if isinstance(primary_encap_id, int) and primary_encap_id in range(1, 4097):
412 primary_encap_id = "vlan-{0}".format(primary_encap_id)
413 else:
414 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096 or unknown.")
415 except Exception as e:
416 if isinstance(primary_encap_id, str) and primary_encap_id != "unknown":
417 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096 or unknown. %s" % e)
418
419 static_path = INTERFACE_TYPE_MAPPING[interface_type].format(pod_id=pod_id, leafs=leafs, extpaths=extpaths, interface=interface)
420
421 path_target_filter = {}
422 if pod_id is not None and leafs is not None and interface is not None and (interface_type != "fex" or extpaths is not None):
423 path_target_filter = {"tDn": static_path}
424
425 if interface_mode is not None:
426 interface_mode = INTERFACE_MODE_MAPPING[interface_mode]
427
428 aci.construct_url(
429 root_class=dict(
430 aci_class="fvTenant",
431 aci_rn="tn-{0}".format(tenant),
432 module_object=tenant,
433 target_filter={"name": tenant},
434 ),
435 subclass_1=dict(
436 aci_class="fvAp",
437 aci_rn="ap-{0}".format(ap),
438 module_object=ap,
439 target_filter={"name": ap},
440 ),
441 subclass_2=dict(
442 aci_class="fvAEPg",
443 aci_rn="epg-{0}".format(epg),
444 module_object=epg,
445 target_filter={"name": epg},
446 ),
447 subclass_3=dict(
448 aci_class="fvRsPathAtt",
449 aci_rn="rspathAtt-[{0}]".format(static_path),
450 module_object=static_path,
451 target_filter=path_target_filter,
452 ),
453 )
454
455 aci.get_existing()
456
457 if state == "present":
458 aci.payload(
459 aci_class="fvRsPathAtt",
460 class_config=dict(
461 descr=description,
462 encap=encap_id,
463 primaryEncap=primary_encap_id,
464 instrImedcy=deploy_immediacy,
465 mode=interface_mode,
466 tDn=static_path,
467 ),
468 )
469
470 aci.get_diff(aci_class="fvRsPathAtt")
471
472 aci.post_config()
473
474 elif state == "absent":
475 aci.delete_config()
476
477 aci.exit_json()
478
479
480 if __name__ == "__main__":
481 main()