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_epg_to_domain.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_epg_to_domain
16short_description: Bind EPGs to Domains (fv:RsDomAtt)
17description:
18- Bind EPGs to Physical and Virtual Domains on Cisco ACI fabrics.
19version_added: '2.4'
20options:
21 allow_useg:
22 description:
23 - Allows micro-segmentation.
24 - The APIC defaults to C(encap) when unset during creation.
25 type: str
26 choices: [ encap, useg ]
27 ap:
28 description:
29 - Name of an existing application network profile, that will contain the EPGs.
30 type: str
31 aliases: [ app_profile, app_profile_name ]
32 deploy_immediacy:
33 description:
34 - Determines when the policy is pushed to hardware Policy CAM.
35 - The APIC defaults to C(lazy) when unset during creation.
36 type: str
37 choices: [ immediate, lazy ]
38 domain:
39 description:
40 - Name of the physical or virtual domain being associated with the EPG.
41 type: str
42 aliases: [ domain_name, domain_profile ]
43 domain_type:
44 description:
45 - Specify whether the Domain is a physical (phys), a virtual (vmm) or an L2 external domain association (l2dom).
46 type: str
47 choices: [ l2dom, phys, vmm ]
48 aliases: [ type ]
49 encap:
50 description:
51 - The VLAN encapsulation for the EPG when binding a VMM Domain with static C(encap_mode).
52 - This acts as the secondary encap when using useg.
53 - Accepted values range between C(1) and C(4096).
54 type: int
55 encap_mode:
56 description:
57 - The encapsulation method to be used.
58 - The APIC defaults to C(auto) when unset during creation.
59 - If vxlan is selected, switching_mode must be "AVE".
60 type: str
61 choices: [ auto, vlan, vxlan ]
62 switching_mode:
63 description:
64 - Switching Mode used by the switch
65 type: str
66 choices: [ AVE, native ]
67 default: native
68 version_added: '2.9'
69 epg:
70 description:
71 - Name of the end point group.
72 type: str
73 aliases: [ epg_name, name ]
74 netflow:
75 description:
76 - Determines if netflow should be enabled.
77 - The APIC defaults to C(no) when unset during creation.
78 type: bool
79 primary_encap:
80 description:
81 - Determines the primary VLAN ID when using useg.
82 - Accepted values range between C(1) and C(4096).
83 type: int
84 resolution_immediacy:
85 description:
86 - Determines when the policies should be resolved and available.
87 - The APIC defaults to C(lazy) when unset during creation.
88 type: str
89 choices: [ immediate, lazy, pre-provision ]
90 state:
91 description:
92 - Use C(present) or C(absent) for adding or removing.
93 - Use C(query) for listing an object or multiple objects.
94 type: str
95 choices: [ absent, present, query ]
96 default: present
97 tenant:
98 description:
99 - Name of an existing tenant.
100 type: str
101 aliases: [ tenant_name ]
102 vm_provider:
103 description:
104 - The VM platform for VMM Domains.
105 - Support for Kubernetes was added in ACI v3.0.
106 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
107 type: str
108 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware ]
109extends_documentation_fragment: aci
110notes:
111- The C(tenant), C(ap), C(epg), and C(domain) used must exist before using this module in your playbook.
112 The M(aci_tenant) M(aci_ap), M(aci_epg) M(aci_domain) modules can be used for this.
113- OpenStack VMM domains must not be created using this module. The OpenStack VMM domain is created directly
114 by the Cisco APIC Neutron plugin as part of the installation and configuration.
115 This module can be used to query status of an OpenStack VMM domain.
116seealso:
117- module: aci_ap
118- module: aci_epg
119- module: aci_domain
120- name: APIC Management Information Model reference
121 description: More information about the internal APIC class B(fv:RsDomAtt).
122 link: https://developer.cisco.com/docs/apic-mim-ref/
123author:
124- Jacob McGill (@jmcgill298)
125'''
126
127EXAMPLES = r'''
128- name: Add a new physical domain to EPG binding
129 aci_epg_to_domain:
130 host: apic
131 username: admin
132 password: SomeSecretPassword
133 tenant: anstest
134 ap: anstest
135 epg: anstest
136 domain: anstest
137 domain_type: phys
138 state: present
139 delegate_to: localhost
140
141- name: Remove an existing physical domain to EPG binding
142 aci_epg_to_domain:
143 host: apic
144 username: admin
145 password: SomeSecretPassword
146 tenant: anstest
147 ap: anstest
148 epg: anstest
149 domain: anstest
150 domain_type: phys
151 state: absent
152 delegate_to: localhost
153
154- name: Query a specific physical domain to EPG binding
155 aci_epg_to_domain:
156 host: apic
157 username: admin
158 password: SomeSecretPassword
159 tenant: anstest
160 ap: anstest
161 epg: anstest
162 domain: anstest
163 domain_type: phys
164 state: query
165 delegate_to: localhost
166 register: query_result
167
168- name: Query all domain to EPG bindings
169 aci_epg_to_domain:
170 host: apic
171 username: admin
172 password: SomeSecretPassword
173 state: query
174 delegate_to: localhost
175 register: query_result
176'''
177
178RETURN = r'''
179current:
180 description: The existing configuration from the APIC after the module has finished
181 returned: success
182 type: list
183 sample:
184 [
185 {
186 "fvTenant": {
187 "attributes": {
188 "descr": "Production environment",
189 "dn": "uni/tn-production",
190 "name": "production",
191 "nameAlias": "",
192 "ownerKey": "",
193 "ownerTag": ""
194 }
195 }
196 }
197 ]
198error:
199 description: The error information as returned from the APIC
200 returned: failure
201 type: dict
202 sample:
203 {
204 "code": "122",
205 "text": "unknown managed object class foo"
206 }
207raw:
208 description: The raw output returned by the APIC REST API (xml or json)
209 returned: parse error
210 type: str
211 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
212sent:
213 description: The actual/minimal configuration pushed to the APIC
214 returned: info
215 type: list
216 sample:
217 {
218 "fvTenant": {
219 "attributes": {
220 "descr": "Production environment"
221 }
222 }
223 }
224previous:
225 description: The original configuration from the APIC before the module has started
226 returned: info
227 type: list
228 sample:
229 [
230 {
231 "fvTenant": {
232 "attributes": {
233 "descr": "Production",
234 "dn": "uni/tn-production",
235 "name": "production",
236 "nameAlias": "",
237 "ownerKey": "",
238 "ownerTag": ""
239 }
240 }
241 }
242 ]
243proposed:
244 description: The assembled configuration from the user-provided parameters
245 returned: info
246 type: dict
247 sample:
248 {
249 "fvTenant": {
250 "attributes": {
251 "descr": "Production environment",
252 "name": "production"
253 }
254 }
255 }
256filter_string:
257 description: The filter string used for the request
258 returned: failure or debug
259 type: str
260 sample: ?rsp-prop-include=config-only
261method:
262 description: The HTTP method used for the request to the APIC
263 returned: failure or debug
264 type: str
265 sample: POST
266response:
267 description: The HTTP response from the APIC
268 returned: failure or debug
269 type: str
270 sample: OK (30 bytes)
271status:
272 description: The HTTP status from the APIC
273 returned: failure or debug
274 type: int
275 sample: 200
276url:
277 description: The HTTP url used for the request to the APIC
278 returned: failure or debug
279 type: str
280 sample: https://10.11.12.13/api/mo/uni/tn-production.json
281'''
282
283from ansible.module_utils.basic import AnsibleModule
284from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
285
286VM_PROVIDER_MAPPING = dict(
287 cloudfoundry='CloudFoundry',
288 kubernetes='Kubernetes',
289 microsoft='Microsoft',
290 openshift='OpenShift',
291 openstack='OpenStack',
292 redhat='Redhat',
293 vmware='VMware',
294)
295
296
297def main():
298 argument_spec = aci_argument_spec()
299 argument_spec.update(
300 allow_useg=dict(type='str', choices=['encap', 'useg']),
301 ap=dict(type='str', aliases=['app_profile', 'app_profile_name']), # Not required for querying all objects
302 deploy_immediacy=dict(type='str', choices=['immediate', 'lazy']),
303 domain=dict(type='str', aliases=['domain_name', 'domain_profile']), # Not required for querying all objects
304 domain_type=dict(type='str', choices=['l2dom', 'phys', 'vmm'], aliases=['type']), # Not required for querying all objects
305 encap=dict(type='int'),
306 encap_mode=dict(type='str', choices=['auto', 'vlan', 'vxlan']),
307 switching_mode=dict(type='str', default='native', choices=['AVE', 'native']),
308 epg=dict(type='str', aliases=['name', 'epg_name']), # Not required for querying all objects
309 netflow=dict(type='bool'),
310 primary_encap=dict(type='int'),
311 resolution_immediacy=dict(type='str', choices=['immediate', 'lazy', 'pre-provision']),
312 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
313 tenant=dict(type='str', aliases=['tenant_name']), # Not required for querying all objects
314 vm_provider=dict(type='str', choices=['cloudfoundry', 'kubernetes', 'microsoft', 'openshift', 'openstack', 'redhat', 'vmware']),
315 )
316
317 module = AnsibleModule(
318 argument_spec=argument_spec,
319 supports_check_mode=True,
320 required_if=[
321 ['domain_type', 'vmm', ['vm_provider']],
322 ['state', 'absent', ['ap', 'domain', 'domain_type', 'epg', 'tenant']],
323 ['state', 'present', ['ap', 'domain', 'domain_type', 'epg', 'tenant']],
324 ],
325 )
326
327 aci = ACIModule(module)
328
329 allow_useg = module.params['allow_useg']
330 ap = module.params['ap']
331 deploy_immediacy = module.params['deploy_immediacy']
332 domain = module.params['domain']
333 domain_type = module.params['domain_type']
334 vm_provider = module.params['vm_provider']
335 encap = module.params['encap']
336 if encap is not None:
337 if encap in range(1, 4097):
338 encap = 'vlan-{0}'.format(encap)
339 else:
340 module.fail_json(msg='Valid VLAN assignments are from 1 to 4096')
341 encap_mode = module.params['encap_mode']
342 switching_mode = module.params['switching_mode']
343 epg = module.params['epg']
344 netflow = aci.boolean(module.params['netflow'], 'enabled', 'disabled')
345 primary_encap = module.params['primary_encap']
346 if primary_encap is not None:
347 if primary_encap in range(1, 4097):
348 primary_encap = 'vlan-{0}'.format(primary_encap)
349 else:
350 module.fail_json(msg='Valid VLAN assignments are from 1 to 4096')
351 resolution_immediacy = module.params['resolution_immediacy']
352 state = module.params['state']
353 tenant = module.params['tenant']
354
355 if domain_type in ['l2dom', 'phys'] and vm_provider is not None:
356 module.fail_json(msg="Domain type '%s' cannot have a 'vm_provider'" % domain_type)
357
358 # Compile the full domain for URL building
359 if domain_type == 'vmm':
360 epg_domain = 'uni/vmmp-{0}/dom-{1}'.format(VM_PROVIDER_MAPPING[vm_provider], domain)
361 elif domain_type == 'l2dom':
362 epg_domain = 'uni/l2dom-{0}'.format(domain)
363 elif domain_type == 'phys':
364 epg_domain = 'uni/phys-{0}'.format(domain)
365 else:
366 epg_domain = None
367
368 aci.construct_url(
369 root_class=dict(
370 aci_class='fvTenant',
371 aci_rn='tn-{0}'.format(tenant),
372 module_object=tenant,
373 target_filter={'name': tenant},
374 ),
375 subclass_1=dict(
376 aci_class='fvAp',
377 aci_rn='ap-{0}'.format(ap),
378 module_object=ap,
379 target_filter={'name': ap},
380 ),
381 subclass_2=dict(
382 aci_class='fvAEPg',
383 aci_rn='epg-{0}'.format(epg),
384 module_object=epg,
385 target_filter={'name': epg},
386 ),
387 subclass_3=dict(
388 aci_class='fvRsDomAtt',
389 aci_rn='rsdomAtt-[{0}]'.format(epg_domain),
390 module_object=epg_domain,
391 target_filter={'tDn': epg_domain},
392 ),
393 )
394
395 aci.get_existing()
396
397 if state == 'present':
398 aci.payload(
399 aci_class='fvRsDomAtt',
400 class_config=dict(
401 classPref=allow_useg,
402 encap=encap,
403 encapMode=encap_mode,
404 switchingMode=switching_mode,
405 instrImedcy=deploy_immediacy,
406 netflowPref=netflow,
407 primaryEncap=primary_encap,
408 resImedcy=resolution_immediacy,
409 ),
410 )
411
412 aci.get_diff(aci_class='fvRsDomAtt')
413
414 aci.post_config()
415
416 elif state == 'absent':
417 aci.delete_config()
418
419 aci.exit_json()
420
421
422if __name__ == "__main__":
423 main()