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.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
17short_description: Manage site-local Endpoint Groups (EPGs) in schema template
18description:
19- Manage site-local EPGs 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 to manage.
46 type: str
47 aliases: [ name ]
48 state:
49 description:
50 - Use C(present) or C(absent) for adding or removing.
51 - Use C(query) for listing an object or multiple objects.
52 type: str
53 choices: [ absent, present, query ]
54 default: present
55seealso:
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
60'''
61
62EXAMPLES = r'''
63- name: Add a new site EPG
64 mso_schema_site_anp_epg:
65 host: mso_host
66 username: admin
67 password: SomeSecretPassword
68 schema: Schema1
69 site: Site1
70 template: Template1
71 anp: ANP1
72 epg: EPG1
73 state: present
74 delegate_to: localhost
75
76- name: Remove a site EPG
77 mso_schema_site_anp_epg:
78 host: mso_host
79 username: admin
80 password: SomeSecretPassword
81 schema: Schema1
82 site: Site1
83 template: Template1
84 anp: ANP1
85 epg: EPG1
86 state: absent
87 delegate_to: localhost
88
89- name: Query a specific site EPGs
90 mso_schema_site_anp_epg:
91 host: mso_host
92 username: admin
93 password: SomeSecretPassword
94 schema: Schema1
95 site: Site1
96 template: Template1
97 anp: ANP1
98 epg: EPG1
99 state: query
100 delegate_to: localhost
101 register: query_result
102
103- name: Query all site EPGs
104 mso_schema_site_anp_epg:
105 host: mso_host
106 username: admin
107 password: SomeSecretPassword
108 schema: Schema1
109 site: Site1
110 template: Template1
111 anp: ANP1
112 state: query
113 delegate_to: localhost
114 register: query_result
115'''
116
117RETURN = r'''
118'''
119
120from ansible.module_utils.basic import AnsibleModule
121from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
122
123
124def main():
125 argument_spec = mso_argument_spec()
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']), # This parameter is not required for querying all objects
132 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
133 )
134
135 module = AnsibleModule(
136 argument_spec=argument_spec,
137 supports_check_mode=True,
138 required_if=[
139 ['state', 'absent', ['epg']],
140 ['state', 'present', ['epg']],
141 ],
142 )
143
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']
150
151 mso = MSOModule(module)
152
153 # Get schema_id
154 schema_obj = mso.get_obj('schemas', displayName=schema)
155 if not schema_obj:
156 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
157
158 schema_path = 'schemas/{id}'.format(**schema_obj)
159 schema_id = schema_obj['id']
160
161 # Get site
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)))
166
167 # Schema-access uses indexes
168 site_idx = sites.index((site_id, template))
169 # Path-based access uses site_id-template
170 site_template = '{0}-{1}'.format(site_id, template)
171
172 # Get ANP
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)
178
179 # Get EPG
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]
186
187 if state == 'query':
188 if epg is None:
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))
192 mso.exit_json()
193
194 epgs_path = '/sites/{0}/anps/{1}/epgs'.format(site_template, anp)
195 ops = []
196
197 mso.previous = mso.existing
198 if state == 'absent':
199 if mso.existing:
200 mso.sent = mso.existing = {}
201 ops.append(dict(op='remove', path=epg_path))
202
203 elif state == 'present':
204
205 payload = dict(
206 epgRef=dict(
207 schemaId=schema_id,
208 templateName=template,
209 anpName=anp,
210 epgName=epg,
211 ),
212 )
213
214 mso.sanitize(payload, collate=True)
215
216 if not mso.existing:
217 ops.append(dict(op='add', path=epgs_path + '/-', value=mso.sent))
218
219 mso.existing = mso.proposed
220
221 if not module.check_mode:
222 mso.request(schema_path, method='PATCH', data=ops)
223
224 mso.exit_json()
225
226
227if __name__ == "__main__":
228 main()