Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Jacob McGill <jmcgill298>
5 # Copyright: (c) 2020, Shreyas Srish <[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": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_epg_to_domain
17 short_description: Bind EPGs to Domains (fv:RsDomAtt)
18 description:
19 - Bind EPGs to Physical and Virtual Domains on Cisco ACI fabrics.
20 options:
21 allow_useg:
22 description:
23 - Allows micro-segmentation.
24 - The APIC defaults to C(encap) when unset during creation.
25 type: str
26 choices: [ encap, useg ]
27 ap:
28 description:
29 - Name of an existing application network profile, that will contain the EPGs.
30 type: str
31 aliases: [ app_profile, app_profile_name ]
32 deploy_immediacy:
33 description:
34 - Determines when the policy is pushed to hardware Policy CAM.
35 - The APIC defaults to C(lazy) when unset during creation.
36 type: str
37 choices: [ immediate, lazy ]
38 domain:
39 description:
40 - Name of the physical or virtual domain being associated with the EPG.
41 type: str
42 aliases: [ domain_name, domain_profile ]
43 domain_type:
44 description:
45 - Specify whether the Domain is a physical (phys), a virtual (vmm) or an L2 external domain association (l2dom).
46 type: str
47 choices: [ l2dom, phys, vmm ]
48 aliases: [ type ]
49 encap:
50 description:
51 - The VLAN encapsulation for the EPG when binding a VMM Domain in static VLAN mode.
52 - The VLAN mode in UI is set to static when O(encap) is not set.
53 - This acts as the secondary encap when using useg.
54 - Accepted values range between C(1) and C(4096).
55 type: int
56 encap_mode:
57 description:
58 - The encapsulation method to be used.
59 - The APIC defaults to C(auto) when unset during creation.
60 - If vxlan is selected, switching_mode must be "AVE".
61 type: str
62 choices: [ auto, vlan, vxlan ]
63 switching_mode:
64 description:
65 - Switching Mode used by the switch
66 type: str
67 choices: [ AVE, native ]
68 default: native
69 epg:
70 description:
71 - Name of the end point group.
72 type: str
73 aliases: [ epg_name, name ]
74 enhanced_lag_policy:
75 description:
76 - Name of the VMM Domain Enhanced Lag Policy.
77 type: str
78 aliases: [ lag_policy ]
79 vmm_uplink_active:
80 description:
81 - A list of active uplink IDs.
82 - The order decides the order in which active uplinks take over for a failed uplink.
83 - At least one active uplink must remain specified in the list when an active uplink was previously configured.
84 type: list
85 elements: str
86 vmm_uplink_standby:
87 description:
88 - A list of standby uplink IDs.
89 - At least one standby uplink must remain specified in the list when no active uplink is configured.
90 type: list
91 elements: str
92 netflow:
93 description:
94 - Determines if netflow should be enabled.
95 - The APIC defaults to C(false) when unset during creation.
96 type: bool
97 primary_encap:
98 description:
99 - Determines the primary VLAN ID when using useg.
100 - Accepted values range between C(1) and C(4096).
101 type: int
102 resolution_immediacy:
103 description:
104 - Determines when the policies should be resolved and available.
105 - The APIC defaults to C(lazy) when unset during creation.
106 type: str
107 choices: [ immediate, lazy, pre-provision ]
108 state:
109 description:
110 - Use C(present) or C(absent) for adding or removing.
111 - Use C(query) for listing an object or multiple objects.
112 type: str
113 choices: [ absent, present, query ]
114 default: present
115 tenant:
116 description:
117 - Name of an existing tenant.
118 type: str
119 aliases: [ tenant_name ]
120 promiscuous:
121 description:
122 - Allow/Disallow promiscuous mode in vmm domain
123 type: str
124 choices: [ accept, reject ]
125 default: reject
126 vm_provider:
127 description:
128 - The VM platform for VMM Domains.
129 - Support for Kubernetes was added in ACI v3.0.
130 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
131 type: str
132 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix ]
133 custom_epg_name:
134 description:
135 - The custom epg name in VMM domain association.
136 type: str
137 delimiter:
138 description:
139 - The delimiter.
140 type: str
141 choices: [ "|", "~", "!", "@", "^", "+", "=", "_" ]
142 untagged_vlan:
143 description:
144 - The access vlan is untagged.
145 type: bool
146 port_binding:
147 description:
148 - The port binding method.
149 type: str
150 choices: [ dynamic, ephemeral, static ]
151 port_allocation:
152 description:
153 - The port allocation method.
154 type: str
155 choices: [ elastic, fixed ]
156 number_of_ports:
157 description:
158 - The number of ports.
159 type: int
160 forged_transmits:
161 description:
162 - Allow forged transmits. A forged transmit occurs when a network adapter starts sending out traffic that identifies itself as something else.
163 type: str
164 choices: [ accept, reject ]
165 default: reject
166 mac_changes:
167 description:
168 - Allows definition of new MAC addresses for the network adapter within the virtual machine (VM).
169 type: str
170 choices: [ accept, reject ]
171 default: reject
172 epg_cos:
173 description:
174 - The class of service (CoS).
175 - The APIC defaults to C(cos_0) when unset during creation.
176 type: str
177 choices: [ cos_0, cos_1, cos_2, cos_3, cos_4, cos_5, cos_6, cos_7 ]
178 epg_cos_preference:
179 description:
180 - The CoS preference.
181 - The APIC defaults to C(disabled) when unset during creation.
182 type: str
183 choices: [ enabled, disabled ]
184 ipam_dhcp_override:
185 description:
186 - The IP Address Management (IPAM) Dynamic Host Configuration Protocol (DHCP) override.
187 - Only applicable for Nutanix domains.
188 type: str
189 ipam_enabled:
190 description:
191 - The IPAM enabled state.
192 - Only applicable for Nutanix domains.
193 - The APIC defaults to C(false) when unset during creation.
194 type: bool
195 ipam_gateway:
196 description:
197 - The IPAM gateway.
198 - Only applicable for Nutanix domains.
199 type: str
200 lag_policy_name:
201 description:
202 - The link aggregation group (LAG) policy name.
203 type: str
204 netflow_direction:
205 description:
206 - The NetFlow monitoring direction.
207 - The APIC defaults to C(both) when unset during creation.
208 type: str
209 choices: [ both, ingress, egress ]
210 primary_encap_inner:
211 description:
212 - The primary inner encapsulation.
213 - This is used for the portgroup at the VMWare Distributed Virtual Switch (DVS).
214 - This VLAN is internal to the DVS and is used for communication between the other VMs and the AVE VM at a host.
215 - Traffic is not forwarded to the fabric over the VLAN.
216 - Only applicable for Cisco ACI Virtual Edge (AVE) domains.
217 - Accepted values range between C(1) and C(4096).
218 type: int
219 secondary_encap_inner:
220 description:
221 - The secondary inner encapsulation.
222 - This is used for the portgroup at the VMWare DVS.
223 - This VLAN is internal to the DVS and is used for communication between the other VMs and the AVE VM at a host.
224 - Traffic is not forwarded to the fabric over the VLAN.
225 - Only applicable for AVE domains.
226 - Accepted values range between C(1) and C(4096).
227 type: int
228 extends_documentation_fragment:
229 - cisco.aci.aci
230 - cisco.aci.annotation
231
232 notes:
233 - The C(tenant), C(ap), C(epg), and C(domain) used must exist before using this module in your playbook.
234 The M(cisco.aci.aci_tenant) M(cisco.aci.aci_ap), M(cisco.aci.aci_epg) M(cisco.aci.aci_domain) modules can be used for this.
235 - OpenStack VMM domains must not be created using this module. The OpenStack VMM domain is created directly
236 by the Cisco APIC Neutron plugin as part of the installation and configuration.
237 This module can be used to query status of an OpenStack VMM domain.
238 seealso:
239 - module: cisco.aci.aci_ap
240 - module: cisco.aci.aci_epg
241 - module: cisco.aci.aci_domain
242 - name: APIC Management Information Model reference
243 description: More information about the internal APIC class B(fv:RsDomAtt).
244 link: https://developer.cisco.com/docs/apic-mim-ref/
245 author:
246 - Jacob McGill (@jmcgill298)
247 - Shreyas Srish (@shrsr)
248 """
249
250 EXAMPLES = r"""
251 - name: Add a new physical domain to EPG binding
252 cisco.aci.aci_epg_to_domain:
253 host: apic
254 username: admin
255 password: SomeSecretPassword
256 tenant: anstest
257 ap: anstest
258 epg: anstest
259 domain: anstest
260 domain_type: phys
261 state: present
262 delegate_to: localhost
263
264 - name: Remove an existing physical domain to EPG binding
265 cisco.aci.aci_epg_to_domain:
266 host: apic
267 username: admin
268 password: SomeSecretPassword
269 tenant: anstest
270 ap: anstest
271 epg: anstest
272 domain: anstest
273 domain_type: phys
274 state: absent
275 delegate_to: localhost
276
277 - name: Query a specific physical domain to EPG binding
278 cisco.aci.aci_epg_to_domain:
279 host: apic
280 username: admin
281 password: SomeSecretPassword
282 tenant: anstest
283 ap: anstest
284 epg: anstest
285 domain: anstest
286 domain_type: phys
287 state: query
288 delegate_to: localhost
289 register: query_result
290
291 - name: Query all domain to EPG bindings
292 cisco.aci.aci_epg_to_domain:
293 host: apic
294 username: admin
295 password: SomeSecretPassword
296 state: query
297 delegate_to: localhost
298 register: query_result
299 """
300
301 RETURN = r"""
302 current:
303 description: The existing configuration from the APIC after the module has finished
304 returned: success
305 type: list
306 sample:
307 [
308 {
309 "fvTenant": {
310 "attributes": {
311 "descr": "Production environment",
312 "dn": "uni/tn-production",
313 "name": "production",
314 "nameAlias": "",
315 "ownerKey": "",
316 "ownerTag": ""
317 }
318 }
319 }
320 ]
321 error:
322 description: The error information as returned from the APIC
323 returned: failure
324 type: dict
325 sample:
326 {
327 "code": "122",
328 "text": "unknown managed object class foo"
329 }
330 raw:
331 description: The raw output returned by the APIC REST API (xml or json)
332 returned: parse error
333 type: str
334 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
335 sent:
336 description: The actual/minimal configuration pushed to the APIC
337 returned: info
338 type: list
339 sample:
340 {
341 "fvTenant": {
342 "attributes": {
343 "descr": "Production environment"
344 }
345 }
346 }
347 previous:
348 description: The original configuration from the APIC before the module has started
349 returned: info
350 type: list
351 sample:
352 [
353 {
354 "fvTenant": {
355 "attributes": {
356 "descr": "Production",
357 "dn": "uni/tn-production",
358 "name": "production",
359 "nameAlias": "",
360 "ownerKey": "",
361 "ownerTag": ""
362 }
363 }
364 }
365 ]
366 proposed:
367 description: The assembled configuration from the user-provided parameters
368 returned: info
369 type: dict
370 sample:
371 {
372 "fvTenant": {
373 "attributes": {
374 "descr": "Production environment",
375 "name": "production"
376 }
377 }
378 }
379 filter_string:
380 description: The filter string used for the request
381 returned: failure or debug
382 type: str
383 sample: ?rsp-prop-include=config-only
384 method:
385 description: The HTTP method used for the request to the APIC
386 returned: failure or debug
387 type: str
388 sample: POST
389 response:
390 description: The HTTP response from the APIC
391 returned: failure or debug
392 type: str
393 sample: OK (30 bytes)
394 status:
395 description: The HTTP status from the APIC
396 returned: failure or debug
397 type: int
398 sample: 200
399 url:
400 description: The HTTP url used for the request to the APIC
401 returned: failure or debug
402 type: str
403 sample: https://10.11.12.13/api/mo/uni/tn-production.json
404 """
405
406 from ansible.module_utils.basic import AnsibleModule
407 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
408 from ansible_collections.cisco.aci.plugins.module_utils.constants import COS_MAPPING, VM_PROVIDER_MAPPING
409
410
411 def main():
412 argument_spec = aci_argument_spec()
413 argument_spec.update(aci_annotation_spec())
414 argument_spec.update(
415 allow_useg=dict(type="str", choices=["encap", "useg"]),
416 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), # Not required for querying all objects
417 deploy_immediacy=dict(type="str", choices=["immediate", "lazy"]),
418 domain=dict(type="str", aliases=["domain_name", "domain_profile"]), # Not required for querying all objects
419 domain_type=dict(type="str", choices=["l2dom", "phys", "vmm"], aliases=["type"]), # Not required for querying all objects
420 encap=dict(type="int"),
421 encap_mode=dict(type="str", choices=["auto", "vlan", "vxlan"]),
422 switching_mode=dict(type="str", default="native", choices=["AVE", "native"]),
423 epg=dict(type="str", aliases=["name", "epg_name"]), # Not required for querying all objects
424 enhanced_lag_policy=dict(type="str", aliases=["lag_policy"]),
425 vmm_uplink_active=dict(type="list", elements="str"),
426 vmm_uplink_standby=dict(type="list", elements="str"),
427 netflow=dict(type="bool"),
428 primary_encap=dict(type="int"),
429 resolution_immediacy=dict(type="str", choices=["immediate", "lazy", "pre-provision"]),
430 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
431 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
432 vm_provider=dict(type="str", choices=list(VM_PROVIDER_MAPPING)),
433 promiscuous=dict(type="str", default="reject", choices=["accept", "reject"]),
434 custom_epg_name=dict(type="str"),
435 delimiter=dict(type="str", choices=["|", "~", "!", "@", "^", "+", "=", "_"]),
436 untagged_vlan=dict(type="bool"),
437 port_binding=dict(type="str", choices=["dynamic", "ephemeral", "static"]),
438 port_allocation=dict(type="str", choices=["elastic", "fixed"]),
439 number_of_ports=dict(type="int"),
440 forged_transmits=dict(type="str", default="reject", choices=["accept", "reject"]),
441 mac_changes=dict(type="str", default="reject", choices=["accept", "reject"]),
442 epg_cos=dict(type="str", choices=list(COS_MAPPING)),
443 epg_cos_preference=dict(type="str", choices=["enabled", "disabled"]),
444 ipam_dhcp_override=dict(type="str"),
445 ipam_enabled=dict(type="bool"),
446 ipam_gateway=dict(type="str"),
447 lag_policy_name=dict(type="str"),
448 netflow_direction=dict(type="str", choices=["both", "ingress", "egress"]),
449 primary_encap_inner=dict(type="int"),
450 secondary_encap_inner=dict(type="int"),
451 )
452
453 module = AnsibleModule(
454 argument_spec=argument_spec,
455 supports_check_mode=True,
456 required_if=[
457 ["domain_type", "vmm", ["vm_provider"]],
458 ["state", "absent", ["ap", "domain", "domain_type", "epg", "tenant"]],
459 ["state", "present", ["ap", "domain", "domain_type", "epg", "tenant"]],
460 ],
461 )
462
463 aci = ACIModule(module)
464
465 allow_useg = module.params.get("allow_useg")
466 ap = module.params.get("ap")
467 deploy_immediacy = module.params.get("deploy_immediacy")
468 domain = module.params.get("domain")
469 domain_type = module.params.get("domain_type")
470 vm_provider = module.params.get("vm_provider")
471 promiscuous = module.params.get("promiscuous")
472 custom_epg_name = module.params.get("custom_epg_name")
473 encap = format_vlan(aci, module.params.get("encap"))
474 encap_mode = module.params.get("encap_mode")
475 switching_mode = module.params.get("switching_mode")
476 epg = module.params.get("epg")
477 enhanced_lag_policy = module.params.get("enhanced_lag_policy")
478 vmm_uplink_active = module.params.get("vmm_uplink_active")
479 vmm_uplink_standby = module.params.get("vmm_uplink_standby")
480 netflow = aci.boolean(module.params.get("netflow"), "enabled", "disabled")
481 primary_encap = format_vlan(aci, module.params.get("primary_encap"))
482 resolution_immediacy = module.params.get("resolution_immediacy")
483 state = module.params.get("state")
484 tenant = module.params.get("tenant")
485
486 if domain_type in ["l2dom", "phys"] and vm_provider is not None:
487 module.fail_json(msg="Domain type '%s' cannot have a 'vm_provider'" % domain_type)
488
489 delimiter = module.params.get("delimiter")
490 untagged_vlan = "yes" if module.params.get("untagged_vlan") is True else "no"
491 port_binding = module.params.get("port_binding")
492 if port_binding == "static" or port_binding == "dynamic":
493 port_binding = "{0}Binding".format(port_binding)
494 port_allocation = module.params.get("port_allocation")
495 number_of_ports = module.params.get("number_of_ports")
496 forged_transmits = module.params.get("forged_transmits")
497 mac_changes = module.params.get("mac_changes")
498 epg_cos = COS_MAPPING.get(module.params.get("epg_cos"))
499 epg_cos_pref = module.params.get("epg_cos_preference")
500 ipam_dhcp_override = module.params.get("ipam_dhcp_override")
501 ipam_enabled = aci.boolean(module.params.get("ipam_enabled"))
502 ipam_gateway = module.params.get("ipam_gateway")
503 lag_policy_name = module.params.get("lag_policy_name")
504 netflow_direction = module.params.get("netflow_direction")
505 primary_encap_inner = format_vlan(aci, module.params.get("primary_encap_inner"))
506 secondary_encap_inner = format_vlan(aci, module.params.get("secondary_encap_inner"))
507
508 child_classes = None
509 child_configs = None
510
511 # Compile the full domain for URL building
512 if domain_type == "vmm":
513 epg_domain = "uni/vmmp-{0}/dom-{1}".format(VM_PROVIDER_MAPPING[vm_provider], domain)
514 child_configs = [dict(vmmSecP=dict(attributes=dict(allowPromiscuous=promiscuous, forgedTransmits=forged_transmits, macChanges=mac_changes)))]
515 # check with child classes added on all versions
516 child_classes = ["vmmSecP"]
517
518 if vmm_uplink_active is not None or vmm_uplink_standby is not None:
519 uplink_order_cont = dict(fvUplinkOrderCont=dict(attributes=dict()))
520 if vmm_uplink_active is not None:
521 uplink_order_cont["fvUplinkOrderCont"]["attributes"]["active"] = ",".join(vmm_uplink_active)
522 if vmm_uplink_standby is not None:
523 uplink_order_cont["fvUplinkOrderCont"]["attributes"]["standby"] = ",".join(vmm_uplink_standby)
524 child_configs.append(uplink_order_cont)
525 child_classes.append("fvUplinkOrderCont")
526
527 if enhanced_lag_policy is not None:
528 lag_policy = epg_domain + "/vswitchpolcont/enlacplagp-{0}".format(enhanced_lag_policy)
529 child_configs.append(
530 dict(
531 fvAEPgLagPolAtt=dict(
532 attributes=dict(annotation=""), children=[dict(fvRsVmmVSwitchEnhancedLagPol=dict(attributes=dict(annotation="", tDn=lag_policy)))]
533 )
534 )
535 )
536 child_classes.append("fvAEPgLagPolAtt")
537
538 elif domain_type == "l2dom":
539 epg_domain = "uni/l2dom-{0}".format(domain)
540 elif domain_type == "phys":
541 epg_domain = "uni/phys-{0}".format(domain)
542 else:
543 epg_domain = None
544
545 aci.construct_url(
546 root_class=dict(
547 aci_class="fvTenant",
548 aci_rn="tn-{0}".format(tenant),
549 module_object=tenant,
550 target_filter={"name": tenant},
551 ),
552 subclass_1=dict(
553 aci_class="fvAp",
554 aci_rn="ap-{0}".format(ap),
555 module_object=ap,
556 target_filter={"name": ap},
557 ),
558 subclass_2=dict(
559 aci_class="fvAEPg",
560 aci_rn="epg-{0}".format(epg),
561 module_object=epg,
562 target_filter={"name": epg},
563 ),
564 subclass_3=dict(
565 aci_class="fvRsDomAtt",
566 aci_rn="rsdomAtt-[{0}]".format(epg_domain),
567 module_object=epg_domain,
568 target_filter={"tDn": epg_domain},
569 ),
570 child_classes=child_classes,
571 )
572
573 aci.get_existing()
574
575 if state == "present":
576 aci.payload(
577 aci_class="fvRsDomAtt",
578 class_config=dict(
579 classPref=allow_useg,
580 encap=encap,
581 encapMode=encap_mode,
582 switchingMode=switching_mode,
583 instrImedcy=deploy_immediacy,
584 netflowPref=netflow,
585 primaryEncap=primary_encap,
586 resImedcy=resolution_immediacy,
587 customEpgName=custom_epg_name,
588 delimiter=delimiter,
589 untagged=untagged_vlan,
590 bindingType=port_binding,
591 portAllocation=port_allocation,
592 numPorts=number_of_ports,
593 epgCos=epg_cos,
594 epgCosPref=epg_cos_pref,
595 ipamDhcpOverride=ipam_dhcp_override,
596 ipamEnabled=ipam_enabled,
597 ipamGateway=ipam_gateway,
598 lagPolicyName=lag_policy_name,
599 netflowDir=netflow_direction,
600 primaryEncapInner=primary_encap_inner,
601 secondaryEncapInner=secondary_encap_inner,
602 ),
603 child_configs=child_configs,
604 )
605
606 aci.get_diff(aci_class="fvRsDomAtt")
607
608 aci.post_config()
609
610 elif state == "absent":
611 aci.delete_config()
612
613 aci.exit_json()
614
615
616 def format_vlan(aci, vlan):
617 if vlan in range(1, 4097):
618 return "vlan-{0}".format(vlan)
619 if vlan is not None:
620 aci.fail_json(msg="Valid VLAN assignments are from 1 to 4096")
621 return vlan
622
623
624 if __name__ == "__main__":
625 main()