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.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
17short_description: Manage schemas
18description:
19- Manage schemas 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 aliases: [ name ]
30 templates:
31 description:
32 - A list of templates for this schema.
33 type: list
34 sites:
35 description:
36 - A list of sites mapped to templates in this schema.
37 type: list
38 state:
39 description:
40 - Use C(present) or C(absent) for adding or removing.
41 - Use C(query) for listing an object or multiple objects.
42 type: str
43 choices: [ absent, present, query ]
44 default: present
45notes:
46- Due to restrictions of the MSO REST API this module cannot create empty schemas (i.e. schemas without templates).
47 Use the M(mso_schema_template) to automatically create schemas with templates.
48seealso:
49- module: mso_schema_site
50- module: mso_schema_template
51extends_documentation_fragment: mso
52'''
53
54EXAMPLES = r'''
55- name: Add a new schema
56 mso_schema:
57 host: mso_host
58 username: admin
59 password: SomeSecretPassword
60 schema: Schema 1
61 state: present
62 templates:
63 - name: Template1
64 displayName: Template 1
65 tenantId: north_europe
66 anps:
67 <...>
68 - name: Template2
69 displayName: Template 2
70 tenantId: nort_europe
71 anps:
72 <...>
73 delegate_to: localhost
74
75- name: Remove schemas
76 mso_schema:
77 host: mso_host
78 username: admin
79 password: SomeSecretPassword
80 schema: Schema 1
81 state: absent
82 delegate_to: localhost
83
84- name: Query a schema
85 mso_schema:
86 host: mso_host
87 username: admin
88 password: SomeSecretPassword
89 schema: Schema 1
90 state: query
91 delegate_to: localhost
92 register: query_result
93
94- name: Query all schemas
95 mso_schema:
96 host: mso_host
97 username: admin
98 password: SomeSecretPassword
99 state: query
100 delegate_to: localhost
101 register: query_result
102'''
103
104RETURN = r'''
105'''
106
107from ansible.module_utils.basic import AnsibleModule
108from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
109
110
111def main():
112 argument_spec = mso_argument_spec()
113 argument_spec.update(
114 schema=dict(type='str', aliases=['name']),
115 templates=dict(type='list'),
116 sites=dict(type='list'),
117 # messages=dict(type='dict'),
118 # associations=dict(type='list'),
119 # health_faults=dict(type='list'),
120 # references=dict(type='dict'),
121 # policy_states=dict(type='list'),
122 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
123 )
124
125 module = AnsibleModule(
126 argument_spec=argument_spec,
127 supports_check_mode=True,
128 required_if=[
129 ['state', 'absent', ['schema']],
130 ['state', 'present', ['schema', 'templates']],
131 ],
132 )
133
134 schema = module.params['schema']
135 templates = module.params['templates']
136 sites = module.params['sites']
137 state = module.params['state']
138
139 mso = MSOModule(module)
140
141 schema_id = None
142 path = 'schemas'
143
144 # Query for existing object(s)
145 if schema:
146 mso.existing = mso.get_obj(path, displayName=schema)
147 if mso.existing:
148 schema_id = mso.existing['id']
149 path = 'schemas/{id}'.format(id=schema_id)
150 else:
151 mso.existing = mso.query_objs(path)
152
153 if state == 'query':
154 pass
155
156 elif state == 'absent':
157 mso.previous = mso.existing
158 if mso.existing:
159 if module.check_mode:
160 mso.existing = {}
161 else:
162 mso.existing = mso.request(path, method='DELETE')
163
164 elif state == 'present':
165 mso.previous = mso.existing
166
167 payload = dict(
168 id=schema_id,
169 displayName=schema,
170 templates=templates,
171 sites=sites,
172 )
173
174 mso.sanitize(payload, collate=True)
175
176 if mso.existing:
177 if not issubset(mso.sent, mso.existing):
178 if module.check_mode:
179 mso.existing = mso.proposed
180 else:
181 mso.existing = mso.request(path, method='PUT', data=mso.sent)
182 else:
183 if module.check_mode:
184 mso.existing = mso.proposed
185 else:
186 mso.existing = mso.request(path, method='POST', data=mso.sent)
187
188 mso.exit_json()
189
190
191if __name__ == "__main__":
192 main()
def issubset(subset, superset)
Definition: mso.py:19