7from __future__
import absolute_import, division, print_function
10ANSIBLE_METADATA = {
'metadata_version':
'1.1',
11 'status': [
'preview'],
12 'supported_by':
'community'}
16module: mso_schema_template_anp_epg_subnet
17short_description: Manage EPG subnets in schema templates
19- Manage EPG subnets in schema templates on Cisco ACI Multi-Site.
21- Dag Wieers (@dagwieers)
26 - The name of the schema.
31 - The name of the template to change.
36 - The name of the ANP.
41 - The name of the EPG to manage.
46 - The IP range in CIDR notation.
52 - The description of this subnet.
56 - The scope of the subnet.
58 choices: [ private, public ]
61 - Whether this subnet
is shared between VRFs.
65 - Whether this subnet has a default gateway.
69 - Use C(present)
or C(absent)
for adding
or removing.
70 - Use C(query)
for listing an object
or multiple objects.
72 choices: [ absent, present, query ]
75- Due to restrictions of the MSO REST API concurrent modifications to EPG subnets can be dangerous
and corrupt data.
76extends_documentation_fragment: mso
80- name: Add a new subnet to an EPG
81 mso_schema_template_anp_epg_subnet:
84 password: SomeSecretPassword
91 delegate_to: localhost
93- name: Remove a subnet from an EPG
94 mso_schema_template_anp_epg_subnet:
97 password: SomeSecretPassword
104 delegate_to: localhost
106- name: Query a specific EPG subnet
107 mso_schema_template_anp_epg_subnet:
110 password: SomeSecretPassword
117 delegate_to: localhost
118 register: query_result
120- name: Query all EPGs subnets
121 mso_schema_template_anp_epg_subnet:
124 password: SomeSecretPassword
129 delegate_to: localhost
130 register: query_result
136from ansible.module_utils.basic import AnsibleModule
137from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, mso_reference_spec, mso_subnet_spec
142 argument_spec.update(
143 schema=dict(type=
'str', required=
True),
144 template=dict(type=
'str', required=
True),
145 anp=dict(type=
'str', required=
True),
146 epg=dict(type=
'str', required=
True),
147 state=dict(type=
'str', default=
'present', choices=[
'absent',
'present',
'query']),
152 argument_spec=argument_spec,
153 supports_check_mode=
True,
155 [
'state',
'absent', [
'subnet']],
156 [
'state',
'present', [
'subnet']],
160 schema = module.params[
'schema']
161 template = module.params[
'template']
162 anp = module.params[
'anp']
163 epg = module.params[
'epg']
164 subnet = module.params[
'subnet']
165 description = module.params[
'description']
166 scope = module.params[
'scope']
167 shared = module.params[
'shared']
168 no_default_gateway = module.params[
'no_default_gateway']
169 state = module.params[
'state']
174 schema_obj = mso.get_obj(
'schemas', displayName=schema)
176 mso.fail_json(msg=
"Provided schema '{0}' does not exist".
format(schema))
178 schema_path =
'schemas/{id}'.
format(**schema_obj)
181 templates = [t[
'name']
for t
in schema_obj[
'templates']]
182 if template
not in templates:
183 mso.fail_json(msg=
"Provided template '{template}' does not exist. Existing templates: {templates}".
format(template=template,
184 templates=
', '.join(templates)))
185 template_idx = templates.index(template)
188 anps = [a[
'name']
for a
in schema_obj[
'templates'][template_idx][
'anps']]
190 mso.fail_json(msg=
"Provided anp '{anp}' does not exist. Existing anps: {anps}".
format(anp=anp, anps=
', '.join(anps)))
191 anp_idx = anps.index(anp)
194 epgs = [e[
'name']
for e
in schema_obj[
'templates'][template_idx][
'anps'][anp_idx][
'epgs']]
196 mso.fail_json(msg=
"Provided epg '{epg}' does not exist. Existing epgs: {epgs}".
format(epg=epg, epgs=
', '.join(epgs)))
197 epg_idx = epgs.index(epg)
200 subnets = [s[
'ip']
for s
in schema_obj[
'templates'][template_idx][
'anps'][anp_idx][
'epgs'][epg_idx][
'subnets']]
201 if subnet
in subnets:
202 subnet_idx = subnets.index(subnet)
204 subnet_path =
'/templates/{0}/anps/{1}/epgs/{2}/subnets/{3}'.
format(template, anp, epg, subnet_idx)
205 mso.existing = schema_obj[
'templates'][template_idx][
'anps'][anp_idx][
'epgs'][epg_idx][
'subnets'][subnet_idx]
209 mso.existing = schema_obj[
'templates'][template_idx][
'anps'][anp_idx][
'epgs'][epg_idx][
'subnets']
210 elif not mso.existing:
211 mso.fail_json(msg=
"Subnet '{subnet}' not found".
format(subnet=subnet))
214 subnets_path =
'/templates/{0}/anps/{1}/epgs/{2}/subnets'.
format(template, anp, epg)
217 mso.previous = mso.existing
218 if state ==
'absent':
221 ops.append(dict(op=
'remove', path=subnet_path))
223 elif state ==
'present':
225 if description
is None:
231 if no_default_gateway
is None:
232 no_default_gateway =
False
236 description=description,
239 noDefaultGateway=no_default_gateway,
242 mso.sanitize(payload, collate=
True)
245 ops.append(dict(op=
'replace', path=subnet_path, value=mso.sent))
247 ops.append(dict(op=
'add', path=subnets_path +
'/-', value=mso.sent))
249 mso.existing = mso.proposed
251 if not module.check_mode:
252 mso.request(schema_path, method=
'PATCH', data=ops)
257if __name__ ==
"__main__":