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