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)  

mso_schema_template_bd.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2018, Dag Wieers (@dagwieers) <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': 'community'}
13
14DOCUMENTATION = r'''
15---
16module: mso_schema_template_bd
17short_description: Manage Bridge Domains (BDs) in schema templates
18description:
19- Manage BDs in schema templates on Cisco ACI Multi-Site.
20author:
21- Dag Wieers (@dagwieers)
22version_added: '2.8'
23options:
24 schema:
25 description:
26 - The name of the schema.
27 type: str
28 required: yes
29 template:
30 description:
31 - The name of the template.
32 type: str
33 required: yes
34 bd:
35 description:
36 - The name of the BD to manage.
37 type: str
38 aliases: [ name ]
39 display_name:
40 description:
41 - The name as displayed on the MSO web interface.
42 type: str
43 vrf:
44 description:
45 - The VRF associated to this BD. This is required only when creating a new BD.
46 type: dict
47 suboptions:
48 name:
49 description:
50 - The name of the VRF to associate with.
51 required: true
52 type: str
53 subnets:
54 description:
55 - The subnets associated to this BD.
56 type: list
57 suboptions:
58 ip:
59 description:
60 - The IP range in CIDR notation.
61 type: str
62 required: true
63 description:
64 description:
65 - The description of this subnet.
66 type: str
67 scope:
68 description:
69 - The scope of the subnet.
70 type: str
71 choices: [ private, public ]
72 shared:
73 description:
74 - Whether this subnet is shared between VRFs.
75 type: bool
76 no_default_gateway:
77 description:
78 - Whether this subnet has a default gateway.
79 type: bool
80 intersite_bum_traffic:
81 description:
82 - Whether to allow intersite BUM traffic.
83 type: bool
84 optimize_wan_bandwidth:
85 description:
86 - Whether to optimize WAN bandwidth.
87 type: bool
88 layer2_stretch:
89 description:
90 - Whether to enable L2 stretch.
91 type: bool
92 layer2_unknown_unicast:
93 description:
94 - Layer2 unknown unicast.
95 type: str
96 choices: [ flood, proxy ]
97 layer3_multicast:
98 description:
99 - Whether to enable L3 multicast.
100 type: bool
101 state:
102 description:
103 - Use C(present) or C(absent) for adding or removing.
104 - Use C(query) for listing an object or multiple objects.
105 type: str
106 choices: [ absent, present, query ]
107 default: present
108extends_documentation_fragment: mso
109'''
110
111EXAMPLES = r'''
112- name: Add a new BD
113 mso_schema_template_bd:
114 host: mso_host
115 username: admin
116 password: SomeSecretPassword
117 schema: Schema 1
118 template: Template 1
119 bd: BD 1
120 vrf:
121 name: VRF1
122 state: present
123 delegate_to: localhost
124
125- name: Remove an BD
126 mso_schema_template_bd:
127 host: mso_host
128 username: admin
129 password: SomeSecretPassword
130 schema: Schema 1
131 template: Template 1
132 bd: BD1
133 state: absent
134 delegate_to: localhost
135
136- name: Query a specific BDs
137 mso_schema_template_bd:
138 host: mso_host
139 username: admin
140 password: SomeSecretPassword
141 schema: Schema 1
142 template: Template 1
143 bd: BD1
144 state: query
145 delegate_to: localhost
146 register: query_result
147
148- name: Query all BDs
149 mso_schema_template_bd:
150 host: mso_host
151 username: admin
152 password: SomeSecretPassword
153 schema: Schema 1
154 template: Template 1
155 state: query
156 delegate_to: localhost
157 register: query_result
158'''
159
160RETURN = r'''
161'''
162
163from ansible.module_utils.basic import AnsibleModule
164from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, mso_reference_spec, mso_subnet_spec
165
166
167def main():
168 argument_spec = mso_argument_spec()
169 argument_spec.update(
170 schema=dict(type='str', required=True),
171 template=dict(type='str', required=True),
172 bd=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
173 display_name=dict(type='str'),
174 intersite_bum_traffic=dict(type='bool'),
175 optimize_wan_bandwidth=dict(type='bool'),
176 layer2_stretch=dict(type='bool'),
177 layer2_unknown_unicast=dict(type='str', choices=['flood', 'proxy']),
178 layer3_multicast=dict(type='bool'),
179 vrf=dict(type='dict', options=mso_reference_spec()),
180 subnets=dict(type='list', options=mso_subnet_spec()),
181 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
182 )
183
184 module = AnsibleModule(
185 argument_spec=argument_spec,
186 supports_check_mode=True,
187 required_if=[
188 ['state', 'absent', ['bd']],
189 ['state', 'present', ['bd', 'vrf']],
190 ],
191 )
192
193 schema = module.params['schema']
194 template = module.params['template']
195 bd = module.params['bd']
196 display_name = module.params['display_name']
197 intersite_bum_traffic = module.params['intersite_bum_traffic']
198 optimize_wan_bandwidth = module.params['optimize_wan_bandwidth']
199 layer2_stretch = module.params['layer2_stretch']
200 layer2_unknown_unicast = module.params['layer2_unknown_unicast']
201 layer3_multicast = module.params['layer3_multicast']
202 vrf = module.params['vrf']
203 subnets = module.params['subnets']
204 state = module.params['state']
205
206 mso = MSOModule(module)
207
208 # Get schema_id
209 schema_obj = mso.get_obj('schemas', displayName=schema)
210 if schema_obj:
211 schema_id = schema_obj['id']
212 else:
213 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
214
215 schema_path = 'schemas/{id}'.format(**schema_obj)
216
217 # Get template
218 templates = [t['name'] for t in schema_obj['templates']]
219 if template not in templates:
220 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates)))
221 template_idx = templates.index(template)
222
223 # Get ANP
224 bds = [b['name'] for b in schema_obj['templates'][template_idx]['bds']]
225
226 if bd is not None and bd in bds:
227 bd_idx = bds.index(bd)
228 mso.existing = schema_obj['templates'][template_idx]['bds'][bd_idx]
229
230 if state == 'query':
231 if bd is None:
232 mso.existing = schema_obj['templates'][template_idx]['bds']
233 elif not mso.existing:
234 mso.fail_json(msg="BD '{bd}' not found".format(bd=bd))
235 mso.exit_json()
236
237 bds_path = '/templates/{0}/bds'.format(template)
238 bd_path = '/templates/{0}/bds/{1}'.format(template, bd)
239 ops = []
240
241 mso.previous = mso.existing
242 if state == 'absent':
243 if mso.existing:
244 mso.sent = mso.existing = {}
245 ops.append(dict(op='remove', path=bd_path))
246
247 elif state == 'present':
248 vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template)
249 subnets = mso.make_subnets(subnets)
250
251 if display_name is None and not mso.existing:
252 display_name = bd
253 if subnets is None and not mso.existing:
254 subnets = []
255
256 payload = dict(
257 name=bd,
258 displayName=display_name,
259 intersiteBumTrafficAllow=intersite_bum_traffic,
260 optimizeWanBandwidth=optimize_wan_bandwidth,
261 l2UnknownUnicast=layer2_unknown_unicast,
262 l2Stretch=layer2_stretch,
263 l3MCast=layer3_multicast,
264 subnets=subnets,
265 vrfRef=vrf_ref,
266 )
267
268 mso.sanitize(payload, collate=True)
269
270 if mso.existing:
271 ops.append(dict(op='replace', path=bd_path, value=mso.sent))
272 else:
273 ops.append(dict(op='add', path=bds_path + '/-', value=mso.sent))
274
275 mso.existing = mso.proposed
276
277 if not module.check_mode:
278 mso.request(schema_path, method='PATCH', data=ops)
279
280 mso.exit_json()
281
282
283if __name__ == "__main__":
284 main()