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_contract.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_contract
16short_description: Bind EPGs to Contracts (fv:RsCons, fv:RsProv)
17description:
18- Bind EPGs to Contracts on Cisco ACI fabrics.
19notes:
20- The C(tenant), C(app_profile), C(EPG), and C(Contract) used must exist before using this module in your playbook.
21 The M(aci_tenant), M(aci_ap), M(aci_epg), and M(aci_contract) modules can be used for this.
22version_added: '2.4'
23options:
24 ap:
25 description:
26 - Name of an existing application network profile, that will contain the EPGs.
27 type: str
28 aliases: [ app_profile, app_profile_name ]
29 contract:
30 description:
31 - The name of the contract.
32 type: str
33 aliases: [ contract_name ]
34 contract_type:
35 description:
36 - Determines if the EPG should Provide or Consume the Contract.
37 type: str
38 required: yes
39 choices: [ consumer, provider ]
40 epg:
41 description:
42 - The name of the end point group.
43 type: str
44 aliases: [ epg_name ]
45 priority:
46 description:
47 - QoS class.
48 - The APIC defaults to C(unspecified) when unset during creation.
49 type: str
50 choices: [ level1, level2, level3, unspecified ]
51 provider_match:
52 description:
53 - The matching algorithm for Provided Contracts.
54 - The APIC defaults to C(at_least_one) when unset during creation.
55 type: str
56 choices: [ all, at_least_one, at_most_one, none ]
57 state:
58 description:
59 - Use C(present) or C(absent) for adding or removing.
60 - Use C(query) for listing an object or multiple objects.
61 type: str
62 choices: [ absent, present, query ]
63 default: present
64 tenant:
65 description:
66 - Name of an existing tenant.
67 type: str
68 aliases: [ tenant_name ]
69extends_documentation_fragment: aci
70seealso:
71- module: aci_ap
72- module: aci_epg
73- module: aci_contract
74- name: APIC Management Information Model reference
75 description: More information about the internal APIC classes B(fv:RsCons) and B(fv:RsProv).
76 link: https://developer.cisco.com/docs/apic-mim-ref/
77author:
78- Jacob McGill (@jmcgill298)
79'''
80
81EXAMPLES = r'''
82- name: Add a new contract to EPG binding
83 aci_epg_to_contract:
84 host: apic
85 username: admin
86 password: SomeSecretPassword
87 tenant: anstest
88 ap: anstest
89 epg: anstest
90 contract: anstest_http
91 contract_type: provider
92 state: present
93 delegate_to: localhost
94
95- name: Remove an existing contract to EPG binding
96 aci_epg_to_contract:
97 host: apic
98 username: admin
99 password: SomeSecretPassword
100 tenant: anstest
101 ap: anstest
102 epg: anstest
103 contract: anstest_http
104 contract_type: provider
105 state: absent
106 delegate_to: localhost
107
108- name: Query a specific contract to EPG binding
109 aci_epg_to_contract:
110 host: apic
111 username: admin
112 password: SomeSecretPassword
113 tenant: anstest
114 ap: anstest
115 epg: anstest
116 contract: anstest_http
117 contract_type: provider
118 state: query
119 delegate_to: localhost
120 register: query_result
121
122- name: Query all provider contract to EPG bindings
123 aci_epg_to_contract:
124 host: apic
125 username: admin
126 password: SomeSecretPassword
127 contract_type: provider
128 state: query
129 delegate_to: localhost
130 register: query_result
131'''
132
133RETURN = r'''
134current:
135 description: The existing configuration from the APIC after the module has finished
136 returned: success
137 type: list
138 sample:
139 [
140 {
141 "fvTenant": {
142 "attributes": {
143 "descr": "Production environment",
144 "dn": "uni/tn-production",
145 "name": "production",
146 "nameAlias": "",
147 "ownerKey": "",
148 "ownerTag": ""
149 }
150 }
151 }
152 ]
153error:
154 description: The error information as returned from the APIC
155 returned: failure
156 type: dict
157 sample:
158 {
159 "code": "122",
160 "text": "unknown managed object class foo"
161 }
162raw:
163 description: The raw output returned by the APIC REST API (xml or json)
164 returned: parse error
165 type: str
166 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
167sent:
168 description: The actual/minimal configuration pushed to the APIC
169 returned: info
170 type: list
171 sample:
172 {
173 "fvTenant": {
174 "attributes": {
175 "descr": "Production environment"
176 }
177 }
178 }
179previous:
180 description: The original configuration from the APIC before the module has started
181 returned: info
182 type: list
183 sample:
184 [
185 {
186 "fvTenant": {
187 "attributes": {
188 "descr": "Production",
189 "dn": "uni/tn-production",
190 "name": "production",
191 "nameAlias": "",
192 "ownerKey": "",
193 "ownerTag": ""
194 }
195 }
196 }
197 ]
198proposed:
199 description: The assembled configuration from the user-provided parameters
200 returned: info
201 type: dict
202 sample:
203 {
204 "fvTenant": {
205 "attributes": {
206 "descr": "Production environment",
207 "name": "production"
208 }
209 }
210 }
211filter_string:
212 description: The filter string used for the request
213 returned: failure or debug
214 type: str
215 sample: ?rsp-prop-include=config-only
216method:
217 description: The HTTP method used for the request to the APIC
218 returned: failure or debug
219 type: str
220 sample: POST
221response:
222 description: The HTTP response from the APIC
223 returned: failure or debug
224 type: str
225 sample: OK (30 bytes)
226status:
227 description: The HTTP status from the APIC
228 returned: failure or debug
229 type: int
230 sample: 200
231url:
232 description: The HTTP url used for the request to the APIC
233 returned: failure or debug
234 type: str
235 sample: https://10.11.12.13/api/mo/uni/tn-production.json
236'''
237
238from ansible.module_utils.basic import AnsibleModule
239from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
240
241ACI_CLASS_MAPPING = dict(
242 consumer={
243 'class': 'fvRsCons',
244 'rn': 'rscons-',
245 },
246 provider={
247 'class': 'fvRsProv',
248 'rn': 'rsprov-',
249 },
250)
251
252PROVIDER_MATCH_MAPPING = dict(
253 all='All',
254 at_least_one='AtleastOne',
255 at_most_one='tmostOne',
256 none='None',
257)
258
259
260def main():
261 argument_spec = aci_argument_spec()
262 argument_spec.update(
263 contract_type=dict(type='str', required=True, choices=['consumer', 'provider']),
264 ap=dict(type='str', aliases=['app_profile', 'app_profile_name']), # Not required for querying all objects
265 epg=dict(type='str', aliases=['epg_name']), # Not required for querying all objects
266 contract=dict(type='str', aliases=['contract_name']), # Not required for querying all objects
267 priority=dict(type='str', choices=['level1', 'level2', 'level3', 'unspecified']),
268 provider_match=dict(type='str', choices=['all', 'at_least_one', 'at_most_one', 'none']),
269 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
270 tenant=dict(type='str', aliases=['tenant_name']), # Not required for querying all objects
271 )
272
273 module = AnsibleModule(
274 argument_spec=argument_spec,
275 supports_check_mode=True,
276 required_if=[
277 ['state', 'absent', ['ap', 'contract', 'epg', 'tenant']],
278 ['state', 'present', ['ap', 'contract', 'epg', 'tenant']],
279 ],
280 )
281
282 ap = module.params['ap']
283 contract = module.params['contract']
284 contract_type = module.params['contract_type']
285 epg = module.params['epg']
286 priority = module.params['priority']
287 provider_match = module.params['provider_match']
288 if provider_match is not None:
289 provider_match = PROVIDER_MATCH_MAPPING[provider_match]
290 state = module.params['state']
291 tenant = module.params['tenant']
292
293 aci_class = ACI_CLASS_MAPPING[contract_type]["class"]
294 aci_rn = ACI_CLASS_MAPPING[contract_type]["rn"]
295
296 if contract_type == "consumer" and provider_match is not None:
297 module.fail_json(msg="the 'provider_match' is only configurable for Provided Contracts")
298
299 aci = ACIModule(module)
300 aci.construct_url(
301 root_class=dict(
302 aci_class='fvTenant',
303 aci_rn='tn-{0}'.format(tenant),
304 module_object=tenant,
305 target_filter={'name': tenant},
306 ),
307 subclass_1=dict(
308 aci_class='fvAp',
309 aci_rn='ap-{0}'.format(ap),
310 module_object=ap,
311 target_filter={'name': ap},
312 ),
313 subclass_2=dict(
314 aci_class='fvAEPg',
315 aci_rn='epg-{0}'.format(epg),
316 module_object=epg,
317 target_filter={'name': epg},
318 ),
319 subclass_3=dict(
320 aci_class=aci_class,
321 aci_rn='{0}{1}'.format(aci_rn, contract),
322 module_object=contract,
323 target_filter={'tnVzBrCPName': contract},
324 ),
325 )
326
327 aci.get_existing()
328
329 if state == 'present':
330 aci.payload(
331 aci_class=aci_class,
332 class_config=dict(
333 matchT=provider_match,
334 prio=priority,
335 tnVzBrCPName=contract,
336 ),
337 )
338
339 aci.get_diff(aci_class=aci_class)
340
341 aci.post_config()
342
343 elif state == 'absent':
344 aci.delete_config()
345
346 aci.exit_json()
347
348
349if __name__ == "__main__":
350 main()