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_externalepg.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_externalepg
17short_description: Manage external EPGs in schema templates
18description:
19- Manage external EPGs 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 externalepg:
35 description:
36 - The name of the external EPG 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 vrf:
44 description:
45 - The VRF associated to this ANP.
46 type: dict
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 external EPG
59 mso_schema_template_externalepg:
60 host: mso_host
61 username: admin
62 password: SomeSecretPassword
63 schema: Schema 1
64 template: Template 1
65 externalepg: External EPG 1
66 state: present
67 delegate_to: localhost
68
69- name: Remove an external EPG
70 mso_schema_template_externalepg:
71 host: mso_host
72 username: admin
73 password: SomeSecretPassword
74 schema: Schema 1
75 template: Template 1
76 externalepg: external EPG1
77 state: absent
78 delegate_to: localhost
79
80- name: Query a specific external EPGs
81 mso_schema_template_externalepg:
82 host: mso_host
83 username: admin
84 password: SomeSecretPassword
85 schema: Schema 1
86 template: Template 1
87 externalepg: external EPG1
88 state: query
89 delegate_to: localhost
90 register: query_result
91
92- name: Query all external EPGs
93 mso_schema_template_externalepg:
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 externalepg=dict(type='str', aliases=['name']), # This parameter is not required for querying all objects
117 display_name=dict(type='str'),
118 vrf=dict(type='dict', options=mso_reference_spec()),
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', ['externalepg']],
127 ['state', 'present', ['externalepg', 'vrf']],
128 ],
129 )
130
131 schema = module.params['schema']
132 template = module.params['template']
133 externalepg = module.params['externalepg']
134 display_name = module.params['display_name']
135 vrf = module.params['vrf']
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 schema_obj:
143 schema_id = schema_obj['id']
144 else:
145 mso.fail_json(msg="Provided schema '{0}' does not exist".format(schema))
146
147 schema_path = 'schemas/{id}'.format(**schema_obj)
148
149 # Get template
150 templates = [t['name'] for t in schema_obj['templates']]
151 if template not in templates:
152 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates)))
153 template_idx = templates.index(template)
154
155 # Get external EPGs
156 externalepgs = [e['name'] for e in schema_obj['templates'][template_idx]['externalEpgs']]
157
158 if externalepg is not None and externalepg in externalepgs:
159 externalepg_idx = externalepgs.index(externalepg)
160 mso.existing = schema_obj['templates'][template_idx]['externalEpgs'][externalepg_idx]
161
162 if state == 'query':
163 if externalepg is None:
164 mso.existing = schema_obj['templates'][template_idx]['externalEpgs']
165 elif not mso.existing:
166 mso.fail_json(msg="External EPG '{externalepg}' not found".format(externalepg=externalepg))
167 mso.exit_json()
168
169 eepgs_path = '/templates/{0}/externalEpgs'.format(template)
170 eepg_path = '/templates/{0}/externalEpgs/{1}'.format(template, externalepg)
171 ops = []
172
173 mso.previous = mso.existing
174 if state == 'absent':
175 if mso.existing:
176 mso.sent = mso.existing = {}
177 ops.append(dict(op='remove', path=eepg_path))
178
179 elif state == 'present':
180 vrf_ref = mso.make_reference(vrf, 'vrf', schema_id, template)
181
182 if display_name is None and not mso.existing:
183 display_name = externalepg
184
185 payload = dict(
186 name=externalepg,
187 displayName=display_name,
188 vrfRef=vrf_ref,
189 # FIXME
190 subnets=[],
191 contractRelationships=[],
192 )
193
194 mso.sanitize(payload, collate=True)
195
196 if mso.existing:
197 ops.append(dict(op='replace', path=eepg_path, value=mso.sent))
198 else:
199 ops.append(dict(op='add', path=eepgs_path + '/-', value=mso.sent))
200
201 mso.existing = mso.proposed
202
203 if not module.check_mode:
204 mso.request(schema_path, method='PATCH', data=ops)
205
206 mso.exit_json()
207
208
209if __name__ == "__main__":
210 main()