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_site_anp_epg_staticport.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2019, 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_site_anp_epg_staticport
17short_description: Manage site-local EPG static ports in schema template
18description:
19- Manage site-local EPG static ports in schema template 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 site:
30 description:
31 - The name of the site.
32 type: str
33 required: yes
34 template:
35 description:
36 - The name of the template.
37 type: str
38 required: yes
39 anp:
40 description:
41 - The name of the ANP.
42 type: str
43 epg:
44 description:
45 - The name of the EPG.
46 type: str
47 type:
48 description:
49 - The path type of the static port
50 type: str
51 choices: [ port ]
52 default: port
53 pod:
54 description:
55 - The pod of the static port.
56 type: str
57 leaf:
58 description:
59 - The leaf of the static port.
60 type: str
61 path:
62 description:
63 - The path of the static port.
64 type: str
65 vlan:
66 description:
67 - The port encap VLAN id of the static port.
68 type: int
69 deployment_immediacy:
70 description:
71 - The deployment immediacy of the static port.
72 - C(immediate) means B(Deploy immediate).
73 - C(lazy) means B(deploy on demand).
74 type: str
75 choices: [ immediate, lazy ]
76 mode:
77 description:
78 - The mode of the static port.
79 - C(native) means B(Access (802.1p)).
80 - C(regular) means B(Trunk).
81 - C(untagged) means B(Access (untagged)).
82 type: str
83 choices: [ native, regular, untagged ]
84 state:
85 description:
86 - Use C(present) or C(absent) for adding or removing.
87 - Use C(query) for listing an object or multiple objects.
88 type: str
89 choices: [ absent, present, query ]
90 default: present
91notes:
92- The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index.
93 This can cause silent corruption on concurrent access when changing/removing on object as
94 the wrong object may be referenced. This module is affected by this deficiency.
95seealso:
96- module: mso_schema_site_anp_epg
97- module: mso_schema_template_anp_epg
98extends_documentation_fragment: mso
99'''
100
101EXAMPLES = r'''
102- name: Add a new static port to a site EPG
103 mso_schema_site_anp_epg_staticport:
104 host: mso_host
105 username: admin
106 password: SomeSecretPassword
107 schema: Schema1
108 site: Site1
109 template: Template1
110 anp: ANP1
111 epg: EPG1
112 type: port
113 pod: pod-1
114 leaf: 101
115 path: eth1/1
116 vlan: 126
117 deployment_immediacy: immediate
118 state: present
119 delegate_to: localhost
120
121- name: Remove a static port from a site EPG
122 mso_schema_site_anp_epg_staticport:
123 host: mso_host
124 username: admin
125 password: SomeSecretPassword
126 schema: Schema1
127 site: Site1
128 template: Template1
129 anp: ANP1
130 epg: EPG1
131 type: port
132 pod: pod-1
133 leaf: 101
134 path: eth1/1
135 state: absent
136 delegate_to: localhost
137
138- name: Query a specific site EPG static port
139 mso_schema_site_anp_epg_staticport:
140 host: mso_host
141 username: admin
142 password: SomeSecretPassword
143 schema: Schema1
144 site: Site1
145 template: Template1
146 anp: ANP1
147 epg: EPG1
148 type: port
149 pod: pod-1
150 leaf: 101
151 path: eth1/1
152 state: query
153 delegate_to: localhost
154 register: query_result
155
156- name: Query all site EPG static ports
157 mso_schema_site_anp_epg_staticport:
158 host: mso_host
159 username: admin
160 password: SomeSecretPassword
161 schema: Schema1
162 site: Site1
163 template: Template1
164 anp: ANP1
165 state: query
166 delegate_to: localhost
167 register: query_result
168'''
169
170RETURN = r'''
171'''
172
173from ansible.module_utils.basic import AnsibleModule
174from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
175
176
177def main():
178 argument_spec = mso_argument_spec()
179 argument_spec.update(
180 schema=dict(type='str', required=True),
181 site=dict(type='str', required=True),
182 template=dict(type='str', required=True),
183 anp=dict(type='str', required=True),
184 epg=dict(type='str', required=True),
185 type=dict(type='str', default='port', choices=['port']),
186 pod=dict(type='str'), # This parameter is not required for querying all objects
187 leaf=dict(type='str'), # This parameter is not required for querying all objects
188 path=dict(type='str'), # This parameter is not required for querying all objects
189 vlan=dict(type='int'), # This parameter is not required for querying all objects
190 deployment_immediacy=dict(type='str', choices=['immediate', 'lazy']),
191 mode=dict(type='str', choices=['native', 'regular', 'untagged']),
192 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
193 )
194
195 module = AnsibleModule(
196 argument_spec=argument_spec,
197 supports_check_mode=True,
198 required_if=[
199 ['state', 'absent', ['type', 'pod', 'leaf', 'path', 'vlan']],
200 ['state', 'present', ['type', 'pod', 'leaf', 'path', 'vlan']],
201 ],
202 )
203
204 schema = module.params['schema']
205 site = module.params['site']
206 template = module.params['template']
207 anp = module.params['anp']
208 epg = module.params['epg']
209 path_type = module.params['type']
210 pod = module.params['pod']
211 leaf = module.params['leaf']
212 path = module.params['path']
213 vlan = module.params['vlan']
214 deployment_immediacy = module.params['deployment_immediacy']
215 mode = module.params['mode']
216 state = module.params['state']
217
218 if path_type == 'port':
219 portpath = 'topology/{0}/paths-{1}/pathep-[{2}]'.format(pod, leaf, path)
220
221 mso = MSOModule(module)
222
223 # Get schema_id
224 schema_obj = mso.get_obj('schemas', displayName=schema)
225 if not schema_obj:
226 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
227
228 schema_path = 'schemas/{id}'.format(**schema_obj)
229 schema_id = schema_obj['id']
230
231 # Get site
232 site_id = mso.lookup_site(site)
233
234 # Get site_idx
235 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
236 if (site_id, template) not in sites:
237 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites)))
238
239 # Schema-access uses indexes
240 site_idx = sites.index((site_id, template))
241 # Path-based access uses site_id-template
242 site_template = '{0}-{1}'.format(site_id, template)
243
244 # Get ANP
245 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
246 anps = [a['anpRef'] for a in schema_obj['sites'][site_idx]['anps']]
247 if anp_ref not in anps:
248 mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ', '.join(anps)))
249 anp_idx = anps.index(anp_ref)
250
251 # Get EPG
252 epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
253 epgs = [e['epgRef'] for e in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs']]
254 if epg_ref not in epgs:
255 mso.fail_json(msg="Provided epg '{0}' does not exist. Existing epgs: {1}".format(epg, ', '.join(epgs)))
256 epg_idx = epgs.index(epg_ref)
257
258 # Get Leaf
259 portpaths = [p['path'] for p in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]['staticPorts']]
260 if portpath in portpaths:
261 portpath_idx = portpaths.index(portpath)
262 # FIXME: Changes based on index are DANGEROUS
263 port_path = '/sites/{0}/anps/{1}/epgs/{2}/staticPorts/{3}'.format(site_template, anp, epg, portpath_idx)
264 mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]['staticPorts'][portpath_idx]
265
266 if state == 'query':
267 if leaf is None or vlan is None:
268 mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]['staticPorts']
269 elif not mso.existing:
270 mso.fail_json(msg="Static port '{portpath}' not found".format(portpath=portpath))
271 mso.exit_json()
272
273 ports_path = '/sites/{0}/anps/{1}/epgs/{2}/staticPorts'.format(site_template, anp, epg)
274 ops = []
275
276 mso.previous = mso.existing
277 if state == 'absent':
278 if mso.existing:
279 mso.sent = mso.existing = {}
280 ops.append(dict(op='remove', path=port_path))
281
282 elif state == 'present':
283 if not mso.existing:
284 if deployment_immediacy is None:
285 deployment_immediacy = 'lazy'
286 if mode is None:
287 mode = 'untagged'
288
289 payload = dict(
290 deploymentImmediacy=deployment_immediacy,
291 mode=mode,
292 path=portpath,
293 portEncapVlan=vlan,
294 type=path_type,
295 )
296
297 mso.sanitize(payload, collate=True)
298
299 if mso.existing:
300 ops.append(dict(op='replace', path=port_path, value=mso.sent))
301 else:
302 ops.append(dict(op='add', path=ports_path + '/-', value=mso.sent))
303
304 mso.existing = mso.proposed
305
306 if not module.check_mode:
307 mso.request(schema_path, method='PATCH', data=ops)
308
309 mso.exit_json()
310
311
312if __name__ == "__main__":
313 main()