ansible  2.9.27
About: Ansible is an IT Configuration Management, Deployment \
About: Ansible (2.x) is an IT Configuration Management, Deployment & Orchestration tool.
ansible download page.
  Fossies Dox: ansible-2.9.27.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

aci_encap_pool_range.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6from __future__ import absolute_import, division, print_function
7__metaclass__ = type
8
9ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'certified'}
12
13DOCUMENTATION = r'''
14---
15module: aci_encap_pool_range
16short_description: Manage encap ranges assigned to pools (fvns:EncapBlk, fvns:VsanEncapBlk)
17description:
18- Manage vlan, vxlan, and vsan ranges that are assigned to pools on Cisco ACI fabrics.
19version_added: '2.5'
20options:
21 allocation_mode:
22 description:
23 - The method used for allocating encaps to resources.
24 - Only vlan and vsan support allocation modes.
25 type: str
26 choices: [ dynamic, inherit, static]
27 aliases: [ mode ]
28 description:
29 description:
30 - Description for the pool range.
31 type: str
32 aliases: [ descr ]
33 pool:
34 description:
35 - The name of the pool that the range should be assigned to.
36 type: str
37 aliases: [ pool_name ]
38 pool_allocation_mode:
39 description:
40 - The method used for allocating encaps to resources.
41 - Only vlan and vsan support allocation modes.
42 type: str
43 choices: [ dynamic, static]
44 aliases: [ pool_mode ]
45 pool_type:
46 description:
47 - The encap type of C(pool).
48 type: str
49 required: yes
50 aliases: [ type ]
51 choices: [ vlan, vxlan, vsan]
52 range_end:
53 description:
54 - The end of encap range.
55 type: int
56 aliases: [ end ]
57 range_name:
58 description:
59 - The name to give to the encap range.
60 type: str
61 aliases: [ name, range ]
62 range_start:
63 description:
64 - The start of the encap range.
65 type: int
66 aliases: [ start ]
67 state:
68 description:
69 - Use C(present) or C(absent) for adding or removing.
70 - Use C(query) for listing an object or multiple objects.
71 type: str
72 choices: [ absent, present, query ]
73 default: present
74extends_documentation_fragment: aci
75notes:
76- The C(pool) must exist in order to add or delete a range.
77seealso:
78- module: aci_encap_pool
79- module: aci_vlan_pool_encap_block
80- name: APIC Management Information Model reference
81 description: More information about the internal APIC classes B(fvns:EncapBlk) and B(fvns:VsanEncapBlk).
82 link: https://developer.cisco.com/docs/apic-mim-ref/
83author:
84- Jacob McGill (@jmcgill298)
85'''
86
87EXAMPLES = r'''
88- name: Add a new VLAN pool range
89 aci_encap_pool_range:
90 host: apic
91 username: admin
92 password: SomeSecretPassword
93 pool: production
94 pool_type: vlan
95 pool_allocation_mode: static
96 range_name: anstest
97 range_start: 20
98 range_end: 40
99 allocation_mode: inherit
100 state: present
101 delegate_to: localhost
102
103- name: Remove a VLAN pool range
104 aci_encap_pool_range:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 pool: production
109 pool_type: vlan
110 pool_allocation_mode: static
111 range_name: anstest
112 range_start: 20
113 range_end: 40
114 state: absent
115 delegate_to: localhost
116
117- name: Query a VLAN range
118 aci_encap_pool_range:
119 host: apic
120 username: admin
121 password: SomeSecretPassword
122 pool: production
123 pool_type: vlan
124 pool_allocation_mode: static
125 range_name: anstest
126 range_start: 20
127 range_end: 50
128 state: query
129 delegate_to: localhost
130 register: query_result
131
132- name: Query a VLAN pool for ranges by range_name
133 aci_encap_pool_range:
134 host: apic
135 username: admin
136 password: SomeSecretPassword
137 pool_type: vlan
138 range_name: anstest
139 state: query
140 delegate_to: localhost
141 register: query_result
142
143- name: Query a VLAN pool for ranges by range_start
144 aci_encap_pool_range:
145 host: apic
146 username: admin
147 password: SomeSecretPassword
148 pool_type: vlan
149 range_start: 20
150 state: query
151 delegate_to: localhost
152 register: query_result
153
154- name: Query a VLAN pool for ranges by range_start and range_end
155 aci_encap_pool_range:
156 host: apic
157 username: admin
158 password: SomeSecretPassword
159 pool_type: vlan
160 range_start: 20
161 range_end: 40
162 state: query
163 delegate_to: localhost
164 register: query_result
165
166- name: Query all VLAN pool ranges
167 aci_encap_pool_range:
168 host: apic
169 username: admin
170 password: SomeSecretPassword
171 pool_type: vlan
172 state: query
173 delegate_to: localhost
174 register: query_result
175'''
176
177RETURN = r'''
178current:
179 description: The existing configuration from the APIC after the module has finished
180 returned: success
181 type: list
182 sample:
183 [
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production environment",
188 "dn": "uni/tn-production",
189 "name": "production",
190 "nameAlias": "",
191 "ownerKey": "",
192 "ownerTag": ""
193 }
194 }
195 }
196 ]
197error:
198 description: The error information as returned from the APIC
199 returned: failure
200 type: dict
201 sample:
202 {
203 "code": "122",
204 "text": "unknown managed object class foo"
205 }
206raw:
207 description: The raw output returned by the APIC REST API (xml or json)
208 returned: parse error
209 type: str
210 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
211sent:
212 description: The actual/minimal configuration pushed to the APIC
213 returned: info
214 type: list
215 sample:
216 {
217 "fvTenant": {
218 "attributes": {
219 "descr": "Production environment"
220 }
221 }
222 }
223previous:
224 description: The original configuration from the APIC before the module has started
225 returned: info
226 type: list
227 sample:
228 [
229 {
230 "fvTenant": {
231 "attributes": {
232 "descr": "Production",
233 "dn": "uni/tn-production",
234 "name": "production",
235 "nameAlias": "",
236 "ownerKey": "",
237 "ownerTag": ""
238 }
239 }
240 }
241 ]
242proposed:
243 description: The assembled configuration from the user-provided parameters
244 returned: info
245 type: dict
246 sample:
247 {
248 "fvTenant": {
249 "attributes": {
250 "descr": "Production environment",
251 "name": "production"
252 }
253 }
254 }
255filter_string:
256 description: The filter string used for the request
257 returned: failure or debug
258 type: str
259 sample: ?rsp-prop-include=config-only
260method:
261 description: The HTTP method used for the request to the APIC
262 returned: failure or debug
263 type: str
264 sample: POST
265response:
266 description: The HTTP response from the APIC
267 returned: failure or debug
268 type: str
269 sample: OK (30 bytes)
270status:
271 description: The HTTP status from the APIC
272 returned: failure or debug
273 type: int
274 sample: 200
275url:
276 description: The HTTP url used for the request to the APIC
277 returned: failure or debug
278 type: str
279 sample: https://10.11.12.13/api/mo/uni/tn-production.json
280'''
281
282from ansible.module_utils.basic import AnsibleModule
283from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
284
285ACI_POOL_MAPPING = dict(
286 vlan=dict(
287 aci_class='fvnsVlanInstP',
288 aci_mo='infra/vlanns-',
289 ),
290 vxlan=dict(
291 aci_class='fvnsVxlanInstP',
292 aci_mo='infra/vxlanns-',
293 ),
294 vsan=dict(
295 aci_class='fvnsVsanInstP',
296 aci_mo='infra/vsanns-',
297 ),
298)
299
300
301def main():
302 argument_spec = aci_argument_spec()
303 argument_spec.update(
304 pool_type=dict(type='str', required=True, aliases=['type'], choices=['vlan', 'vxlan', 'vsan']),
305 allocation_mode=dict(type='str', aliases=['mode'], choices=['dynamic', 'inherit', 'static']),
306 description=dict(type='str', aliases=['descr']),
307 pool=dict(type='str', aliases=['pool_name']), # Not required for querying all objects
308 pool_allocation_mode=dict(type='str', aliases=['pool_mode'], choices=['dynamic', 'static']),
309 range_end=dict(type='int', aliases=['end']), # Not required for querying all objects
310 range_name=dict(type='str', aliases=["name", "range"]), # Not required for querying all objects
311 range_start=dict(type='int', aliases=["start"]), # Not required for querying all objects
312 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
313 )
314
315 module = AnsibleModule(
316 argument_spec=argument_spec,
317 supports_check_mode=True,
318 required_if=[
319 ['state', 'absent', ['pool', 'range_end', 'range_name', 'range_start']],
320 ['state', 'present', ['pool', 'range_end', 'range_name', 'range_start']],
321 ],
322 )
323
324 allocation_mode = module.params['allocation_mode']
325 description = module.params['description']
326 pool = module.params['pool']
327 pool_allocation_mode = module.params['pool_allocation_mode']
328 pool_type = module.params['pool_type']
329 range_end = module.params['range_end']
330 range_name = module.params['range_name']
331 range_start = module.params['range_start']
332 state = module.params['state']
333
334 if range_end is not None:
335 encap_end = '{0}-{1}'.format(pool_type, range_end)
336 else:
337 encap_end = None
338
339 if range_start is not None:
340 encap_start = '{0}-{1}'.format(pool_type, range_start)
341 else:
342 encap_start = None
343
344 ACI_RANGE_MAPPING = dict(
345 vlan=dict(
346 aci_class='fvnsEncapBlk',
347 aci_mo='from-[{0}]-to-[{1}]'.format(encap_start, encap_end),
348 ),
349 vxlan=dict(
350 aci_class='fvnsEncapBlk',
351 aci_mo='from-[{0}]-to-[{1}]'.format(encap_start, encap_end),
352 ),
353 vsan=dict(
354 aci_class='fvnsVsanEncapBlk',
355 aci_mo='vsanfrom-[{0}]-to-[{1}]'.format(encap_start, encap_end),
356 ),
357 )
358
359 # Collect proper class and mo information based on pool_type
360 aci_range_class = ACI_RANGE_MAPPING[pool_type]["aci_class"]
361 aci_range_mo = ACI_RANGE_MAPPING[pool_type]["aci_mo"]
362 aci_pool_class = ACI_POOL_MAPPING[pool_type]["aci_class"]
363 aci_pool_mo = ACI_POOL_MAPPING[pool_type]["aci_mo"]
364 pool_name = pool
365
366 # Validate range_end and range_start are valid for its respective encap type
367 for encap_id in range_end, range_start:
368 if encap_id is not None:
369 if pool_type == 'vlan':
370 if not 1 <= encap_id <= 4094:
371 module.fail_json(msg='vlan pools must have "range_start" and "range_end" values between 1 and 4094')
372 elif pool_type == 'vxlan':
373 if not 5000 <= encap_id <= 16777215:
374 module.fail_json(msg='vxlan pools must have "range_start" and "range_end" values between 5000 and 16777215')
375 elif pool_type == 'vsan':
376 if not 1 <= encap_id <= 4093:
377 module.fail_json(msg='vsan pools must have "range_start" and "range_end" values between 1 and 4093')
378
379 if range_end is not None and range_start is not None:
380 # Validate range_start is less than range_end
381 if range_start > range_end:
382 module.fail_json(msg='The "range_start" must be less than or equal to the "range_end"')
383
384 elif range_end is None and range_start is None:
385 if range_name is None:
386 # Reset range managed object to None for aci util to properly handle query
387 aci_range_mo = None
388
389 # Vxlan does not support setting the allocation mode
390 if pool_type == 'vxlan' and allocation_mode is not None:
391 module.fail_json(msg='vxlan pools do not support setting the "allocation_mode"; please omit this parameter for vxlan pools')
392
393 # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
394 if pool_type != 'vxlan' and pool is not None:
395 if pool_allocation_mode is not None:
396 pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
397 else:
398 module.fail_json(msg='ACI requires the "pool_allocation_mode" for "pool_type" of "vlan" and "vsan" when the "pool" is provided')
399
400 aci = ACIModule(module)
401 aci.construct_url(
402 root_class=dict(
403 aci_class=aci_pool_class,
404 aci_rn='{0}{1}'.format(aci_pool_mo, pool_name),
405 module_object=pool,
406 target_filter={'name': pool},
407 ),
408 subclass_1=dict(
409 aci_class=aci_range_class,
410 aci_rn='{0}'.format(aci_range_mo),
411 module_object=aci_range_mo,
412 target_filter={'from': encap_start, 'to': encap_end, 'name': range_name},
413 ),
414 )
415
416 aci.get_existing()
417
418 if state == 'present':
419 aci.payload(
420 aci_class=aci_range_class,
421 class_config={
422 "allocMode": allocation_mode,
423 "descr": description,
424 "from": encap_start,
425 "name": range_name,
426 "to": encap_end,
427 },
428 )
429
430 aci.get_diff(aci_class=aci_range_class)
431
432 aci.post_config()
433
434 elif state == 'absent':
435 aci.delete_config()
436
437 aci.exit_json()
438
439
440if __name__ == "__main__":
441 main()