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_l3out.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_l3out
17short_description: Manage site-local BD l3out's in schema template
18description:
19- Manage site-local BDs l3out's 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.
42 type: str
43 aliases: [ name ]
44 l3out:
45 description:
46 - The name of the l3out.
47 type: str
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
55notes:
56- The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index.
57 This can cause silent corruption on concurrent access when changing/removing on object as
58 the wrong object may be referenced. This module is affected by this deficiency.
59seealso:
60- module: mso_schema_site_bd
61- module: mso_schema_template_bd
62extends_documentation_fragment: mso
63'''
64
65EXAMPLES = r'''
66- name: Add a new site BD l3out
67 mso_schema_site_bd_l3out:
68 host: mso_host
69 username: admin
70 password: SomeSecretPassword
71 schema: Schema1
72 site: Site1
73 template: Template1
74 bd: BD1
75 l3out: L3out1
76 state: present
77 delegate_to: localhost
78
79- name: Remove a site BD l3out
80 mso_schema_site_bd_l3out:
81 host: mso_host
82 username: admin
83 password: SomeSecretPassword
84 schema: Schema1
85 site: Site1
86 template: Template1
87 bd: BD1
88 l3out: L3out1
89 state: absent
90 delegate_to: localhost
91
92- name: Query a specific site BD l3out
93 mso_schema_site_bd_l3out:
94 host: mso_host
95 username: admin
96 password: SomeSecretPassword
97 schema: Schema1
98 site: Site1
99 template: Template1
100 bd: BD1
101 l3out: L3out1
102 state: query
103 delegate_to: localhost
104 register: query_result
105
106- name: Query all site BD l3outs
107 mso_schema_site_bd_l3out:
108 host: mso_host
109 username: admin
110 password: SomeSecretPassword
111 schema: Schema1
112 site: Site1
113 template: Template1
114 bd: BD1
115 state: query
116 delegate_to: localhost
117 register: query_result
118'''
119
120RETURN = r'''
121'''
122
123from ansible.module_utils.basic import AnsibleModule
124from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
125
126
127def main():
128 argument_spec = mso_argument_spec()
129 argument_spec.update(
130 schema=dict(type='str', required=True),
131 site=dict(type='str', required=True),
132 template=dict(type='str', required=True),
133 bd=dict(type='str', required=True),
134 l3out=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
135 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
136 )
137
138 module = AnsibleModule(
139 argument_spec=argument_spec,
140 supports_check_mode=True,
141 required_if=[
142 ['state', 'absent', ['l3out']],
143 ['state', 'present', ['l3out']],
144 ],
145 )
146
147 schema = module.params['schema']
148 site = module.params['site']
149 template = module.params['template']
150 bd = module.params['bd']
151 l3out = module.params['l3out']
152 state = module.params['state']
153
154 mso = MSOModule(module)
155
156 # Get schema_id
157 schema_obj = mso.get_obj('schemas', displayName=schema)
158 if not schema_obj:
159 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
160
161 schema_path = 'schemas/{id}'.format(**schema_obj)
162 schema_id = schema_obj['id']
163
164 # Get site
165 site_id = mso.lookup_site(site)
166
167 # Get site_idx
168 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
169 if (site_id, template) not in sites:
170 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites)))
171
172 # Schema-access uses indexes
173 site_idx = sites.index((site_id, template))
174 # Path-based access uses site_id-template
175 site_template = '{0}-{1}'.format(site_id, template)
176
177 # Get BD
178 bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd)
179 bds = [v['bdRef'] for v in schema_obj['sites'][site_idx]['bds']]
180 if bd_ref not in bds:
181 mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ', '.join(bds)))
182 bd_idx = bds.index(bd_ref)
183
184 # Get L3out
185 l3outs = schema_obj['sites'][site_idx]['bds'][bd_idx]['l3Outs']
186 if l3out is not None and l3out in l3outs:
187 l3out_idx = l3outs.index(l3out)
188 # FIXME: Changes based on index are DANGEROUS
189 l3out_path = '/sites/{0}/bds/{1}/l3Outs/{2}'.format(site_template, bd, l3out_idx)
190 mso.existing = schema_obj['sites'][site_idx]['bds'][bd_idx]['l3Outs'][l3out_idx]
191
192 if state == 'query':
193 if l3out is None:
194 mso.existing = schema_obj['sites'][site_idx]['bds'][bd_idx]['l3Outs']
195 elif not mso.existing:
196 mso.fail_json(msg="L3out '{l3out}' not found".format(l3out=l3out))
197 mso.exit_json()
198
199 l3outs_path = '/sites/{0}/bds/{1}/l3Outs'.format(site_template, bd)
200 ops = []
201
202 mso.previous = mso.existing
203 if state == 'absent':
204 if mso.existing:
205 mso.sent = mso.existing = {}
206 ops.append(dict(op='remove', path=l3out_path))
207
208 elif state == 'present':
209 mso.sent = l3out
210 if not mso.existing:
211 ops.append(dict(op='add', path=l3outs_path + '/-', value=l3out))
212
213 mso.existing = mso.sent
214
215 if not module.check_mode:
216 mso.request(schema_path, method='PATCH', data=ops)
217
218 mso.exit_json()
219
220
221if __name__ == "__main__":
222 main()