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_bd.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2019, 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_bd
17short_description: Manage site-local Bridge Domains (BDs) in schema template
18description:
19- Manage site-local BDs in schema template 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.
32 type: str
33 required: yes
34 template:
35 description:
36 - The name of the template.
37 type: str
38 required: yes
39 bd:
40 description:
41 - The name of the BD to manage.
42 type: str
43 aliases: [ name ]
44 host_route:
45 description:
46 - Whether host-based routing is enabled.
47 type: bool
48 state:
49 description:
50 - Use C(present) or C(absent) for adding or removing.
51 - Use C(query) for listing an object or multiple objects.
52 type: str
53 choices: [ absent, present, query ]
54 default: present
55seealso:
56- module: mso_schema_site
57- module: mso_schema_site_bd_l3out
58- module: mso_schema_site_bd_subnet
59- module: mso_schema_template_bd
60extends_documentation_fragment: mso
61'''
62
63EXAMPLES = r'''
64- name: Add a new site BD
65 mso_schema_site_bd:
66 host: mso_host
67 username: admin
68 password: SomeSecretPassword
69 schema: Schema1
70 site: Site1
71 template: Template1
72 bd: BD1
73 state: present
74 delegate_to: localhost
75
76- name: Remove a site BD
77 mso_schema_site_bd:
78 host: mso_host
79 username: admin
80 password: SomeSecretPassword
81 schema: Schema1
82 site: Site1
83 template: Template1
84 bd: BD1
85 state: absent
86 delegate_to: localhost
87
88- name: Query a specific site BD
89 mso_schema_site_bd:
90 host: mso_host
91 username: admin
92 password: SomeSecretPassword
93 schema: Schema1
94 site: Site1
95 template: Template1
96 bd: BD1
97 state: query
98 delegate_to: localhost
99 register: query_result
100
101- name: Query all site BDs
102 mso_schema_site_bd:
103 host: mso_host
104 username: admin
105 password: SomeSecretPassword
106 schema: Schema1
107 site: Site1
108 template: Template1
109 state: query
110 delegate_to: localhost
111 register: query_result
112'''
113
114RETURN = r'''
115'''
116
117from ansible.module_utils.basic import AnsibleModule
118from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
119
120
121def main():
122 argument_spec = mso_argument_spec()
123 argument_spec.update(
124 schema=dict(type='str', required=True),
125 site=dict(type='str', required=True),
126 template=dict(type='str', required=True),
127 bd=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
128 host_route=dict(type='bool'),
129 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
130 )
131
132 module = AnsibleModule(
133 argument_spec=argument_spec,
134 supports_check_mode=True,
135 required_if=[
136 ['state', 'absent', ['bd']],
137 ['state', 'present', ['bd']],
138 ],
139 )
140
141 schema = module.params['schema']
142 site = module.params['site']
143 template = module.params['template']
144 bd = module.params['bd']
145 host_route = module.params['host_route']
146 state = module.params['state']
147
148 mso = MSOModule(module)
149
150 # Get schema_id
151 schema_obj = mso.get_obj('schemas', displayName=schema)
152 if not schema_obj:
153 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
154
155 schema_path = 'schemas/{id}'.format(**schema_obj)
156 schema_id = schema_obj['id']
157
158 # Get site
159 site_id = mso.lookup_site(site)
160
161 # Get site_idx
162 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
163 if (site_id, template) not in sites:
164 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites)))
165
166 # Schema-access uses indexes
167 site_idx = sites.index((site_id, template))
168 # Path-based access uses site_id-template
169 site_template = '{0}-{1}'.format(site_id, template)
170
171 # Get BD
172 bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd)
173 bds = [v['bdRef'] for v in schema_obj['sites'][site_idx]['bds']]
174 if bd is not None and bd_ref in bds:
175 bd_idx = bds.index(bd_ref)
176 bd_path = '/sites/{0}/bds/{1}'.format(site_template, bd)
177 mso.existing = schema_obj['sites'][site_idx]['bds'][bd_idx]
178
179 if state == 'query':
180 if bd is None:
181 mso.existing = schema_obj['sites'][site_idx]['bds']
182 elif not mso.existing:
183 mso.fail_json(msg="BD '{bd}' not found".format(bd=bd))
184 mso.exit_json()
185
186 bds_path = '/sites/{0}/bds'.format(site_template)
187 ops = []
188
189 mso.previous = mso.existing
190 if state == 'absent':
191 if mso.existing:
192 mso.sent = mso.existing = {}
193 ops.append(dict(op='remove', path=bd_path))
194
195 elif state == 'present':
196 if not mso.existing:
197 if host_route is None:
198 host_route = False
199
200 payload = dict(
201 bdRef=dict(
202 schemaId=schema_id,
203 templateName=template,
204 bdName=bd,
205 ),
206 hostBasedRouting=host_route,
207 )
208
209 mso.sanitize(payload, collate=True)
210
211 if mso.existing:
212 ops.append(dict(op='replace', path=bd_path, value=mso.sent))
213 else:
214 ops.append(dict(op='add', path=bds_path + '/-', value=mso.sent))
215
216 mso.existing = mso.proposed
217
218 if not module.check_mode:
219 mso.request(schema_path, method='PATCH', data=ops)
220
221 mso.exit_json()
222
223
224if __name__ == "__main__":
225 main()