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_subnet.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_subnet
17short_description: Manage site-local EPG subnets in schema template
18description:
19- Manage site-local EPG subnets 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 subnet:
48 description:
49 - The IP range in CIDR notation.
50 type: str
51 required: true
52 aliases: [ ip ]
53 description:
54 description:
55 - The description of this subnet.
56 type: str
57 scope:
58 description:
59 - The scope of the subnet.
60 type: str
61 choices: [ private, public ]
62 shared:
63 description:
64 - Whether this subnet is shared between VRFs.
65 type: bool
66 no_default_gateway:
67 description:
68 - Whether this subnet has a default gateway.
69 type: bool
70 state:
71 description:
72 - Use C(present) or C(absent) for adding or removing.
73 - Use C(query) for listing an object or multiple objects.
74 type: str
75 choices: [ absent, present, query ]
76 default: present
77notes:
78- The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index.
79 This can cause silent corruption on concurrent access when changing/removing on object as
80 the wrong object may be referenced. This module is affected by this deficiency.
81seealso:
82- module: mso_schema_site_anp_epg
83- module: mso_schema_template_anp_epg_subnet
84extends_documentation_fragment: mso
85'''
86
87EXAMPLES = r'''
88- name: Add a new subnet to a site EPG
89 mso_schema_site_anp_epg_subnet:
90 host: mso_host
91 username: admin
92 password: SomeSecretPassword
93 schema: Schema1
94 site: Site1
95 template: Template1
96 anp: ANP1
97 epg: EPG1
98 subnet: 10.0.0.0/24
99 state: present
100 delegate_to: localhost
101
102- name: Remove a subnet from a site EPG
103 mso_schema_site_anp_epg_subnet:
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 subnet: 10.0.0.0/24
113 state: absent
114 delegate_to: localhost
115
116- name: Query a specific site EPG subnet
117 mso_schema_site_anp_epg_subnet:
118 host: mso_host
119 username: admin
120 password: SomeSecretPassword
121 schema: Schema1
122 site: Site1
123 template: Template1
124 anp: ANP1
125 epg: EPG1
126 subnet: 10.0.0.0/24
127 state: query
128 delegate_to: localhost
129 register: query_result
130
131- name: Query all site EPG subnets
132 mso_schema_site_anp_epg_subnet:
133 host: mso_host
134 username: admin
135 password: SomeSecretPassword
136 schema: Schema1
137 site: Site1
138 template: Template1
139 anp: ANP1
140 state: query
141 delegate_to: localhost
142 register: query_result
143'''
144
145RETURN = r'''
146'''
147
148from ansible.module_utils.basic import AnsibleModule
149from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, mso_subnet_spec, issubset
150
151
152def main():
153 argument_spec = mso_argument_spec()
154 argument_spec.update(
155 schema=dict(type='str', required=True),
156 site=dict(type='str', required=True),
157 template=dict(type='str', required=True),
158 anp=dict(type='str', required=True),
159 epg=dict(type='str', required=True),
160 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
161 )
162 argument_spec.update(mso_subnet_spec())
163
164 module = AnsibleModule(
165 argument_spec=argument_spec,
166 supports_check_mode=True,
167 required_if=[
168 ['state', 'absent', ['subnet']],
169 ['state', 'present', ['subnet']],
170 ],
171 )
172
173 schema = module.params['schema']
174 site = module.params['site']
175 template = module.params['template']
176 anp = module.params['anp']
177 epg = module.params['epg']
178 subnet = module.params['subnet']
179 description = module.params['description']
180 scope = module.params['scope']
181 shared = module.params['shared']
182 no_default_gateway = module.params['no_default_gateway']
183 state = module.params['state']
184
185 mso = MSOModule(module)
186
187 # Get schema_id
188 schema_obj = mso.get_obj('schemas', displayName=schema)
189 if not schema_obj:
190 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
191
192 schema_path = 'schemas/{id}'.format(**schema_obj)
193 schema_id = schema_obj['id']
194
195 # Get site
196 site_id = mso.lookup_site(site)
197
198 # Get site_idx
199 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
200 if (site_id, template) not in sites:
201 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites)))
202
203 # Schema-access uses indexes
204 site_idx = sites.index((site_id, template))
205 # Path-based access uses site_id-template
206 site_template = '{0}-{1}'.format(site_id, template)
207
208 # Get ANP
209 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
210 anps = [a['anpRef'] for a in schema_obj['sites'][site_idx]['anps']]
211 if anp_ref not in anps:
212 mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ', '.join(anps)))
213 anp_idx = anps.index(anp_ref)
214
215 # Get EPG
216 epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
217 epgs = [e['epgRef'] for e in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs']]
218 if epg_ref not in epgs:
219 mso.fail_json(msg="Provided epg '{0}' does not exist. Existing epgs: {1}".format(epg, ', '.join(epgs)))
220 epg_idx = epgs.index(epg_ref)
221
222 # Get Subnet
223 subnets = [s['ip'] for s in schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]['subnets']]
224 if subnet in subnets:
225 subnet_idx = subnets.index(subnet)
226 # FIXME: Changes based on index are DANGEROUS
227 subnet_path = '/sites/{0}/anps/{1}/epgs/{2}/subnets/{3}'.format(site_template, anp, epg, subnet_idx)
228 mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]['subnets'][subnet_idx]
229
230 if state == 'query':
231 if subnet is None:
232 mso.existing = schema_obj['sites'][site_idx]['anps'][anp_idx]['epgs'][epg_idx]['subnets']
233 elif not mso.existing:
234 mso.fail_json(msg="Subnet '{subnet}' not found".format(subnet=subnet))
235 mso.exit_json()
236
237 subnets_path = '/sites/{0}/anps/{1}/epgs/{2}/subnets'.format(site_template, anp, epg)
238 ops = []
239
240 mso.previous = mso.existing
241 if state == 'absent':
242 if mso.existing:
243 mso.sent = mso.existing = {}
244 ops.append(dict(op='remove', path=subnet_path))
245
246 elif state == 'present':
247 if not mso.existing:
248 if description is None:
249 description = subnet
250 if scope is None:
251 scope = 'private'
252 if shared is None:
253 shared = False
254 if no_default_gateway is None:
255 no_default_gateway = False
256
257 payload = dict(
258 ip=subnet,
259 description=description,
260 scope=scope,
261 shared=shared,
262 noDefaultGateway=no_default_gateway,
263 )
264
265 mso.sanitize(payload, collate=True)
266
267 if mso.existing:
268 ops.append(dict(op='replace', path=subnet_path, value=mso.sent))
269 else:
270 ops.append(dict(op='add', path=subnets_path + '/-', value=mso.sent))
271
272 mso.existing = mso.proposed
273
274 if not module.check_mode:
275 mso.request(schema_path, method='PATCH', data=ops)
276
277 mso.exit_json()
278
279
280if __name__ == "__main__":
281 main()