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.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_site
17short_description: Manage sites in schemas
18description:
19- Manage sites 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 to manage.
32 type: str
33 required: yes
34 template:
35 description:
36 - The name of the template.
37 type: str
38 required: yes
39 aliases: [ name ]
40 state:
41 description:
42 - Use C(present) or C(absent) for adding or removing.
43 - Use C(query) for listing an object or multiple objects.
44 type: str
45 choices: [ absent, present, query ]
46 default: present
47seealso:
48- module: mso_schema_template
49- module: mso_site
50extends_documentation_fragment: mso
51'''
52
53EXAMPLES = r'''
54- name: Add a new site to a schema
55 mso_schema_site:
56 host: mso_host
57 username: admin
58 password: SomeSecretPassword
59 schema: Schema 1
60 site: bdsol-pod51
61 template: Template 1
62 state: present
63 delegate_to: localhost
64
65- name: Remove a site from a schema
66 mso_schema_site:
67 host: mso_host
68 username: admin
69 password: SomeSecretPassword
70 schema: Schema 1
71 site: bdsol-pod51
72 template: Template 1
73 state: absent
74 delegate_to: localhost
75
76- name: Query a schema site
77 mso_schema_site:
78 host: mso_host
79 username: admin
80 password: SomeSecretPassword
81 schema: Schema 1
82 site: bdsol-pod51
83 template: Template 1
84 state: query
85 delegate_to: localhost
86 register: query_result
87
88- name: Query all schema sites
89 mso_schema_site:
90 host: mso_host
91 username: admin
92 password: SomeSecretPassword
93 schema: Schema 1
94 site: bdsol-pod51
95 state: query
96 delegate_to: localhost
97 register: query_result
98'''
99
100RETURN = r'''
101'''
102
103from ansible.module_utils.basic import AnsibleModule
104from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
105
106
107def main():
108 argument_spec = mso_argument_spec()
109 argument_spec.update(
110 schema=dict(type='str', required=True),
111 site=dict(type='str', aliases=['name']),
112 template=dict(type='str'),
113 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
114 )
115
116 module = AnsibleModule(
117 argument_spec=argument_spec,
118 supports_check_mode=True,
119 required_if=[
120 ['state', 'absent', ['site', 'template']],
121 ['state', 'present', ['site', 'template']],
122 ],
123 )
124
125 schema = module.params['schema']
126 site = module.params['site']
127 template = module.params['template']
128 state = module.params['state']
129
130 mso = MSOModule(module)
131
132 # Get schema
133 schema_obj = mso.get_obj('schemas', displayName=schema)
134 if not schema_obj:
135 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
136
137 # Schema exists
138 schema_path = 'schemas/{id}'.format(**schema_obj)
139
140 # Get site
141 site_id = mso.lookup_site(site)
142
143 mso.existing = {}
144 if 'sites' in schema_obj:
145 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
146 if template:
147 if (site_id, template) in sites:
148 site_idx = sites.index((site_id, template))
149 mso.existing = schema_obj['sites'][site_idx]
150 else:
151 mso.existing = schema_obj['sites']
152
153 if state == 'query':
154 if not mso.existing:
155 if template:
156 mso.fail_json(msg="Template '{0}' not found".format(template))
157 else:
158 mso.existing = []
159 mso.exit_json()
160
161 sites_path = '/sites'
162 site_path = '/sites/{0}'.format(site)
163 ops = []
164
165 mso.previous = mso.existing
166 if state == 'absent':
167 if mso.existing:
168 # Remove existing site
169 mso.sent = mso.existing = {}
170 ops.append(dict(op='remove', path=site_path))
171
172 elif state == 'present':
173 if not mso.existing:
174 # Add new site
175 payload = dict(
176 siteId=site_id,
177 templateName=template,
178 anps=[],
179 bds=[],
180 contracts=[],
181 externalEpgs=[],
182 intersiteL3outs=[],
183 serviceGraphs=[],
184 vrfs=[],
185 )
186
187 mso.sanitize(payload, collate=True)
188
189 ops.append(dict(op='add', path=sites_path + '/-', value=mso.sent))
190
191 mso.existing = mso.proposed
192
193 if not module.check_mode:
194 mso.request(schema_path, method='PATCH', data=ops)
195
196 mso.exit_json()
197
198
199if __name__ == "__main__":
200 main()