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
17short_description: Manage templates in schemas
19- Manage templates on Cisco ACI Multi-Site.
21- Dag Wieers (@dagwieers)
26 - The tenant used for this template.
31 - The name of the schema.
36 - The name of the template.
41 - The name
as displayed on the MSO web interface.
45 - Use C(present)
or C(absent)
for adding
or removing.
46 - Use C(query)
for listing an object
or multiple objects.
48 choices: [ absent, present, query ]
51- Due to restrictions of the MSO REST API this module creates schemas when needed,
and removes them when the last template has been removed.
54- module: mso_schema_site
55extends_documentation_fragment: mso
59- name: Add a new template to a schema
63 password: SomeSecretPassword
68 delegate_to: localhost
70- name: Remove a template from a schema
74 password: SomeSecretPassword
79 delegate_to: localhost
81- name: Query a template
85 password: SomeSecretPassword
90 delegate_to: localhost
91 register: query_result
93- name: Query all templates
97 password: SomeSecretPassword
101 delegate_to: localhost
102 register: query_result
108from ansible.module_utils.basic import AnsibleModule
109from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
114 argument_spec.update(
115 tenant=dict(type=
'str', required=
True),
116 schema=dict(type=
'str', required=
True),
117 template=dict(type=
'str', aliases=[
'name']),
118 display_name=dict(type=
'str'),
119 state=dict(type=
'str', default=
'present', choices=[
'absent',
'present',
'query']),
123 argument_spec=argument_spec,
124 supports_check_mode=
True,
126 [
'state',
'absent', [
'template']],
127 [
'state',
'present', [
'template']],
131 tenant = module.params[
'tenant']
132 schema = module.params[
'schema']
133 template = module.params[
'template']
134 display_name = module.params[
'display_name']
135 state = module.params[
'state']
140 schema_obj = mso.get_obj(
'schemas', displayName=schema)
145 schema_path =
'schemas/{id}'.
format(**schema_obj)
148 templates = [t[
'name']
for t
in schema_obj[
'templates']]
150 if template
in templates:
151 template_idx = templates.index(template)
152 mso.existing = schema_obj[
'templates'][template_idx]
154 mso.existing = schema_obj[
'templates']
156 schema_path =
'schemas'
161 mso.fail_json(msg=
"Template '{0}' not found".
format(template))
166 template_path =
'/templates/{0}'.
format(template)
169 mso.previous = mso.existing
170 if state ==
'absent':
171 mso.proposed = mso.sent = {}
176 elif len(templates) == 1:
179 if not module.check_mode:
180 mso.request(schema_path, method=
'DELETE')
184 ops.append(dict(op=
'remove', path=template_path))
189 elif state ==
'present':
190 tenant_id = mso.lookup_tenant(tenant)
192 if display_name
is None:
193 display_name = mso.existing.get(
'displayName', template)
201 displayName=display_name,
207 mso.existing = payload[
'templates'][0]
209 if not module.check_mode:
210 mso.request(schema_path, method=
'POST', data=payload)
216 displayName=display_name,
220 mso.sanitize(payload, collate=
True)
222 ops.append(dict(op=
'replace', path=template_path +
'/displayName', value=display_name))
223 ops.append(dict(op=
'replace', path=template_path +
'/tenantId', value=tenant_id))
225 mso.existing = mso.proposed
230 displayName=display_name,
234 mso.sanitize(payload, collate=
True)
236 ops.append(dict(op=
'add', path=
'/templates/-', value=payload))
238 mso.existing = mso.proposed
240 if not module.check_mode:
241 mso.request(schema_path, method=
'PATCH', data=ops)
246if __name__ ==
"__main__":