7from __future__
import absolute_import, division, print_function
10ANSIBLE_METADATA = {
'metadata_version':
'1.1',
11 'status': [
'preview'],
12 'supported_by':
'community'}
16module: mso_schema_template_filter_entry
17short_description: Manage filter entries in schema templates
19- Manage filter entries in schema templates on Cisco ACI Multi-Site.
21- Dag Wieers (@dagwieers)
26 - The name of the schema.
31 - The name of the template.
36 - The name of the filter to manage.
41 - The name as displayed on the MSO web interface.
45 - The filter entry name to manage.
50 - The name
as displayed on the MSO web interface.
52 aliases: [ entry_display_name ]
55 - The description of this filer entry.
57 aliases: [ entry_description ]
60 - The ethernet type to use
for this filter entry.
62 choices: [ arp, fcoe, ip, ipv4, ipv6, mac-security, mpls-unicast, trill, unspecified ]
65 - The IP protocol to use
for this filter entry.
67 choices: [ eigrp, egp, icmp, icmpv6, igmp, igp, l2tp, ospfigp, pim, tcp, udp, unspecified ]
70 - A list of TCP session rules.
72 choices: [ acknowledgement, established, finish, synchronize, reset, unspecified ]
75 - The source port range
from.
79 - The source port range to.
83 - The destination port range
from.
87 - The destination port range to.
91 - The ARP flag to use
for this filter entry.
93 choices: [ reply, request, unspecified ]
96 - Whether this filter entry
is stateful.
101 - Whether this filter entry only matches fragments.
106 - Use C(present)
or C(absent)
for adding
or removing.
107 - Use C(query)
for listing an object
or multiple objects.
109 choices: [ absent, present, query ]
112- module: mso_schema_template_contract_filter
114- Due to restrictions of the MSO REST API this module creates filters when needed,
and removes them when the last entry has been removed.
115extends_documentation_fragment: mso
119- name: Add a new filter entry
120 mso_schema_template_filter_entry:
123 password: SomeSecretPassword
128 delegate_to: localhost
130- name: Remove a filter entry
131 mso_schema_template_filter_entry:
134 password: SomeSecretPassword
139 delegate_to: localhost
141- name: Query a specific filter entry
142 mso_schema_template_filter_entry:
145 password: SomeSecretPassword
150 delegate_to: localhost
151 register: query_result
153- name: Query all filter entries
154 mso_schema_template_filter_entry:
157 password: SomeSecretPassword
161 delegate_to: localhost
162 register: query_result
168from ansible.module_utils.basic import AnsibleModule
169from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, mso_reference_spec, issubset
174 argument_spec.update(
175 schema=dict(type=
'str', required=
True),
176 template=dict(type=
'str', required=
True),
177 filter=dict(type=
'str', required=
True),
178 filter_display_name=dict(type=
'str'),
179 entry=dict(type=
'str', aliases=[
'name']),
180 description=dict(type=
'str', aliases=[
'entry_description']),
181 display_name=dict(type=
'str', aliases=[
'entry_display_name']),
182 ethertype=dict(type=
'str', choices=[
'arp',
'fcoe',
'ip',
'ipv4',
'ipv6',
'mac-security',
'mpls-unicast',
'trill',
'unspecified']),
183 ip_protocol=dict(type=
'str', choices=[
'eigrp',
'egp',
'icmp',
'icmpv6',
'igmp',
'igp',
'l2tp',
'ospfigp',
'pim',
'tcp',
'udp',
'unspecified']),
184 tcp_session_rules=dict(type=
'list', choices=[
'acknowledgement',
'established',
'finish',
'synchronize',
'reset',
'unspecified']),
185 source_from=dict(type=
'str'),
186 source_to=dict(type=
'str'),
187 destination_from=dict(type=
'str'),
188 destination_to=dict(type=
'str'),
189 arp_flag=dict(type=
'str', choices=[
'reply',
'request',
'unspecified']),
190 stateful=dict(type=
'bool'),
191 fragments_only=dict(type=
'bool'),
192 state=dict(type=
'str', default=
'present', choices=[
'absent',
'present',
'query']),
196 argument_spec=argument_spec,
197 supports_check_mode=
True,
199 [
'state',
'absent', [
'entry']],
200 [
'state',
'present', [
'entry']],
204 schema = module.params[
'schema']
205 template = module.params[
'template']
206 filter_name = module.params[
'filter']
207 filter_display_name = module.params[
'filter_display_name']
208 entry = module.params[
'entry']
209 display_name = module.params[
'display_name']
210 description = module.params[
'description']
211 ethertype = module.params[
'ethertype']
212 ip_protocol = module.params[
'ip_protocol']
213 tcp_session_rules = module.params[
'tcp_session_rules']
214 source_from = module.params[
'source_from']
215 source_to = module.params[
'source_to']
216 destination_from = module.params[
'destination_from']
217 destination_to = module.params[
'destination_to']
218 arp_flag = module.params[
'arp_flag']
219 stateful = module.params[
'stateful']
220 fragments_only = module.params[
'fragments_only']
221 state = module.params[
'state']
226 schema_obj = mso.get_obj(
'schemas', displayName=schema)
228 mso.fail_json(msg=
"Provided schema '{0}' does not exist".
format(schema))
230 schema_path =
'schemas/{id}'.
format(**schema_obj)
233 templates = [t[
'name']
for t
in schema_obj[
'templates']]
234 if template
not in templates:
235 mso.fail_json(msg=
"Provided template '{template}' does not exist. Existing templates: {templates}".
format(template=template,
236 templates=
', '.join(templates)))
237 template_idx = templates.index(template)
243 filters = [f[
'name']
for f
in schema_obj[
'templates'][template_idx][
'filters']]
244 if filter_name
in filters:
245 filter_idx = filters.index(filter_name)
247 entries = [f[
'name']
for f
in schema_obj[
'templates'][template_idx][
'filters'][filter_idx][
'entries']]
249 entry_idx = entries.index(entry)
250 mso.existing = schema_obj[
'templates'][template_idx][
'filters'][filter_idx][
'entries'][entry_idx]
254 if filter_idx
is None:
255 mso.fail_json(msg=
"Filter '{filter}' not found".
format(filter=filter_name))
256 mso.existing = schema_obj[
'templates'][template_idx][
'filters'][filter_idx][
'entries']
257 elif not mso.existing:
258 mso.fail_json(msg=
"Entry '{entry}' not found".
format(entry=entry))
261 filters_path =
'/templates/{0}/filters'.
format(template)
262 filter_path =
'/templates/{0}/filters/{1}'.
format(template, filter_name)
263 entries_path =
'/templates/{0}/filters/{1}/entries'.
format(template, filter_name)
264 entry_path =
'/templates/{0}/filters/{1}/entries/{2}'.
format(template, filter_name, entry)
267 mso.previous = mso.existing
268 if state ==
'absent':
269 mso.proposed = mso.sent = {}
271 if filter_idx
is None:
274 elif entry_idx
is None:
277 elif len(entries) == 1:
280 ops.append(dict(op=
'remove', path=filter_path))
284 ops.append(dict(op=
'remove', path=entry_path))
286 elif state ==
'present':
289 if display_name
is None:
291 if description
is None:
293 if ethertype
is None:
294 ethertype =
'unspecified'
295 if ip_protocol
is None:
296 ip_protocol =
'unspecified'
297 if tcp_session_rules
is None:
298 tcp_session_rules = [
'unspecified']
299 if source_from
is None:
300 source_from =
'unspecified'
301 if source_to
is None:
302 source_to =
'unspecified'
303 if destination_from
is None:
304 destination_from =
'unspecified'
305 if destination_to
is None:
306 destination_to =
'unspecified'
308 arp_flag =
'unspecified'
311 if fragments_only
is None:
312 fragments_only =
False
316 displayName=display_name,
317 description=description,
319 ipProtocol=ip_protocol,
320 tcpSessionRules=tcp_session_rules,
321 sourceFrom=source_from,
323 destinationFrom=destination_from,
324 destinationTo=destination_to,
327 matchOnlyFragments=fragments_only,
330 mso.sanitize(payload, collate=
True)
332 if filter_idx
is None:
334 if filter_display_name
is None:
335 filter_display_name = filter_name
339 displayName=filter_display_name,
343 ops.append(dict(op=
'add', path=filters_path +
'/-', value=payload))
345 elif entry_idx
is None:
347 ops.append(dict(op=
'add', path=entries_path +
'/-', value=mso.sent))
351 for (key, value)
in mso.sent.items():
352 ops.append(dict(op=
'replace', path=entry_path +
'/' + key, value=value))
354 mso.existing = mso.proposed
356 if not module.check_mode:
357 mso.request(schema_path, method=
'PATCH', data=ops)
362if __name__ ==
"__main__":