7from __future__
import absolute_import, division, print_function
10ANSIBLE_METADATA = {
'metadata_version':
'1.1',
11 'status': [
'preview'],
12 'supported_by':
'community'}
16module: mso_schema_site_anp_epg
17short_description: Manage site-local Endpoint Groups (EPGs) in schema template
19- Manage site-local EPGs in schema template on Cisco ACI Multi-Site.
21- Dag Wieers (@dagwieers)
26 - The name of the schema.
31 - The name of the site.
36 - The name of the template.
41 - The name of the ANP.
45 - The name of the EPG to manage.
50 - Use C(present) or C(absent)
for adding
or removing.
51 - Use C(query)
for listing an object
or multiple objects.
53 choices: [ absent, present, query ]
56- module: mso_schema_site_anp
57- module: mso_schema_site_anp_epg_subnet
58- module: mso_schema_template_anp_epg
59extends_documentation_fragment: mso
63- name: Add a new site EPG
64 mso_schema_site_anp_epg:
67 password: SomeSecretPassword
74 delegate_to: localhost
76- name: Remove a site EPG
77 mso_schema_site_anp_epg:
80 password: SomeSecretPassword
87 delegate_to: localhost
89- name: Query a specific site EPGs
90 mso_schema_site_anp_epg:
93 password: SomeSecretPassword
100 delegate_to: localhost
101 register: query_result
103- name: Query all site EPGs
104 mso_schema_site_anp_epg:
107 password: SomeSecretPassword
113 delegate_to: localhost
114 register: query_result
120from ansible.module_utils.basic import AnsibleModule
121from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
126 argument_spec.update(
127 schema=dict(type=
'str', required=
True),
128 site=dict(type=
'str', required=
True),
129 template=dict(type=
'str', required=
True),
130 anp=dict(type=
'str', required=
True),
131 epg=dict(type=
'str', aliases=[
'name']),
132 state=dict(type=
'str', default=
'present', choices=[
'absent',
'present',
'query']),
136 argument_spec=argument_spec,
137 supports_check_mode=
True,
139 [
'state',
'absent', [
'epg']],
140 [
'state',
'present', [
'epg']],
144 schema = module.params[
'schema']
145 site = module.params[
'site']
146 template = module.params[
'template']
147 anp = module.params[
'anp']
148 epg = module.params[
'epg']
149 state = module.params[
'state']
154 schema_obj = mso.get_obj(
'schemas', displayName=schema)
156 mso.fail_json(msg=
"Provided schema '{0}' does not exist".
format(schema))
158 schema_path =
'schemas/{id}'.
format(**schema_obj)
159 schema_id = schema_obj[
'id']
162 site_id = mso.lookup_site(site)
163 sites = [(s[
'siteId'], s[
'templateName'])
for s
in schema_obj[
'sites']]
164 if (site_id, template)
not in sites:
165 mso.fail_json(msg=
"Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".
format(site, template,
', '.join(sites)))
168 site_idx = sites.index((site_id, template))
170 site_template =
'{0}-{1}'.
format(site_id, template)
173 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
174 anps = [a[
'anpRef']
for a
in schema_obj[
'sites'][site_idx][
'anps']]
175 if anp_ref
not in anps:
176 mso.fail_json(msg=
"Provided anp '{0}' does not exist. Existing anps: {1}".
format(anp,
', '.join(anps)))
177 anp_idx = anps.index(anp_ref)
180 epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
181 epgs = [e[
'epgRef']
for e
in schema_obj[
'sites'][site_idx][
'anps'][anp_idx][
'epgs']]
182 if epg
is not None and epg_ref
in epgs:
183 epg_idx = epgs.index(epg_ref)
184 epg_path =
'/sites/{0}/anps/{1}/epgs/{2}'.
format(site_template, anp, epg)
185 mso.existing = schema_obj[
'sites'][site_idx][
'anps'][anp_idx][
'epgs'][epg_idx]
189 mso.existing = schema_obj[
'sites'][site_idx][
'anps'][anp_idx][
'epgs']
190 elif not mso.existing:
191 mso.fail_json(msg=
"EPG '{epg}' not found".
format(epg=epg))
194 epgs_path =
'/sites/{0}/anps/{1}/epgs'.
format(site_template, anp)
197 mso.previous = mso.existing
198 if state ==
'absent':
200 mso.sent = mso.existing = {}
201 ops.append(dict(op=
'remove', path=epg_path))
203 elif state ==
'present':
208 templateName=template,
214 mso.sanitize(payload, collate=
True)
217 ops.append(dict(op=
'add', path=epgs_path +
'/-', value=mso.sent))
219 mso.existing = mso.proposed
221 if not module.check_mode:
222 mso.request(schema_path, method=
'PATCH', data=ops)
227if __name__ ==
"__main__":