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_template_anp_epg_subnet.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2018, 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_template_anp_epg_subnet
17short_description: Manage EPG subnets in schema templates
18description:
19- Manage EPG subnets in schema templates 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 template:
30 description:
31 - The name of the template to change.
32 type: str
33 required: yes
34 anp:
35 description:
36 - The name of the ANP.
37 type: str
38 required: yes
39 epg:
40 description:
41 - The name of the EPG to manage.
42 type: str
43 required: yes
44 subnet:
45 description:
46 - The IP range in CIDR notation.
47 type: str
48 required: true
49 aliases: [ ip ]
50 description:
51 description:
52 - The description of this subnet.
53 type: str
54 scope:
55 description:
56 - The scope of the subnet.
57 type: str
58 choices: [ private, public ]
59 shared:
60 description:
61 - Whether this subnet is shared between VRFs.
62 type: bool
63 no_default_gateway:
64 description:
65 - Whether this subnet has a default gateway.
66 type: bool
67 state:
68 description:
69 - Use C(present) or C(absent) for adding or removing.
70 - Use C(query) for listing an object or multiple objects.
71 type: str
72 choices: [ absent, present, query ]
73 default: present
74notes:
75- Due to restrictions of the MSO REST API concurrent modifications to EPG subnets can be dangerous and corrupt data.
76extends_documentation_fragment: mso
77'''
78
79EXAMPLES = r'''
80- name: Add a new subnet to an EPG
81 mso_schema_template_anp_epg_subnet:
82 host: mso_host
83 username: admin
84 password: SomeSecretPassword
85 schema: Schema 1
86 template: Template 1
87 anp: ANP 1
88 epg: EPG 1
89 subnet: 10.0.0.0/24
90 state: present
91 delegate_to: localhost
92
93- name: Remove a subnet from an EPG
94 mso_schema_template_anp_epg_subnet:
95 host: mso_host
96 username: admin
97 password: SomeSecretPassword
98 schema: Schema 1
99 template: Template 1
100 anp: ANP 1
101 epg: EPG 1
102 subnet: 10.0.0.0/24
103 state: absent
104 delegate_to: localhost
105
106- name: Query a specific EPG subnet
107 mso_schema_template_anp_epg_subnet:
108 host: mso_host
109 username: admin
110 password: SomeSecretPassword
111 schema: Schema 1
112 template: Template 1
113 anp: ANP 1
114 epg: EPG 1
115 subnet: 10.0.0.0/24
116 state: query
117 delegate_to: localhost
118 register: query_result
119
120- name: Query all EPGs subnets
121 mso_schema_template_anp_epg_subnet:
122 host: mso_host
123 username: admin
124 password: SomeSecretPassword
125 schema: Schema 1
126 template: Template 1
127 anp: ANP 1
128 state: query
129 delegate_to: localhost
130 register: query_result
131'''
132
133RETURN = r'''
134'''
135
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
138
139
140def main():
141 argument_spec = mso_argument_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']),
148 )
149 argument_spec.update(mso_subnet_spec())
150
151 module = AnsibleModule(
152 argument_spec=argument_spec,
153 supports_check_mode=True,
154 required_if=[
155 ['state', 'absent', ['subnet']],
156 ['state', 'present', ['subnet']],
157 ],
158 )
159
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']
170
171 mso = MSOModule(module)
172
173 # Get schema
174 schema_obj = mso.get_obj('schemas', displayName=schema)
175 if not schema_obj:
176 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
177
178 schema_path = 'schemas/{id}'.format(**schema_obj)
179
180 # Get template
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)
186
187 # Get ANP
188 anps = [a['name'] for a in schema_obj['templates'][template_idx]['anps']]
189 if anp not in 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)
192
193 # Get EPG
194 epgs = [e['name'] for e in schema_obj['templates'][template_idx]['anps'][anp_idx]['epgs']]
195 if epg not in 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)
198
199 # Get Subnet
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)
203 # FIXME: Changes based on index are DANGEROUS
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]
206
207 if state == 'query':
208 if subnet is None:
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))
212 mso.exit_json()
213
214 subnets_path = '/templates/{0}/anps/{1}/epgs/{2}/subnets'.format(template, anp, epg)
215 ops = []
216
217 mso.previous = mso.existing
218 if state == 'absent':
219 if mso.existing:
220 mso.existing = {}
221 ops.append(dict(op='remove', path=subnet_path))
222
223 elif state == 'present':
224 if not mso.existing:
225 if description is None:
226 description = subnet
227 if scope is None:
228 scope = 'private'
229 if shared is None:
230 shared = False
231 if no_default_gateway is None:
232 no_default_gateway = False
233
234 payload = dict(
235 ip=subnet,
236 description=description,
237 scope=scope,
238 shared=shared,
239 noDefaultGateway=no_default_gateway,
240 )
241
242 mso.sanitize(payload, collate=True)
243
244 if mso.existing:
245 ops.append(dict(op='replace', path=subnet_path, value=mso.sent))
246 else:
247 ops.append(dict(op='add', path=subnets_path + '/-', value=mso.sent))
248
249 mso.existing = mso.proposed
250
251 if not module.check_mode:
252 mso.request(schema_path, method='PATCH', data=ops)
253
254 mso.exit_json()
255
256
257if __name__ == "__main__":
258 main()