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_vrf.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_vrf
17short_description: Manage site-local VRFs in schema template
18description:
19- Manage site-local VRFs 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 vrf:
40 description:
41 - The name of the VRF to manage.
42 type: str
43 aliases: [ name ]
44 state:
45 description:
46 - Use C(present) or C(absent) for adding or removing.
47 - Use C(query) for listing an object or multiple objects.
48 type: str
49 choices: [ absent, present, query ]
50 default: present
51seealso:
52- module: mso_schema_site
53- module: mso_schema_template_vrf
54extends_documentation_fragment: mso
55'''
56
57EXAMPLES = r'''
58- name: Add a new site VRF
59 mso_schema_site_vrf:
60 host: mso_host
61 username: admin
62 password: SomeSecretPassword
63 schema: Schema1
64 site: Site1
65 template: Template1
66 vrf: VRF1
67 state: present
68 delegate_to: localhost
69
70- name: Remove a site VRF
71 mso_schema_site_vrf:
72 host: mso_host
73 username: admin
74 password: SomeSecretPassword
75 schema: Schema1
76 site: Site1
77 template: Template1
78 vrf: VRF1
79 state: absent
80 delegate_to: localhost
81
82- name: Query a specific site VRF
83 mso_schema_site_vrf:
84 host: mso_host
85 username: admin
86 password: SomeSecretPassword
87 schema: Schema1
88 site: Site1
89 template: Template1
90 vrf: VRF1
91 state: query
92 delegate_to: localhost
93 register: query_result
94
95- name: Query all site VRFs
96 mso_schema_site_vrf:
97 host: mso_host
98 username: admin
99 password: SomeSecretPassword
100 schema: Schema1
101 site: Site1
102 template: Template1
103 state: query
104 delegate_to: localhost
105 register: query_result
106'''
107
108RETURN = r'''
109'''
110
111from ansible.module_utils.basic import AnsibleModule
112from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec
113
114
115def main():
116 argument_spec = mso_argument_spec()
117 argument_spec.update(
118 schema=dict(type='str', required=True),
119 site=dict(type='str', required=True),
120 template=dict(type='str', required=True),
121 vrf=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
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', ['vrf']],
130 ['state', 'present', ['vrf']],
131 ],
132 )
133
134 schema = module.params['schema']
135 site = module.params['site']
136 template = module.params['template']
137 vrf = module.params['vrf']
138 state = module.params['state']
139
140 mso = MSOModule(module)
141
142 # Get schema_id
143 schema_obj = mso.get_obj('schemas', displayName=schema)
144 if not schema_obj:
145 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
146
147 schema_path = 'schemas/{id}'.format(**schema_obj)
148 schema_id = schema_obj['id']
149
150 # Get site
151 site_id = mso.lookup_site(site)
152
153 # Get site_idx
154 sites = [(s['siteId'], s['templateName']) for s in schema_obj['sites']]
155 if (site_id, template) not in sites:
156 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ', '.join(sites)))
157
158 # Schema-access uses indexes
159 site_idx = sites.index((site_id, template))
160 # Path-based access uses site_id-template
161 site_template = '{0}-{1}'.format(site_id, template)
162
163 # Get VRF
164 vrf_ref = mso.vrf_ref(schema_id=schema_id, template=template, vrf=vrf)
165 vrfs = [v['vrfRef'] for v in schema_obj['sites'][site_idx]['vrfs']]
166 if vrf is not None and vrf_ref in vrfs:
167 vrf_idx = vrfs.index(vrf_ref)
168 vrf_path = '/sites/{0}/vrfs/{1}'.format(site_template, vrf)
169 mso.existing = schema_obj['sites'][site_idx]['vrfs'][vrf_idx]
170
171 if state == 'query':
172 if vrf is None:
173 mso.existing = schema_obj['sites'][site_idx]['vrfs']
174 elif not mso.existing:
175 mso.fail_json(msg="VRF '{vrf}' not found".format(vrf=vrf))
176 mso.exit_json()
177
178 vrfs_path = '/sites/{0}/vrfs'.format(site_template)
179 ops = []
180
181 mso.previous = mso.existing
182 if state == 'absent':
183 if mso.existing:
184 mso.sent = mso.existing = {}
185 ops.append(dict(op='remove', path=vrf_path))
186
187 elif state == 'present':
188 payload = dict(
189 vrfRef=dict(
190 schemaId=schema_id,
191 templateName=template,
192 vrfName=vrf,
193 ),
194 )
195
196 mso.sanitize(payload, collate=True)
197
198 if mso.existing:
199 ops.append(dict(op='replace', path=vrf_path, value=mso.sent))
200 else:
201 ops.append(dict(op='add', path=vrfs_path + '/-', value=mso.sent))
202
203 mso.existing = mso.proposed
204
205 if not module.check_mode:
206 mso.request(schema_path, method='PATCH', data=ops)
207
208 mso.exit_json()
209
210
211if __name__ == "__main__":
212 main()