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.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
16short_description: Manage encap pools (fvns:VlanInstP, fvns:VxlanInstP, fvns:VsanInstP)
17description:
18- Manage vlan, vxlan, and vsan pools on Cisco ACI fabrics.
19version_added: '2.5'
20options:
21 description:
22 description:
23 - Description for the C(pool).
24 type: str
25 aliases: [ descr ]
26 pool:
27 description:
28 - The name of the pool.
29 type: str
30 aliases: [ name, pool_name ]
31 pool_allocation_mode:
32 description:
33 - The method used for allocating encaps to resources.
34 - Only vlan and vsan support allocation modes.
35 type: str
36 choices: [ dynamic, static ]
37 aliases: [ allocation_mode, mode ]
38 pool_type:
39 description:
40 - The encap type of C(pool).
41 type: str
42 required: yes
43 aliases: [ type ]
44 choices: [ vlan, vsan, vxlan ]
45 state:
46 description:
47 - Use C(present) or C(absent) for adding or removing.
48 - Use C(query) for listing an object or multiple objects.
49 type: str
50 choices: [ absent, present, query ]
51 default: present
52extends_documentation_fragment: aci
53seealso:
54- module: aci_encap_pool_range
55- module: aci_vlan_pool
56- name: APIC Management Information Model reference
57 description: More information about the internal APIC classes B(fvns:VlanInstP),
58 B(fvns:VxlanInstP) and B(fvns:VsanInstP)
59 link: https://developer.cisco.com/docs/apic-mim-ref/
60author:
61- Jacob McGill (@jmcgill298)
62'''
63
64EXAMPLES = r'''
65- name: Add a new vlan pool
66 aci_encap_pool:
67 host: apic
68 username: admin
69 password: SomeSecretPassword
70 pool: production
71 pool_type: vlan
72 description: Production VLANs
73 state: present
74 delegate_to: localhost
75
76- name: Remove a vlan pool
77 aci_encap_pool:
78 host: apic
79 username: admin
80 password: SomeSecretPassword
81 pool: production
82 pool_type: vlan
83 state: absent
84 delegate_to: localhost
85
86- name: Query a vlan pool
87 aci_encap_pool:
88 host: apic
89 username: admin
90 password: SomeSecretPassword
91 pool: production
92 pool_type: vlan
93 state: query
94 delegate_to: localhost
95 register: query_result
96
97- name: Query all vlan pools
98 aci_encap_pool:
99 host: apic
100 username: admin
101 password: SomeSecretPassword
102 pool_type: vlan
103 state: query
104 delegate_to: localhost
105 register: query_result
106'''
107
108RETURN = r'''
109current:
110 description: The existing configuration from the APIC after the module has finished
111 returned: success
112 type: list
113 sample:
114 [
115 {
116 "fvTenant": {
117 "attributes": {
118 "descr": "Production environment",
119 "dn": "uni/tn-production",
120 "name": "production",
121 "nameAlias": "",
122 "ownerKey": "",
123 "ownerTag": ""
124 }
125 }
126 }
127 ]
128error:
129 description: The error information as returned from the APIC
130 returned: failure
131 type: dict
132 sample:
133 {
134 "code": "122",
135 "text": "unknown managed object class foo"
136 }
137raw:
138 description: The raw output returned by the APIC REST API (xml or json)
139 returned: parse error
140 type: str
141 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
142sent:
143 description: The actual/minimal configuration pushed to the APIC
144 returned: info
145 type: list
146 sample:
147 {
148 "fvTenant": {
149 "attributes": {
150 "descr": "Production environment"
151 }
152 }
153 }
154previous:
155 description: The original configuration from the APIC before the module has started
156 returned: info
157 type: list
158 sample:
159 [
160 {
161 "fvTenant": {
162 "attributes": {
163 "descr": "Production",
164 "dn": "uni/tn-production",
165 "name": "production",
166 "nameAlias": "",
167 "ownerKey": "",
168 "ownerTag": ""
169 }
170 }
171 }
172 ]
173proposed:
174 description: The assembled configuration from the user-provided parameters
175 returned: info
176 type: dict
177 sample:
178 {
179 "fvTenant": {
180 "attributes": {
181 "descr": "Production environment",
182 "name": "production"
183 }
184 }
185 }
186filter_string:
187 description: The filter string used for the request
188 returned: failure or debug
189 type: str
190 sample: ?rsp-prop-include=config-only
191method:
192 description: The HTTP method used for the request to the APIC
193 returned: failure or debug
194 type: str
195 sample: POST
196response:
197 description: The HTTP response from the APIC
198 returned: failure or debug
199 type: str
200 sample: OK (30 bytes)
201status:
202 description: The HTTP status from the APIC
203 returned: failure or debug
204 type: int
205 sample: 200
206url:
207 description: The HTTP url used for the request to the APIC
208 returned: failure or debug
209 type: str
210 sample: https://10.11.12.13/api/mo/uni/tn-production.json
211'''
212
213from ansible.module_utils.basic import AnsibleModule
214from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
215
216ACI_POOL_MAPPING = dict(
217 vlan=dict(
218 aci_class='fvnsVlanInstP',
219 aci_mo='infra/vlanns-',
220 ),
221 vxlan=dict(
222 aci_class='fvnsVxlanInstP',
223 aci_mo='infra/vxlanns-',
224 ),
225 vsan=dict(
226 aci_class='fvnsVsanInstP',
227 aci_mo='infra/vsanns-',
228 ),
229)
230
231
232def main():
233 argument_spec = aci_argument_spec()
234 argument_spec.update(
235 pool_type=dict(type='str', required=True, aliases=['type'], choices=['vlan', 'vsan', 'vxlan']),
236 description=dict(type='str', aliases=['descr']),
237 pool=dict(type='str', aliases=['name', 'pool_name']), # Not required for querying all objects
238 pool_allocation_mode=dict(type='str', aliases=['allocation_mode', 'mode'], choices=['dynamic', 'static']),
239 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
240 )
241
242 module = AnsibleModule(
243 argument_spec=argument_spec,
244 supports_check_mode=True,
245 required_if=[
246 ['state', 'absent', ['pool']],
247 ['state', 'present', ['pool']],
248 ],
249 )
250
251 description = module.params['description']
252 pool = module.params['pool']
253 pool_type = module.params['pool_type']
254 pool_allocation_mode = module.params['pool_allocation_mode']
255 state = module.params['state']
256
257 aci_class = ACI_POOL_MAPPING[pool_type]['aci_class']
258 aci_mo = ACI_POOL_MAPPING[pool_type]['aci_mo']
259 pool_name = pool
260
261 # ACI Pool URL requires the pool_allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
262 if pool_type != 'vxlan' and pool is not None:
263 if pool_allocation_mode is not None:
264 pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
265 else:
266 module.fail_json(msg="ACI requires parameter 'pool_allocation_mode' for 'pool_type' of 'vlan' and 'vsan' when parameter 'pool' is provided")
267
268 # Vxlan pools do not support pool allocation modes
269 if pool_type == 'vxlan' and pool_allocation_mode is not None:
270 module.fail_json(msg="vxlan pools do not support setting the 'pool_allocation_mode'; please remove this parameter from the task")
271
272 aci = ACIModule(module)
273 aci.construct_url(
274 root_class=dict(
275 aci_class=aci_class,
276 aci_rn='{0}{1}'.format(aci_mo, pool_name),
277 module_object=pool,
278 target_filter={'name': pool},
279 ),
280 )
281
282 aci.get_existing()
283
284 if state == 'present':
285 # Filter out module parameters with null values
286 aci.payload(
287 aci_class=aci_class,
288 class_config=dict(
289 allocMode=pool_allocation_mode,
290 descr=description,
291 name=pool,
292 )
293 )
294
295 # Generate config diff which will be used as POST request body
296 aci.get_diff(aci_class=aci_class)
297
298 # Submit changes if module not in check_mode and the proposed is different than existing
299 aci.post_config()
300
301 elif state == 'absent':
302 aci.delete_config()
303
304 aci.exit_json()
305
306
307if __name__ == "__main__":
308 main()