Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Gaspard Micol (@gmicol) <[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_node_block
16 short_description: Manage Node Block (infra:NodeBlk)
17 description:
18 - Manage Node Blocks on Cisco ACI fabrics.
19 - A node block is a range of nodes. Each node block begins with the first port and ends with the last port.
20 options:
21 switch_profile:
22 description:
23 - The name of the Fabric access policy leaf/spine switch profile.
24 type: str
25 aliases:
26 - leaf_profile_name
27 - leaf_profile
28 - switch_profile_name
29 - spine_switch_profile
30 - spine_switch_profile_name
31 access_port_selector:
32 description:
33 - The name of the Fabric access policy leaf/spine switch port selector.
34 type: str
35 aliases: [ access_port_selector_name, port_selector, port_selector_name ]
36 node_block:
37 description:
38 - The name of the Node Block.
39 type: str
40 aliases: [ node_block_name, name ]
41 description:
42 description:
43 - The description for the Node Block.
44 type: str
45 aliases: [ node_block_description ]
46 from_port:
47 description:
48 - The beginning of the port range block for the Node Block.
49 type: str
50 aliases: [ from, from_port_range ]
51 to_port:
52 description:
53 - The end of the port range block for the Node Block.
54 type: str
55 aliases: [ to, to_port_range ]
56 type_node:
57 description:
58 - The type of Node Block to be created under respective access port.
59 type: str
60 choices: [ leaf, spine ]
61 aliases: [ type ]
62 state:
63 description:
64 - Use C(present) or C(absent) for adding or removing.
65 - Use C(query) for listing an object or multiple objects.
66 type: str
67 choices: [ absent, present, query ]
68 default: present
69 extends_documentation_fragment:
70 - cisco.aci.aci
71 - cisco.aci.annotation
72
73 notes:
74 - If Adding a port block on an access leaf switch port selector of I(type) C(leaf),
75 The I(switch_profile) and I(access_port_selector) must exist before using this module in your playbook.
76 The M(cisco.aci.aci_switch_policy_leaf_profile) and M(cisco.aci.aci_switch_leaf_selector) modules can be used for this.
77 - If Adding a port block on an access switch port selector of C(type) C(spine),
78 The I(switch_profile) and I(access_port_selector) must exist before using this module in your playbook.
79 The M(cisco.aci.aci_access_spine_switch_profile) and M(cisco.aci.aci_access_spine_switch_selector) modules can be used for this.
80 seealso:
81 - module: cisco.aci.aci_switch_policy_leaf_profile
82 - module: cisco.aci.aci_switch_leaf_selector
83 - module: cisco.aci.aci_access_spine_switch_profile
84 - module: cisco.aci.aci_access_spine_switch_selector
85 - name: APIC Management Information Model reference
86 description: More information about the internal APIC classes B(infra:NodeBlk).
87 link: https://developer.cisco.com/docs/apic-mim-ref/
88 author:
89 - Gaspard Micol (@gmicol)
90 """
91
92 EXAMPLES = r"""
93 - name: Add a new Node Block associated to a switch policy leaf profile selector
94 cisco.aci.aci_node_block:
95 host: apic
96 username: admin
97 password: SomeSecretPassword
98 switch_profile: my_leaf_switch_profile
99 access_port_selector: my_leaf_switch_selector
100 node_block: my_node_block
101 from_port: 1011
102 to_port: 1011
103 type_node: leaf
104 state: present
105 delegate_to: localhost
106
107 - name: Add a new Node Block associated to a switch policy spine profile selector
108 cisco.aci.aci_node_block:
109 host: apic
110 username: admin
111 password: SomeSecretPassword
112 switch_profile: my_spine_switch_profile
113 access_port_selector: my_spine_switch_selector
114 node_block: my_node_block
115 from_port: 1012
116 to_port: 1012
117 type_node: spine
118 state: present
119 delegate_to: localhost
120
121 - name: Query a Node Block associated to a switch policy leaf profile selector
122 cisco.aci.aci_node_block:
123 host: apic
124 username: admin
125 password: SomeSecretPassword
126 switch_profile: my_leaf_switch_profile
127 access_port_selector: my_leaf_switch_selector
128 node_block: my_node_block
129 state: query
130 delegate_to: localhost
131 register: query_result
132
133 - name: Query all Node Blocks under the switch policy leaf profile selector
134 cisco.aci.aci_node_block:
135 host: apic
136 username: admin
137 password: SomeSecretPassword
138 switch_profile: my_leaf_switch_profile
139 access_port_selector: my_leaf_switch_selector
140 state: query
141 delegate_to: localhost
142 register: query_result
143
144 - name: Query all Node Blocks
145 cisco.aci.aci_node_block:
146 host: apic
147 username: admin
148 password: SomeSecretPassword
149 state: query
150 delegate_to: localhost
151 register: query_result
152
153 - name: Remove a Node Block associated to a switch policy leaf profile selector
154 cisco.aci.aci_node_block:
155 host: apic
156 username: admin
157 password: SomeSecretPassword
158 switch_profile: my_leaf_switch_profile
159 access_port_selector: my_leaf_switch_selector
160 node_block: my_node_block
161 type_node: leaf
162 state: absent
163 delegate_to: localhost
164 """
165
166 RETURN = r"""
167 current:
168 description: The existing configuration from the APIC after the module has finished
169 returned: success
170 type: list
171 sample:
172 [
173 {
174 "fvTenant": {
175 "attributes": {
176 "descr": "Production environment",
177 "dn": "uni/tn-production",
178 "name": "production",
179 "nameAlias": "",
180 "ownerKey": "",
181 "ownerTag": ""
182 }
183 }
184 }
185 ]
186 error:
187 description: The error information as returned from the APIC
188 returned: failure
189 type: dict
190 sample:
191 {
192 "code": "122",
193 "text": "unknown managed object class foo"
194 }
195 raw:
196 description: The raw output returned by the APIC REST API (xml or json)
197 returned: parse error
198 type: str
199 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
200 sent:
201 description: The actual/minimal configuration pushed to the APIC
202 returned: info
203 type: list
204 sample:
205 {
206 "fvTenant": {
207 "attributes": {
208 "descr": "Production environment"
209 }
210 }
211 }
212 previous:
213 description: The original configuration from the APIC before the module has started
214 returned: info
215 type: list
216 sample:
217 [
218 {
219 "fvTenant": {
220 "attributes": {
221 "descr": "Production",
222 "dn": "uni/tn-production",
223 "name": "production",
224 "nameAlias": "",
225 "ownerKey": "",
226 "ownerTag": ""
227 }
228 }
229 }
230 ]
231 proposed:
232 description: The assembled configuration from the user-provided parameters
233 returned: info
234 type: dict
235 sample:
236 {
237 "fvTenant": {
238 "attributes": {
239 "descr": "Production environment",
240 "name": "production"
241 }
242 }
243 }
244 filter_string:
245 description: The filter string used for the request
246 returned: failure or debug
247 type: str
248 sample: ?rsp-prop-include=config-only
249 method:
250 description: The HTTP method used for the request to the APIC
251 returned: failure or debug
252 type: str
253 sample: POST
254 response:
255 description: The HTTP response from the APIC
256 returned: failure or debug
257 type: str
258 sample: OK (30 bytes)
259 status:
260 description: The HTTP status from the APIC
261 returned: failure or debug
262 type: int
263 sample: 200
264 url:
265 description: The HTTP url used for the request to the APIC
266 returned: failure or debug
267 type: str
268 sample: https://10.11.12.13/api/mo/uni/tn-production.json
269 """
270
271 from ansible.module_utils.basic import AnsibleModule
272 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
273
274
275 def main():
276 argument_spec = aci_argument_spec()
277 argument_spec.update(aci_annotation_spec())
278 argument_spec.update(
279 switch_profile=dict(
280 type="str",
281 aliases=[
282 "leaf_profile_name",
283 "leaf_profile",
284 "switch_profile_name",
285 "spine_switch_profile",
286 "spine_switch_profile_name",
287 ],
288 ), # Not required for querying all objects
289 access_port_selector=dict(
290 type="str",
291 aliases=[
292 "access_port_selector_name",
293 "port_selector",
294 "port_selector_name",
295 ],
296 ), # Not required for querying all objects
297 node_block=dict(type="str", aliases=["node_block_name", "name"]), # Not required for querying all objects
298 description=dict(type="str", aliases=["node_block_description"]),
299 from_port=dict(type="str", aliases=["from", "from_port_range"]),
300 to_port=dict(type="str", aliases=["to", "to_port_range"]),
301 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
302 type_node=dict(type="str", choices=["leaf", "spine"], aliases=["type"]), # Not required for querying all objects
303 )
304
305 module = AnsibleModule(
306 argument_spec=argument_spec,
307 supports_check_mode=True,
308 required_if=[
309 ["state", "absent", ["switch_profile", "access_port_selector", "node_block", "type_node"]],
310 ["state", "present", ["switch_profile", "access_port_selector", "node_block", "type_node"]],
311 ],
312 )
313
314 switch_profile = module.params.get("switch_profile")
315 access_port_selector = module.params.get("access_port_selector")
316 node_block = module.params.get("node_block")
317 description = module.params.get("description")
318 from_port = module.params.get("from_port")
319 to_port = module.params.get("to_port")
320 state = module.params.get("state")
321 type_node = module.params.get("type_node")
322
323 aci = ACIModule(module)
324
325 if type_node == "spine":
326 subclass_1 = dict(
327 aci_class="infraSpineP",
328 aci_rn="spprof-{0}".format(switch_profile),
329 module_object=switch_profile,
330 target_filter={"name": switch_profile},
331 )
332 subclass_2 = dict(
333 aci_class="infraSpineS",
334 aci_rn="spines-{0}-typ-range".format(access_port_selector),
335 module_object=access_port_selector,
336 target_filter={"name": access_port_selector},
337 )
338 else:
339 subclass_1 = dict(
340 aci_class="infraNodeP",
341 aci_rn="nprof-{0}".format(switch_profile),
342 module_object=switch_profile,
343 target_filter={"name": switch_profile},
344 )
345 subclass_2 = dict(
346 aci_class="infraLeafS",
347 aci_rn="leaves-{0}-typ-range".format(access_port_selector),
348 module_object=access_port_selector,
349 target_filter={"name": access_port_selector},
350 )
351 aci.construct_url(
352 root_class=dict(
353 aci_class="infraInfra",
354 aci_rn="infra",
355 ),
356 subclass_1=subclass_1,
357 subclass_2=subclass_2,
358 subclass_3=dict(
359 aci_class="infraNodeBlk",
360 aci_rn="nodeblk-{0}".format(node_block),
361 module_object=node_block,
362 target_filter={"name": node_block},
363 ),
364 )
365
366 aci.get_existing()
367
368 if state == "present":
369 aci.payload(
370 aci_class="infraNodeBlk",
371 class_config=dict(
372 descr=description,
373 name=node_block,
374 from_=from_port,
375 to_=to_port,
376 ),
377 )
378
379 aci.get_diff(aci_class="infraNodeBlk")
380
381 aci.post_config()
382
383 elif state == "absent":
384 aci.delete_config()
385
386 aci.exit_json()
387
388
389 if __name__ == "__main__":
390 main()