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_switch_leaf_selector.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2017, Bruno Calogero <brunocalogero@hotmail.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_switch_leaf_selector
17short_description: Bind leaf selectors to switch policy leaf profiles (infra:LeafS, infra:NodeBlk, infra:RsAccNodePGrep)
18description:
19- Bind leaf selectors (with node block range and policy group) to switch policy leaf profiles on Cisco ACI fabrics.
20version_added: '2.5'
21options:
22 description:
23 description:
24 - The description to assign to the C(leaf).
25 type: str
26 leaf_profile:
27 description:
28 - Name of the Leaf Profile to which we add a Selector.
29 type: str
30 aliases: [ leaf_profile_name ]
31 leaf:
32 description:
33 - Name of Leaf Selector.
34 type: str
35 aliases: [ name, leaf_name, leaf_profile_leaf_name, leaf_selector_name ]
36 leaf_node_blk:
37 description:
38 - Name of Node Block range to be added to Leaf Selector of given Leaf Profile.
39 type: str
40 aliases: [ leaf_node_blk_name, node_blk_name ]
41 leaf_node_blk_description:
42 description:
43 - The description to assign to the C(leaf_node_blk)
44 type: str
45 from:
46 description:
47 - Start of Node Block range.
48 type: int
49 aliases: [ node_blk_range_from, from_range, range_from ]
50 to:
51 description:
52 - Start of Node Block range.
53 type: int
54 aliases: [ node_blk_range_to, to_range, range_to ]
55 policy_group:
56 description:
57 - Name of the Policy Group to be added to Leaf Selector of given Leaf Profile.
58 type: str
59 aliases: [ name, policy_group_name ]
60 state:
61 description:
62 - Use C(present) or C(absent) for adding or removing.
63 - Use C(query) for listing an object or multiple objects.
64 type: str
65 choices: [ absent, present, query ]
66 default: present
67extends_documentation_fragment: aci
68notes:
69- This module is to be used with M(aci_switch_policy_leaf_profile).
70 One first creates a leaf profile (infra:NodeP) and then creates an associated selector (infra:LeafS),
71seealso:
72- module: aci_switch_policy_leaf_profile
73- name: APIC Management Information Model reference
74 description: More information about the internal APIC classes B(infra:LeafS),
75 B(infra:NodeBlk) and B(infra:RsAccNodePGrp).
76 link: https://developer.cisco.com/docs/apic-mim-ref/
77author:
78- Bruno Calogero (@brunocalogero)
79'''
80
81EXAMPLES = r'''
82- name: adding a switch policy leaf profile selector associated Node Block range (w/ policy group)
83 aci_switch_leaf_selector:
84 host: apic
85 username: admin
86 password: SomeSecretPassword
87 leaf_profile: sw_name
88 leaf: leaf_selector_name
89 leaf_node_blk: node_blk_name
90 from: 1011
91 to: 1011
92 policy_group: somepolicygroupname
93 state: present
94 delegate_to: localhost
95
96- name: adding a switch policy leaf profile selector associated Node Block range (w/o policy group)
97 aci_switch_leaf_selector:
98 host: apic
99 username: admin
100 password: SomeSecretPassword
101 leaf_profile: sw_name
102 leaf: leaf_selector_name
103 leaf_node_blk: node_blk_name
104 from: 1011
105 to: 1011
106 state: present
107 delegate_to: localhost
108
109- name: Removing a switch policy leaf profile selector
110 aci_switch_leaf_selector:
111 host: apic
112 username: admin
113 password: SomeSecretPassword
114 leaf_profile: sw_name
115 leaf: leaf_selector_name
116 state: absent
117 delegate_to: localhost
118
119- name: Querying a switch policy leaf profile selector
120 aci_switch_leaf_selector:
121 host: apic
122 username: admin
123 password: SomeSecretPassword
124 leaf_profile: sw_name
125 leaf: leaf_selector_name
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
239
240def main():
241 argument_spec = aci_argument_spec()
242 argument_spec.update({
243 'description': dict(type='str'),
244 'leaf_profile': dict(type='str', aliases=['leaf_profile_name']), # Not required for querying all objects
245 'leaf': dict(type='str', aliases=['name', 'leaf_name', 'leaf_profile_leaf_name', 'leaf_selector_name']), # Not required for querying all objects
246 'leaf_node_blk': dict(type='str', aliases=['leaf_node_blk_name', 'node_blk_name']),
247 'leaf_node_blk_description': dict(type='str'),
248 # NOTE: Keyword 'from' is a reserved word in python, so we need it as a string
249 'from': dict(type='int', aliases=['node_blk_range_from', 'from_range', 'range_from']),
250 'to': dict(type='int', aliases=['node_blk_range_to', 'to_range', 'range_to']),
251 'policy_group': dict(type='str', aliases=['policy_group_name']),
252 'state': dict(type='str', default='present', choices=['absent', 'present', 'query']),
253 })
254
255 module = AnsibleModule(
256 argument_spec=argument_spec,
257 supports_check_mode=True,
258 required_if=[
259 ['state', 'absent', ['leaf_profile', 'leaf']],
260 ['state', 'present', ['leaf_profile', 'leaf', 'leaf_node_blk', 'from', 'to']]
261 ]
262 )
263
264 description = module.params['description']
265 leaf_profile = module.params['leaf_profile']
266 leaf = module.params['leaf']
267 leaf_node_blk = module.params['leaf_node_blk']
268 leaf_node_blk_description = module.params['leaf_node_blk_description']
269 from_ = module.params['from']
270 to_ = module.params['to']
271 policy_group = module.params['policy_group']
272 state = module.params['state']
273
274 # Build child_configs dynamically
275 child_configs = [
276 dict(
277 infraNodeBlk=dict(
278 attributes=dict(
279 descr=leaf_node_blk_description,
280 name=leaf_node_blk,
281 from_=from_,
282 to_=to_,
283 ),
284 ),
285 ),
286 ]
287
288 # Add infraRsAccNodePGrp only when policy_group was defined
289 if policy_group is not None:
290 child_configs.append(dict(
291 infraRsAccNodePGrp=dict(
292 attributes=dict(
293 tDn='uni/infra/funcprof/accnodepgrp-{0}'.format(policy_group),
294 ),
295 ),
296 ))
297
298 aci = ACIModule(module)
299 aci.construct_url(
300 root_class=dict(
301 aci_class='infraNodeP',
302 aci_rn='infra/nprof-{0}'.format(leaf_profile),
303 module_object=leaf_profile,
304 target_filter={'name': leaf_profile},
305 ),
306 subclass_1=dict(
307 aci_class='infraLeafS',
308 # NOTE: normal rn: leaves-{name}-typ-{type}, hence here hardcoded to range for purposes of module
309 aci_rn='leaves-{0}-typ-range'.format(leaf),
310 module_object=leaf,
311 target_filter={'name': leaf},
312 ),
313 # NOTE: infraNodeBlk is not made into a subclass because there is a 1-1 mapping between node block and leaf selector name
314 child_classes=['infraNodeBlk', 'infraRsAccNodePGrp'],
315
316 )
317
318 aci.get_existing()
319
320 if state == 'present':
321 aci.payload(
322 aci_class='infraLeafS',
323 class_config=dict(
324 descr=description,
325 name=leaf,
326 ),
327 child_configs=child_configs,
328 )
329
330 aci.get_diff(aci_class='infraLeafS')
331
332 aci.post_config()
333
334 elif state == 'absent':
335 aci.delete_config()
336
337 aci.exit_json()
338
339
340if __name__ == "__main__":
341 main()