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_template_vrf.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_template_vrf
17short_description: Manage VRFs in schema templates
18description:
19- Manage VRFs in schema templates 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 template:
30 description:
31 - The name of the template.
32 type: str
33 required: yes
34 vrf:
35 description:
36 - The name of the VRF to manage.
37 type: str
38 aliases: [ name ]
39 display_name:
40 description:
41 - The name as displayed on the MSO web interface.
42 type: str
43 layer3_multicast:
44 description:
45 - Whether to enable L3 multicast.
46 type: bool
47 state:
48 description:
49 - Use C(present) or C(absent) for adding or removing.
50 - Use C(query) for listing an object or multiple objects.
51 type: str
52 choices: [ absent, present, query ]
53 default: present
54extends_documentation_fragment: mso
55'''
56
57EXAMPLES = r'''
58- name: Add a new VRF
59 mso_schema_template_vrf:
60 host: mso_host
61 username: admin
62 password: SomeSecretPassword
63 schema: Schema 1
64 template: Template 1
65 vrf: VRF 1
66 state: present
67 delegate_to: localhost
68
69- name: Remove an VRF
70 mso_schema_template_vrf:
71 host: mso_host
72 username: admin
73 password: SomeSecretPassword
74 schema: Schema 1
75 template: Template 1
76 vrf: VRF1
77 state: absent
78 delegate_to: localhost
79
80- name: Query a specific VRFs
81 mso_schema_template_vrf:
82 host: mso_host
83 username: admin
84 password: SomeSecretPassword
85 schema: Schema 1
86 template: Template 1
87 vrf: VRF1
88 state: query
89 delegate_to: localhost
90 register: query_result
91
92- name: Query all VRFs
93 mso_schema_template_vrf:
94 host: mso_host
95 username: admin
96 password: SomeSecretPassword
97 schema: Schema 1
98 template: Template 1
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, mso_reference_spec, issubset
109
110
111def main():
112 argument_spec = mso_argument_spec()
113 argument_spec.update(
114 schema=dict(type='str', required=True),
115 template=dict(type='str', required=True),
116 vrf=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
117 display_name=dict(type='str'),
118 layer3_multicast=dict(type='bool'),
119 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
120 )
121
122 module = AnsibleModule(
123 argument_spec=argument_spec,
124 supports_check_mode=True,
125 required_if=[
126 ['state', 'absent', ['vrf']],
127 ['state', 'present', ['vrf']],
128 ],
129 )
130
131 schema = module.params['schema']
132 template = module.params['template']
133 vrf = module.params['vrf']
134 display_name = module.params['display_name']
135 layer3_multicast = module.params['layer3_multicast']
136 state = module.params['state']
137
138 mso = MSOModule(module)
139
140 # Get schema_id
141 schema_obj = mso.get_obj('schemas', displayName=schema)
142 if not schema_obj:
143 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
144
145 schema_path = 'schemas/{id}'.format(**schema_obj)
146
147 # Get template
148 templates = [t['name'] for t in schema_obj['templates']]
149 if template not in templates:
150 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates)))
151 template_idx = templates.index(template)
152
153 # Get ANP
154 vrfs = [v['name'] for v in schema_obj['templates'][template_idx]['vrfs']]
155
156 if vrf is not None and vrf in vrfs:
157 vrf_idx = vrfs.index(vrf)
158 mso.existing = schema_obj['templates'][template_idx]['vrfs'][vrf_idx]
159
160 if state == 'query':
161 if vrf is None:
162 mso.existing = schema_obj['templates'][template_idx]['vrfs']
163 elif not mso.existing:
164 mso.fail_json(msg="VRF '{vrf}' not found".format(vrf=vrf))
165 mso.exit_json()
166
167 vrfs_path = '/templates/{0}/vrfs'.format(template)
168 vrf_path = '/templates/{0}/vrfs/{1}'.format(template, vrf)
169 ops = []
170
171 mso.previous = mso.existing
172 if state == 'absent':
173 if mso.existing:
174 mso.sent = mso.existing = {}
175 ops.append(dict(op='remove', path=vrf_path))
176
177 elif state == 'present':
178 if display_name is None and not mso.existing:
179 display_name = vrf
180
181 payload = dict(
182 name=vrf,
183 displayName=display_name,
184 l3MCast=layer3_multicast,
185 # FIXME
186 regions=[],
187 )
188
189 mso.sanitize(payload, collate=True)
190
191 if mso.existing:
192 ops.append(dict(op='replace', path=vrf_path, value=mso.sent))
193 else:
194 ops.append(dict(op='add', path=vrfs_path + '/-', value=mso.sent))
195
196 mso.existing = mso.proposed
197
198 if not module.check_mode:
199 mso.request(schema_path, method='PATCH', data=ops)
200
201 mso.exit_json()
202
203
204if __name__ == "__main__":
205 main()