Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Sudhakar Shet Kudtarkar (@kudtarkar1)
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": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_l2out
17 short_description: Manage Layer2 Out (L2Out) objects (l2ext:Out)
18 description:
19 - Manage Layer2 Out configuration on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - Name of an existing tenant.
24 type: str
25 l2out:
26 description:
27 - The name of outer layer2.
28 type: str
29 aliases: [ 'name' ]
30 description:
31 description:
32 - Description for the L2Out.
33 type: str
34 bd:
35 description:
36 - Name of the Bridge domain which is associted with the L2Out.
37 type: str
38 domain:
39 description:
40 - Name of the external L2 Domain that is being associated with L2Out.
41 type: str
42 vlan:
43 description:
44 - The VLAN which is being associated with the L2Out.
45 type: int
46 state:
47 description:
48 - Use C(present) or C(absent) for adding or removing.
49 - Use C(query) for listing an object or multiple objects.
50 type: str
51 choices: [ absent, present, query ]
52 default: present
53 name_alias:
54 description:
55 - The alias for the current object. This relates to the nameAlias field in ACI.
56 type: str
57 extends_documentation_fragment:
58 - cisco.aci.aci
59 - cisco.aci.annotation
60 - cisco.aci.owner
61
62 notes:
63 - The C(tenant) must exist before using this module in your playbook.
64 The M(cisco.aci.aci_tenant) modules can be used for this.
65 seealso:
66 - name: APIC Management Information Model reference
67 description: More information about the internal APIC class B(l2ext:Out).
68 link: https://developer.cisco.com/docs/apic-mim-ref/
69 author:
70 - Sudhakar Shet Kudtarkar (@kudtarkar1)
71 - Shreyas Srish (@shrsr)
72 """
73
74 EXAMPLES = r"""
75 - name: Add a new L2Out
76 cisco.aci.aci_l2out:
77 host: apic
78 username: admin
79 password: SomeSecretePassword
80 tenant: Auto-Demo
81 l2out: l2out
82 description: via Ansible
83 bd: bd1
84 domain: l2Dom
85 vlan: 3200
86 state: present
87 delegate_to: localhost
88
89 - name: Remove an L2Out
90 cisco.aci.aci_l2out:
91 host: apic
92 username: admin
93 password: SomeSecretePassword
94 tenant: Auto-Demo
95 l2out: l2out
96 state: absent
97 delegate_to: localhost
98
99 - name: Query an L2Out
100 cisco.aci.aci_l2out:
101 host: apic
102 username: admin
103 password: SomeSecretePassword
104 tenant: Auto-Demo
105 l2out: l2out
106 state: query
107 delegate_to: localhost
108 register: query_result
109
110 - name: Query all L2Outs in a specific tenant
111 cisco.aci.aci_l2out:
112 host: apic
113 username: admin
114 password: SomeSecretePassword
115 tenant: Auto-Demo
116 state: query
117 delegate_to: localhost
118 register: query_result
119 """
120
121 RETURN = r"""
122 current:
123 description: The existing configuration from the APIC after the module has finished
124 returned: success
125 type: list
126 sample:
127 [
128 {
129 "fvTenant": {
130 "attributes": {
131 "descr": "Production environment",
132 "dn": "uni/tn-production",
133 "name": "production",
134 "nameAlias": "",
135 "ownerKey": "",
136 "ownerTag": ""
137 }
138 }
139 }
140 ]
141 error:
142 description: The error information as returned from the APIC
143 returned: failure
144 type: dict
145 sample:
146 {
147 "code": "122",
148 "text": "unknown managed object class foo"
149 }
150 raw:
151 description: The raw output returned by the APIC REST API (xml or json)
152 returned: parse error
153 type: str
154 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
155 sent:
156 description: The actual/minimal configuration pushed to the APIC
157 returned: info
158 type: list
159 sample:
160 {
161 "fvTenant": {
162 "attributes": {
163 "descr": "Production environment"
164 }
165 }
166 }
167 previous:
168 description: The original configuration from the APIC before the module has started
169 returned: info
170 type: list
171 sample:
172 [
173 {
174 "fvTenant": {
175 "attributes": {
176 "descr": "Production",
177 "dn": "uni/tn-production",
178 "name": "production",
179 "nameAlias": "",
180 "ownerKey": "",
181 "ownerTag": ""
182 }
183 }
184 }
185 ]
186 proposed:
187 description: The assembled configuration from the user-provided parameters
188 returned: info
189 type: dict
190 sample:
191 {
192 "fvTenant": {
193 "attributes": {
194 "descr": "Production environment",
195 "name": "production"
196 }
197 }
198 }
199 filter_string:
200 description: The filter string used for the request
201 returned: failure or debug
202 type: str
203 sample: ?rsp-prop-include=config-only
204 method:
205 description: The HTTP method used for the request to the APIC
206 returned: failure or debug
207 type: str
208 sample: POST
209 response:
210 description: The HTTP response from the APIC
211 returned: failure or debug
212 type: str
213 sample: OK (30 bytes)
214 status:
215 description: The HTTP status from the APIC
216 returned: failure or debug
217 type: int
218 sample: 200
219 url:
220 description: The HTTP url used for the request to the APIC
221 returned: failure or debug
222 type: str
223 sample: https://10.11.12.13/api/mo/uni/tn-production.json
224 """
225
226 from ansible.module_utils.basic import AnsibleModule
227 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
228
229
230 def main():
231 argument_spec = aci_argument_spec()
232 argument_spec.update(aci_annotation_spec())
233 argument_spec.update(aci_owner_spec())
234 argument_spec.update(
235 bd=dict(type="str"),
236 l2out=dict(type="str", aliases=["name"]),
237 domain=dict(type="str"),
238 vlan=dict(type="int"),
239 description=dict(type="str"),
240 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
241 tenant=dict(type="str"),
242 name_alias=dict(type="str"),
243 )
244
245 module = AnsibleModule(
246 argument_spec=argument_spec,
247 supports_check_mode=True,
248 required_if=[
249 ["state", "absent", ["l2out", "tenant"]],
250 ["state", "present", ["bd", "l2out", "tenant", "domain", "vlan"]],
251 ],
252 )
253
254 bd = module.params.get("bd")
255 l2out = module.params.get("l2out")
256 description = module.params.get("description")
257 domain = module.params.get("domain")
258 vlan = module.params.get("vlan")
259 state = module.params.get("state")
260 tenant = module.params.get("tenant")
261 name_alias = module.params.get("name_alias")
262 child_classes = ["l2extRsEBd", "l2extRsL2DomAtt", "l2extLNodeP"]
263
264 aci = ACIModule(module)
265 aci.construct_url(
266 root_class=dict(
267 aci_class="fvTenant",
268 aci_rn="tn-{0}".format(tenant),
269 module_object=tenant,
270 target_filter={"name": tenant},
271 ),
272 subclass_1=dict(
273 aci_class="l2extOut",
274 aci_rn="l2out-{0}".format(l2out),
275 module_object=l2out,
276 target_filter={"name": l2out},
277 ),
278 child_classes=child_classes,
279 )
280
281 aci.get_existing()
282
283 if state == "present":
284 child_configs = [
285 dict(l2extRsL2DomAtt=dict(attributes=dict(tDn="uni/l2dom-{0}".format(domain)))),
286 dict(l2extRsEBd=dict(attributes=dict(tnFvBDName=bd, encap="vlan-{0}".format(vlan)))),
287 ]
288
289 aci.payload(
290 aci_class="l2extOut",
291 class_config=dict(name=l2out, descr=description, dn="uni/tn-{0}/l2out-{1}".format(tenant, l2out), nameAlias=name_alias),
292 child_configs=child_configs,
293 )
294
295 aci.get_diff(aci_class="l2extOut")
296
297 aci.post_config()
298
299 elif state == "absent":
300 aci.delete_config()
301
302 aci.exit_json()
303
304
305 if __name__ == "__main__":
306 main()