1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Module nxos logging fixes (#52883)

* nxos_logging fixes
This commit is contained in:
Mike Wiebe 2019-03-06 13:55:03 -05:00 committed by Nathaniel Case
parent 0b579a0837
commit b090b57eac
3 changed files with 78 additions and 27 deletions

View file

@ -301,14 +301,16 @@ class LocalNxapi:
if isinstance(commands, (list, set, tuple)): if isinstance(commands, (list, set, tuple)):
commands = ' ;'.join(commands) commands = ' ;'.join(commands)
msg = { # Order should not matter but some versions of NX-OS software fail
'version': version, # to process the payload properly if 'input' gets serialized before
'type': command_type, # 'type' and the payload of 'input' contains the word 'type'.
'chunk': chunk, msg = collections.OrderedDict()
'sid': sid, msg['version'] = version
'input': commands, msg['type'] = command_type
'output_format': 'json' msg['chunk'] = chunk
} msg['sid'] = sid
msg['input'] = commands
msg['output_format'] = 'json'
return dict(ins_api=msg) return dict(ins_api=msg)
@ -448,7 +450,6 @@ class LocalNxapi:
commands = 'config replace {0}'.format(replace) commands = 'config replace {0}'.format(replace)
commands = to_list(commands) commands = to_list(commands)
msg, msg_timestamps = self.send_request(commands, output='config', check_status=True, msg, msg_timestamps = self.send_request(commands, output='config', check_status=True,
return_error=return_error, opts=opts) return_error=return_error, opts=opts)
if return_error: if return_error:
@ -664,13 +665,18 @@ class HttpApi:
raise ValueError("commit comment is not supported") raise ValueError("commit comment is not supported")
def read_module_context(self, module_key): def read_module_context(self, module_key):
if self._module_context.get(module_key): try:
return self._module_context[module_key] module_context = self._connection.read_module_context(module_key)
except ConnectionError as exc:
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
return None return module_context
def save_module_context(self, module_key, module_context): def save_module_context(self, module_key, module_context):
self._module_context[module_key] = module_context try:
self._connection.save_module_context(module_key, module_context)
except ConnectionError as exc:
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
return None return None

View file

@ -84,6 +84,9 @@ options:
purge: purge:
description: description:
- Remove any switch logging configuration that does not match what has been configured - Remove any switch logging configuration that does not match what has been configured
Not supported for ansible_connection local.
All nxos_logging tasks must use the same ansible_connection type.
type: bool type: bool
default: no default: no
version_added: '2.8' version_added: '2.8'
@ -182,6 +185,7 @@ from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args, normalize_interface from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args, normalize_interface
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
STATIC_CLI = {'link-enable': 'logging event link-status enable', STATIC_CLI = {'link-enable': 'logging event link-status enable',
'link-default': 'logging event link-status default', 'link-default': 'logging event link-status default',
'trunk-enable': 'logging event trunk-status enable', 'trunk-enable': 'logging event trunk-status enable',
@ -207,7 +211,7 @@ DEFAULT_LOGGING_LEVEL = {0: [],
DEST_GROUP = ['console', 'logfile', 'module', 'monitor', 'server'] DEST_GROUP = ['console', 'logfile', 'module', 'monitor', 'server']
def map_obj_to_commands(updates): def map_obj_to_commands(module, updates):
commands = list() commands = list()
want, have = updates want, have = updates
@ -295,6 +299,7 @@ def map_obj_to_commands(updates):
commands.append('logging level {0} {1}'.format( commands.append('logging level {0} {1}'.format(
w['facility'], STATIC_CLI[w['facility_link_status']])) w['facility'], STATIC_CLI[w['facility_link_status']]))
else: else:
if not match_facility_default(module, w['facility'], w['facility_level']):
commands.append('logging level {0} {1}'.format(w['facility'], commands.append('logging level {0} {1}'.format(w['facility'],
w['facility_level'])) w['facility_level']))
@ -313,6 +318,30 @@ def map_obj_to_commands(updates):
return commands return commands
def match_facility_default(module, facility, want_level):
''' Check wanted facility to see if it matches current device default '''
matches_default = False
# Sample output from show logging level command
# Facility Default Severity Current Session Severity
# -------- ---------------- ------------------------
# bfd 5 5
#
# 0(emergencies) 1(alerts) 2(critical)
# 3(errors) 4(warnings) 5(notifications)
# 6(information) 7(debugging)
regexl = r'\S+\s+(\d+)\s+(\d+)'
cmd = {'command': 'show logging level {0}'.format(facility), 'output': 'text'}
facility_data = run_commands(module, cmd)
for line in facility_data[0].split('\n'):
mo = re.search(regexl, line)
if mo and int(mo.group(1)) == int(want_level) and int(mo.group(2)) == int(want_level):
matches_default = True
return matches_default
def split_interface(interface): def split_interface(interface):
match = re.search(r'(\D+)(\S*)', interface, re.M) match = re.search(r'(\D+)(\S*)', interface, re.M)
if match: if match:
@ -719,7 +748,7 @@ def main():
timestamp=dict(choices=['microseconds', 'milliseconds', 'seconds']), timestamp=dict(choices=['microseconds', 'milliseconds', 'seconds']),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),
aggregate=dict(type='list'), aggregate=dict(type='list'),
purge=dict(default=False, type='bool') purge=dict(default=False, type='bool'),
) )
argument_spec.update(nxos_argument_spec) argument_spec.update(nxos_argument_spec)
@ -742,7 +771,7 @@ def main():
merged_wants = merge_wants(read_module_context(module), want) merged_wants = merge_wants(read_module_context(module), want)
have = map_config_to_obj(module) have = map_config_to_obj(module)
commands = map_obj_to_commands((want, have)) commands = map_obj_to_commands(module, (want, have))
result['commands'] = commands result['commands'] = commands
if commands: if commands:
@ -753,7 +782,7 @@ def main():
save_module_context(module, merged_wants) save_module_context(module, merged_wants)
if module.params.get('purge'): if module.params.get('purge'):
pcommands = map_obj_to_commands((outliers(have, merged_wants), have)) pcommands = map_obj_to_commands(module, (outliers(have, merged_wants), have))
if pcommands: if pcommands:
if not module.check_mode: if not module.check_mode:
load_config(module, pcommands) load_config(module, pcommands)

View file

@ -17,6 +17,7 @@ version_added: "2.6"
import json import json
import re import re
import collections
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
from ansible.module_utils.connection import ConnectionError from ansible.module_utils.connection import ConnectionError
@ -36,6 +37,18 @@ class HttpApi(HttpApiBase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(HttpApi, self).__init__(*args, **kwargs) super(HttpApi, self).__init__(*args, **kwargs)
self._device_info = None self._device_info = None
self._module_context = {}
def read_module_context(self, module_key):
if self._module_context.get(module_key):
return self._module_context[module_key]
return None
def save_module_context(self, module_key, module_context):
self._module_context[module_key] = module_context
return None
def send_request(self, data, **message_kwargs): def send_request(self, data, **message_kwargs):
output = None output = None
@ -201,12 +214,15 @@ def request_builder(commands, output, version='1.0', chunk='0', sid=None):
if isinstance(commands, (list, set, tuple)): if isinstance(commands, (list, set, tuple)):
commands = ' ;'.join(commands) commands = ' ;'.join(commands)
msg = { # Order should not matter but some versions of NX-OS software fail
'version': version, # to process the payload properly if 'input' gets serialized before
'type': command_type, # 'type' and the payload of 'input' contains the word 'type'.
'chunk': chunk, msg = collections.OrderedDict()
'sid': sid, msg['version'] = version
'input': commands, msg['type'] = command_type
'output_format': 'json' msg['chunk'] = chunk
} msg['sid'] = sid
msg['input'] = commands
msg['output_format'] = 'json'
return json.dumps(dict(ins_api=msg)) return json.dumps(dict(ins_api=msg))