Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Christian Kolrep <[email protected]>
5 # Copyright: (c) 2024, Akini Ross <[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
17 short_description: Manage End Point Groups (EPG) objects (fv:AEPg)
18 description:
19 - Manage End Point Groups (EPG) on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - Name of an existing tenant.
24 type: str
25 aliases: [ tenant_name ]
26 ap:
27 description:
28 - Name of an existing application network profile, that will contain the EPGs.
29 type: str
30 aliases: [ app_profile, app_profile_name ]
31 epg:
32 description:
33 - Name of the end point group.
34 type: str
35 aliases: [ epg_name, name ]
36 bd:
37 description:
38 - Name of the bridge domain being associated with the EPG.
39 type: str
40 aliases: [ bd_name, bridge_domain ]
41 priority:
42 description:
43 - The QoS class.
44 - The APIC defaults to C(unspecified) when unset during creation.
45 type: str
46 choices: [ level1, level2, level3, unspecified ]
47 intra_epg_isolation:
48 description:
49 - The Intra EPG Isolation.
50 - The APIC defaults to C(unenforced) when unset during creation.
51 type: str
52 choices: [ enforced, unenforced ]
53 description:
54 description:
55 - Description for the EPG.
56 type: str
57 aliases: [ descr ]
58 fwd_control:
59 description:
60 - The forwarding control used by the EPG.
61 - The APIC defaults to C(none) when unset during creation.
62 type: str
63 choices: [ none, proxy-arp ]
64 preferred_group:
65 description:
66 - Whether or not the EPG is part of the Preferred Group and can communicate without contracts.
67 - This is very convenient for migration scenarios, or when ACI is used for network automation but not for policy.
68 - The APIC defaults to C(false) when unset during creation.
69 type: bool
70 monitoring_policy:
71 description:
72 - The name of the monitoring policy.
73 type: str
74 custom_qos_policy:
75 description:
76 - The name of the custom Quality of Service policy.
77 type: str
78 useg:
79 description:
80 - Use C(yes) to create uSeg EPG and C(no) is used to create Application EPG.
81 type: str
82 choices: [ 'yes', 'no' ]
83 match:
84 description:
85 - The match type of the default Block Statement (fv:Crtrn).
86 - The APIC defaults to C(any) when unset during creation.
87 type: str
88 choices: [ any, all ]
89 precedence:
90 description:
91 - The Block Statement(fv:Crtrn) Precedence to resolve equal matches between micro segmented EPGs.
92 - The APIC defaults to C(0) when unset during creation.
93 type: int
94 state:
95 description:
96 - Use C(present) or C(absent) for adding or removing.
97 - Use C(query) for listing an object or multiple objects.
98 type: str
99 choices: [ absent, present, query ]
100 default: present
101 name_alias:
102 description:
103 - The alias for the current object. This relates to the nameAlias field in ACI.
104 type: str
105 extends_documentation_fragment:
106 - cisco.aci.aci
107 - cisco.aci.annotation
108
109 notes:
110 - The C(tenant) and C(app_profile) used must exist before using this module in your playbook.
111 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_ap) modules can be used for this.
112 seealso:
113 - module: cisco.aci.aci_tenant
114 - module: cisco.aci.aci_ap
115 - name: APIC Management Information Model reference
116 description: More information about the internal APIC class B(fv:AEPg).
117 link: https://developer.cisco.com/docs/apic-mim-ref/
118 author:
119 - Swetha Chunduri (@schunduri)
120 - Shreyas Srish (@shrsr)
121 - Christian Kolrep (@Christian-Kolrep)
122 - Akini Ross (@akinross)
123 """
124
125 EXAMPLES = r"""
126 - name: Add a new EPG
127 cisco.aci.aci_epg:
128 host: apic
129 username: admin
130 password: SomeSecretPassword
131 tenant: production
132 ap: intranet
133 epg: web_epg
134 description: Web Intranet EPG
135 bd: prod_bd
136 monitoring_policy: default
137 preferred_group: true
138 state: present
139 delegate_to: localhost
140
141 - name: Add a new uSeg EPG
142 cisco.aci.aci_epg:
143 host: apic
144 username: admin
145 password: SomeSecretPassword
146 tenant: production
147 ap: intranet
148 epg: web_epg
149 description: Web Intranet EPG
150 bd: prod_bd
151 monitoring_policy: default
152 preferred_group: true
153 useg: 'yes'
154 state: present
155 delegate_to: localhost
156
157 - name: Add a uSeg EPG with block statement match and precedence
158 cisco.aci.aci_epg:
159 host: apic
160 username: admin
161 password: SomeSecretPassword
162 tenant: production
163 ap: intranet
164 epg: web_epg
165 description: Web Intranet EPG
166 bd: prod_bd
167 monitoring_policy: default
168 preferred_group: true
169 useg: 'yes'
170 match: all
171 precedence: 1
172 state: present
173 delegate_to: localhost
174
175 - name: Query an EPG
176 cisco.aci.aci_epg:
177 host: apic
178 username: admin
179 password: SomeSecretPassword
180 tenant: production
181 ap: ticketing
182 epg: web_epg
183 state: query
184 delegate_to: localhost
185 register: query_result
186
187 - name: Query all EPGs
188 cisco.aci.aci_epg:
189 host: apic
190 username: admin
191 password: SomeSecretPassword
192 state: query
193 delegate_to: localhost
194 register: query_result
195
196 - name: Query all EPGs with a Specific Name
197 cisco.aci.aci_epg:
198 host: apic
199 username: admin
200 password: SomeSecretPassword
201 validate_certs: false
202 epg: web_epg
203 state: query
204 delegate_to: localhost
205 register: query_result
206
207 - name: Query all EPGs of an App Profile
208 cisco.aci.aci_epg:
209 host: apic
210 username: admin
211 password: SomeSecretPassword
212 validate_certs: false
213 ap: ticketing
214 state: query
215 delegate_to: localhost
216 register: query_result
217
218 - name: Remove an EPG
219 cisco.aci.aci_epg:
220 host: apic
221 username: admin
222 password: SomeSecretPassword
223 validate_certs: false
224 tenant: production
225 app_profile: intranet
226 epg: web_epg
227 monitoring_policy: default
228 state: absent
229 delegate_to: localhost
230 """
231
232 RETURN = r"""
233 current:
234 description: The existing configuration from the APIC after the module has finished
235 returned: success
236 type: list
237 sample:
238 [
239 {
240 "fvTenant": {
241 "attributes": {
242 "descr": "Production environment",
243 "dn": "uni/tn-production",
244 "name": "production",
245 "nameAlias": "",
246 "ownerKey": "",
247 "ownerTag": ""
248 }
249 }
250 }
251 ]
252 error:
253 description: The error information as returned from the APIC
254 returned: failure
255 type: dict
256 sample:
257 {
258 "code": "122",
259 "text": "unknown managed object class foo"
260 }
261 raw:
262 description: The raw output returned by the APIC REST API (xml or json)
263 returned: parse error
264 type: str
265 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
266 sent:
267 description: The actual/minimal configuration pushed to the APIC
268 returned: info
269 type: list
270 sample:
271 {
272 "fvTenant": {
273 "attributes": {
274 "descr": "Production environment"
275 }
276 }
277 }
278 previous:
279 description: The original configuration from the APIC before the module has started
280 returned: info
281 type: list
282 sample:
283 [
284 {
285 "fvTenant": {
286 "attributes": {
287 "descr": "Production",
288 "dn": "uni/tn-production",
289 "name": "production",
290 "nameAlias": "",
291 "ownerKey": "",
292 "ownerTag": ""
293 }
294 }
295 }
296 ]
297 proposed:
298 description: The assembled configuration from the user-provided parameters
299 returned: info
300 type: dict
301 sample:
302 {
303 "fvTenant": {
304 "attributes": {
305 "descr": "Production environment",
306 "name": "production"
307 }
308 }
309 }
310 filter_string:
311 description: The filter string used for the request
312 returned: failure or debug
313 type: str
314 sample: ?rsp-prop-include=config-only
315 method:
316 description: The HTTP method used for the request to the APIC
317 returned: failure or debug
318 type: str
319 sample: POST
320 response:
321 description: The HTTP response from the APIC
322 returned: failure or debug
323 type: str
324 sample: OK (30 bytes)
325 status:
326 description: The HTTP status from the APIC
327 returned: failure or debug
328 type: int
329 sample: 200
330 url:
331 description: The HTTP url used for the request to the APIC
332 returned: failure or debug
333 type: str
334 sample: https://10.11.12.13/api/mo/uni/tn-production.json
335 """
336
337 from ansible.module_utils.basic import AnsibleModule
338 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
339
340
341 def main():
342 argument_spec = aci_argument_spec()
343 argument_spec.update(aci_annotation_spec())
344 argument_spec.update(
345 epg=dict(type="str", aliases=["epg_name", "name"]), # Not required for querying all objects
346 bd=dict(type="str", aliases=["bd_name", "bridge_domain"]),
347 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]), # Not required for querying all objects
348 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
349 description=dict(type="str", aliases=["descr"]),
350 priority=dict(type="str", choices=["level1", "level2", "level3", "unspecified"]),
351 intra_epg_isolation=dict(choices=["enforced", "unenforced"]),
352 fwd_control=dict(type="str", choices=["none", "proxy-arp"]),
353 preferred_group=dict(type="bool"),
354 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
355 name_alias=dict(type="str"),
356 monitoring_policy=dict(type="str"),
357 custom_qos_policy=dict(type="str"),
358 useg=dict(type="str", choices=["yes", "no"]),
359 match=dict(type="str", choices=["all", "any"]),
360 precedence=dict(type="int"),
361 )
362
363 module = AnsibleModule(
364 argument_spec=argument_spec,
365 supports_check_mode=True,
366 required_if=[
367 ["state", "absent", ["ap", "epg", "tenant"]],
368 ["state", "present", ["ap", "epg", "tenant"]],
369 ],
370 )
371
372 aci = ACIModule(module)
373
374 epg = module.params.get("epg")
375 bd = module.params.get("bd")
376 description = module.params.get("description")
377 priority = module.params.get("priority")
378 intra_epg_isolation = module.params.get("intra_epg_isolation")
379 fwd_control = module.params.get("fwd_control")
380 preferred_group = aci.boolean(module.params.get("preferred_group"), "include", "exclude")
381 state = module.params.get("state")
382 tenant = module.params.get("tenant")
383 ap = module.params.get("ap")
384 name_alias = module.params.get("name_alias")
385 monitoring_policy = module.params.get("monitoring_policy")
386 custom_qos_policy = module.params.get("custom_qos_policy")
387 useg = module.params.get("useg")
388 match = module.params.get("match")
389 precedence = module.params.get("precedence")
390
391 child_configs = [dict(fvRsBd=dict(attributes=dict(tnFvBDName=bd))), dict(fvRsAEPgMonPol=dict(attributes=dict(tnMonEPGPolName=monitoring_policy)))]
392
393 if custom_qos_policy is not None:
394 child_configs.append(dict(fvRsCustQosPol=dict(attributes=dict(tnQosCustomPolName=custom_qos_policy))))
395
396 aci.construct_url(
397 root_class=dict(
398 aci_class="fvTenant",
399 aci_rn="tn-{0}".format(tenant),
400 module_object=tenant,
401 target_filter={"name": tenant},
402 ),
403 subclass_1=dict(
404 aci_class="fvAp",
405 aci_rn="ap-{0}".format(ap),
406 module_object=ap,
407 target_filter={"name": ap},
408 ),
409 subclass_2=dict(
410 aci_class="fvAEPg",
411 aci_rn="epg-{0}".format(epg),
412 module_object=epg,
413 target_filter={"name": epg},
414 ),
415 child_classes=["fvRsBd", "fvRsAEPgMonPol", "fvRsCustQosPol", "fvCrtrn"],
416 )
417
418 aci.get_existing()
419
420 if state == "present":
421 if useg is not None and aci.existing and aci.existing[0]["fvAEPg"]["attributes"]["isAttrBasedEPg"] != useg:
422 module.fail_json(msg="Changing attribute useg on existing EPG is not supported.")
423 if useg == "yes":
424 child_configs.append(dict(fvCrtrn=dict(attributes=dict(name="default", match=match, prec=precedence))))
425
426 aci.payload(
427 aci_class="fvAEPg",
428 class_config=dict(
429 name=epg,
430 descr=description,
431 prio=priority,
432 pcEnfPref=intra_epg_isolation,
433 fwdCtrl=fwd_control,
434 prefGrMemb=preferred_group,
435 nameAlias=name_alias,
436 isAttrBasedEPg=useg,
437 ),
438 child_configs=child_configs,
439 )
440
441 aci.get_diff(aci_class="fvAEPg")
442
443 aci.post_config()
444
445 elif state == "absent":
446 aci.delete_config()
447
448 aci.exit_json()
449
450
451 if __name__ == "__main__":
452 main()