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

aci_domain_to_encap_pool.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2017, Dag Wieers <dag@wieers.com>
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7from __future__ import absolute_import, division, print_function
8__metaclass__ = type
9
10ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['preview'],
12 'supported_by': 'certified'}
13
14DOCUMENTATION = r'''
15---
16module: aci_domain_to_encap_pool
17short_description: Bind Domain to Encap Pools (infra:RsVlanNs)
18description:
19- Bind Domain to Encap Pools on Cisco ACI fabrics.
20notes:
21- The C(domain) and C(encap_pool) parameters should exist before using this module.
22 The M(aci_domain) and M(aci_encap_pool) can be used for these.
23version_added: '2.5'
24options:
25 domain:
26 description:
27 - Name of the domain being associated with the Encap Pool.
28 type: str
29 aliases: [ domain_name, domain_profile ]
30 domain_type:
31 description:
32 - Determines if the Domain is physical (phys) or virtual (vmm).
33 type: str
34 choices: [ fc, l2dom, l3dom, phys, vmm ]
35 pool:
36 description:
37 - The name of the pool.
38 type: str
39 aliases: [ pool_name ]
40 pool_allocation_mode:
41 description:
42 - The method used for allocating encaps to resources.
43 - Only vlan and vsan support allocation modes.
44 type: str
45 choices: [ dynamic, static]
46 aliases: [ allocation_mode, mode ]
47 pool_type:
48 description:
49 - The encap type of C(pool).
50 type: str
51 required: yes
52 choices: [ vlan, vsan, vxlan ]
53 state:
54 description:
55 - Use C(present) or C(absent) for adding or removing.
56 - Use C(query) for listing an object or multiple objects.
57 type: str
58 choices: [ absent, present, query ]
59 default: present
60 vm_provider:
61 description:
62 - The VM platform for VMM Domains.
63 - Support for Kubernetes was added in ACI v3.0.
64 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
65 type: str
66 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware ]
67extends_documentation_fragment: aci
68seealso:
69- module: aci_domain
70- module: aci_encap_pool
71- name: APIC Management Information Model reference
72 description: More information about the internal APIC class B(infra:RsVlanNs).
73 link: https://developer.cisco.com/docs/apic-mim-ref/
74author:
75- Dag Wieers (@dagwieers)
76'''
77
78EXAMPLES = r'''
79- name: Add domain to VLAN pool binding
80 aci_domain_to_encap_pool:
81 host: apic
82 username: admin
83 password: SomeSecretPassword
84 domain: phys_dom
85 domain_type: phys
86 pool: test_pool
87 pool_type: vlan
88 pool_allocation_mode: dynamic
89 state: present
90 delegate_to: localhost
91
92- name: Remove domain to VLAN pool binding
93 aci_domain_to_encap_pool:
94 host: apic
95 username: admin
96 password: SomeSecretPassword
97 domain: phys_dom
98 domain_type: phys
99 pool: test_pool
100 pool_type: vlan
101 pool_allocation_mode: dynamic
102 state: absent
103 delegate_to: localhost
104
105- name: Query our domain to VLAN pool binding
106 aci_domain_to_encap_pool:
107 host: apic
108 username: admin
109 password: SomeSecretPassword
110 domain: phys_dom
111 pool: test_pool
112 pool_type: vlan
113 pool_allocation_mode: dynamic
114 state: query
115 delegate_to: localhost
116 register: query_result
117
118- name: Query all domain to VLAN pool bindings
119 aci_domain_to_encap_pool:
120 host: apic
121 username: admin
122 password: SomeSecretPassword
123 domain_type: phys
124 pool_type: vlan
125 pool_allocation_mode: dynamic
126 state: query
127 delegate_to: localhost
128 register: query_result
129'''
130
131RETURN = r'''
132current:
133 description: The existing configuration from the APIC after the module has finished
134 returned: success
135 type: list
136 sample:
137 [
138 {
139 "fvTenant": {
140 "attributes": {
141 "descr": "Production environment",
142 "dn": "uni/tn-production",
143 "name": "production",
144 "nameAlias": "",
145 "ownerKey": "",
146 "ownerTag": ""
147 }
148 }
149 }
150 ]
151error:
152 description: The error information as returned from the APIC
153 returned: failure
154 type: dict
155 sample:
156 {
157 "code": "122",
158 "text": "unknown managed object class foo"
159 }
160raw:
161 description: The raw output returned by the APIC REST API (xml or json)
162 returned: parse error
163 type: str
164 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
165sent:
166 description: The actual/minimal configuration pushed to the APIC
167 returned: info
168 type: list
169 sample:
170 {
171 "fvTenant": {
172 "attributes": {
173 "descr": "Production environment"
174 }
175 }
176 }
177previous:
178 description: The original configuration from the APIC before the module has started
179 returned: info
180 type: list
181 sample:
182 [
183 {
184 "fvTenant": {
185 "attributes": {
186 "descr": "Production",
187 "dn": "uni/tn-production",
188 "name": "production",
189 "nameAlias": "",
190 "ownerKey": "",
191 "ownerTag": ""
192 }
193 }
194 }
195 ]
196proposed:
197 description: The assembled configuration from the user-provided parameters
198 returned: info
199 type: dict
200 sample:
201 {
202 "fvTenant": {
203 "attributes": {
204 "descr": "Production environment",
205 "name": "production"
206 }
207 }
208 }
209filter_string:
210 description: The filter string used for the request
211 returned: failure or debug
212 type: str
213 sample: ?rsp-prop-include=config-only
214method:
215 description: The HTTP method used for the request to the APIC
216 returned: failure or debug
217 type: str
218 sample: POST
219response:
220 description: The HTTP response from the APIC
221 returned: failure or debug
222 type: str
223 sample: OK (30 bytes)
224status:
225 description: The HTTP status from the APIC
226 returned: failure or debug
227 type: int
228 sample: 200
229url:
230 description: The HTTP url used for the request to the APIC
231 returned: failure or debug
232 type: str
233 sample: https://10.11.12.13/api/mo/uni/tn-production.json
234'''
235
236from ansible.module_utils.basic import AnsibleModule
237from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
238
239VM_PROVIDER_MAPPING = dict(
240 cloudfoundry='CloudFoundry',
241 kubernetes='Kubernetes',
242 microsoft='Microsoft',
243 openshift='OpenShift',
244 openstack='OpenStack',
245 redhat='Redhat',
246 vmware='VMware',
247)
248
249POOL_MAPPING = dict(
250 vlan=dict(
251 aci_mo='uni/infra/vlanns-{0}',
252 child_class='infraRsVlanNs',
253 ),
254 vxlan=dict(
255 aci_mo='uni/infra/vxlanns-{0}',
256 child_class='vmmRsVxlanNs',
257 ),
258 vsan=dict(
259 aci_mo='uni/infra/vsanns-{0}',
260 child_class='fcRsVsanNs',
261 ),
262)
263
264
265def main():
266 argument_spec = aci_argument_spec()
267 argument_spec.update(
268 domain_type=dict(type='str', required=True, choices=['fc', 'l2dom', 'l3dom', 'phys', 'vmm']),
269 pool_type=dict(type='str', required=True, choices=['vlan', 'vsan', 'vxlan']),
270 domain=dict(type='str', aliases=['domain_name', 'domain_profile']), # Not required for querying all objects
271 pool=dict(type='str', aliases=['pool_name']), # Not required for querying all objects
272 pool_allocation_mode=dict(type='str', aliases=['allocation_mode', 'mode'], choices=['dynamic', 'static']),
273 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
274 vm_provider=dict(type='str', choices=['cloudfoundry', 'kubernetes', 'microsoft', 'openshift', 'openstack', 'redhat', 'vmware']),
275 )
276
277 module = AnsibleModule(
278 argument_spec=argument_spec,
279 supports_check_mode=True,
280 required_if=[
281 ['domain_type', 'vmm', ['vm_provider']],
282 ['state', 'absent', ['domain', 'domain_type', 'pool', 'pool_type']],
283 ['state', 'present', ['domain', 'domain_type', 'pool', 'pool_type']],
284 ],
285 )
286
287 domain = module.params['domain']
288 domain_type = module.params['domain_type']
289 pool = module.params['pool']
290 pool_allocation_mode = module.params['pool_allocation_mode']
291 pool_type = module.params['pool_type']
292 vm_provider = module.params['vm_provider']
293 state = module.params['state']
294
295 # Report when vm_provider is set when type is not virtual
296 if domain_type != 'vmm' and vm_provider is not None:
297 module.fail_json(msg="Domain type '{0}' cannot have a 'vm_provider'".format(domain_type))
298
299 # ACI Pool URL requires the allocation mode for vlan and vsan pools (ex: uni/infra/vlanns-[poolname]-static)
300 pool_name = pool
301 if pool_type != 'vxlan' and pool is not None:
302 if pool_allocation_mode is not None:
303 pool_name = '[{0}]-{1}'.format(pool, pool_allocation_mode)
304 else:
305 module.fail_json(msg="ACI requires the 'pool_allocation_mode' for 'pool_type' of 'vlan' and 'vsan' when 'pool' is provided")
306
307 # Vxlan pools do not support allocation modes
308 if pool_type == 'vxlan' and pool_allocation_mode is not None:
309 module.fail_json(msg='vxlan pools do not support setting the allocation_mode; please remove this parameter from the task')
310
311 # Compile the full domain for URL building
312 if domain_type == 'fc':
313 domain_class = 'fcDomP'
314 domain_mo = 'uni/fc-{0}'.format(domain)
315 domain_rn = 'fc-{0}'.format(domain)
316 elif domain_type == 'l2ext':
317 domain_class = 'l2extDomP'
318 domain_mo = 'uni/l2dom-{0}'.format(domain)
319 domain_rn = 'l2dom-{0}'.format(domain)
320 elif domain_type == 'l3ext':
321 domain_class = 'l3extDomP'
322 domain_mo = 'uni/l3dom-{0}'.format(domain)
323 domain_rn = 'l3dom-{0}'.format(domain)
324 elif domain_type == 'phys':
325 domain_class = 'physDomP'
326 domain_mo = 'uni/phys-{0}'.format(domain)
327 domain_rn = 'phys-{0}'.format(domain)
328 elif domain_type == 'vmm':
329 domain_class = 'vmmDomP'
330 domain_mo = 'uni/vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING[vm_provider], domain)
331 domain_rn = 'vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING[vm_provider], domain)
332
333 # Ensure that querying all objects works when only domain_type is provided
334 if domain is None:
335 domain_mo = None
336
337 pool_mo = POOL_MAPPING[pool_type]['aci_mo'].format(pool_name)
338 child_class = POOL_MAPPING[pool_type]['child_class']
339
340 aci = ACIModule(module)
341 aci.construct_url(
342 root_class=dict(
343 aci_class=domain_class,
344 aci_rn=domain_rn,
345 module_object=domain_mo,
346 target_filter={'name': domain},
347 ),
348 child_classes=[child_class],
349 )
350
351 aci.get_existing()
352
353 if state == 'present':
354 # Filter out module params with null values
355 aci.payload(
356 aci_class=domain_class,
357 class_config=dict(name=domain),
358 child_configs=[
359 {child_class: {'attributes': {'tDn': pool_mo}}},
360 ]
361 )
362
363 # Generate config diff which will be used as POST request body
364 aci.get_diff(aci_class=domain_class)
365
366 # Submit changes if module not in check_mode and the proposed is different than existing
367 aci.post_config()
368
369 elif state == 'absent':
370 aci.delete_config()
371
372 aci.exit_json()
373
374
375if __name__ == "__main__":
376 main()