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.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
17short_description: Manage templates in schemas
18description:
19- Manage templates on Cisco ACI Multi-Site.
20author:
21- Dag Wieers (@dagwieers)
22version_added: '2.8'
23options:
24 tenant:
25 description:
26 - The tenant used for this template.
27 type: str
28 required: yes
29 schema:
30 description:
31 - The name of the schema.
32 type: str
33 required: yes
34 template:
35 description:
36 - The name of the template.
37 type: str
38 aliases: [ name ]
39 display_name:
40 description:
41 - The name as displayed on the MSO web interface.
42 type: str
43 state:
44 description:
45 - Use C(present) or C(absent) for adding or removing.
46 - Use C(query) for listing an object or multiple objects.
47 type: str
48 choices: [ absent, present, query ]
49 default: present
50notes:
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.
52seealso:
53- module: mso_schema
54- module: mso_schema_site
55extends_documentation_fragment: mso
56'''
57
58EXAMPLES = r'''
59- name: Add a new template to a schema
60 mso_schema_template:
61 host: mso_host
62 username: admin
63 password: SomeSecretPassword
64 tenant: Tenant 1
65 schema: Schema 1
66 template: Template 1
67 state: present
68 delegate_to: localhost
69
70- name: Remove a template from a schema
71 mso_schema_template:
72 host: mso_host
73 username: admin
74 password: SomeSecretPassword
75 tenant: Tenant 1
76 schema: Schema 1
77 template: Template 1
78 state: absent
79 delegate_to: localhost
80
81- name: Query a template
82 mso_schema_template:
83 host: mso_host
84 username: admin
85 password: SomeSecretPassword
86 tenant: Tenant 1
87 schema: Schema 1
88 template: Template 1
89 state: query
90 delegate_to: localhost
91 register: query_result
92
93- name: Query all templates
94 mso_schema_template:
95 host: mso_host
96 username: admin
97 password: SomeSecretPassword
98 tenant: Tenant 1
99 schema: Schema 1
100 state: query
101 delegate_to: localhost
102 register: query_result
103'''
104
105RETURN = r'''
106'''
107
108from ansible.module_utils.basic import AnsibleModule
109from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
110
111
112def main():
113 argument_spec = 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']),
120 )
121
122 module = AnsibleModule(
123 argument_spec=argument_spec,
124 supports_check_mode=True,
125 required_if=[
126 ['state', 'absent', ['template']],
127 ['state', 'present', ['template']],
128 ],
129 )
130
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']
136
137 mso = MSOModule(module)
138
139 # Get schema
140 schema_obj = mso.get_obj('schemas', displayName=schema)
141
142 mso.existing = {}
143 if schema_obj:
144 # Schema exists
145 schema_path = 'schemas/{id}'.format(**schema_obj)
146
147 # Get template
148 templates = [t['name'] for t in schema_obj['templates']]
149 if template:
150 if template in templates:
151 template_idx = templates.index(template)
152 mso.existing = schema_obj['templates'][template_idx]
153 else:
154 mso.existing = schema_obj['templates']
155 else:
156 schema_path = 'schemas'
157
158 if state == 'query':
159 if not mso.existing:
160 if template:
161 mso.fail_json(msg="Template '{0}' not found".format(template))
162 else:
163 mso.existing = []
164 mso.exit_json()
165
166 template_path = '/templates/{0}'.format(template)
167 ops = []
168
169 mso.previous = mso.existing
170 if state == 'absent':
171 mso.proposed = mso.sent = {}
172
173 if not schema_obj:
174 # There was no schema to begin with
175 pass
176 elif len(templates) == 1:
177 # There is only one tenant, remove schema
178 mso.existing = {}
179 if not module.check_mode:
180 mso.request(schema_path, method='DELETE')
181 elif mso.existing:
182 # Remove existing template
183 mso.existing = {}
184 ops.append(dict(op='remove', path=template_path))
185 else:
186 # There was no template to begin with
187 pass
188
189 elif state == 'present':
190 tenant_id = mso.lookup_tenant(tenant)
191
192 if display_name is None:
193 display_name = mso.existing.get('displayName', template)
194
195 if not schema_obj:
196 # Schema does not exist, so we have to create it
197 payload = dict(
198 displayName=schema,
199 templates=[dict(
200 name=template,
201 displayName=display_name,
202 tenantId=tenant_id,
203 )],
204 sites=[],
205 )
206
207 mso.existing = payload['templates'][0]
208
209 if not module.check_mode:
210 mso.request(schema_path, method='POST', data=payload)
211
212 elif mso.existing:
213 # Template exists, so we have to update it
214 payload = dict(
215 name=template,
216 displayName=display_name,
217 tenantId=tenant_id,
218 )
219
220 mso.sanitize(payload, collate=True)
221
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))
224
225 mso.existing = mso.proposed
226 else:
227 # Template does not exist, so we have to add it
228 payload = dict(
229 name=template,
230 displayName=display_name,
231 tenantId=tenant_id,
232 )
233
234 mso.sanitize(payload, collate=True)
235
236 ops.append(dict(op='add', path='/templates/-', value=payload))
237
238 mso.existing = mso.proposed
239
240 if not module.check_mode:
241 mso.request(schema_path, method='PATCH', data=ops)
242
243 mso.exit_json()
244
245
246if __name__ == "__main__":
247 main()