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