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

cliconf and netconf refactor of iosxr_logging (#34495)

* * cliconf and netconf refactor of iosxr_logging

* * documentation issue fix

* * adds required_if and mutually_exclusive in arg spec
This commit is contained in:
Kedar Kekan 2018-01-11 10:08:11 +05:30 committed by GitHub
parent b66863abf0
commit 08957cf46e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 837 additions and 253 deletions

View file

@ -65,6 +65,8 @@ NS_DICT = {
'INTERFACE-CONFIGURATIONS_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"}, 'INTERFACE-CONFIGURATIONS_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"},
'INFRA-STATISTICS_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-infra-statsd-oper"}, 'INFRA-STATISTICS_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-infra-statsd-oper"},
'INTERFACE-PROPERTIES_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-oper"}, 'INTERFACE-PROPERTIES_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-oper"},
'IP-DOMAIN_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-ip-domain-cfg"},
'SYSLOG_NSMAP': {None: "http://cisco.com/ns/yang/Cisco-IOS-XR-infra-syslog-cfg"},
} }
iosxr_provider_spec = { iosxr_provider_spec = {
@ -181,11 +183,17 @@ def build_xml_subtree(container_ele, xmap, param=None, opcode=None):
child = etree.SubElement(parent, candidates[-1]) child = etree.SubElement(parent, candidates[-1])
child.text = text child.text = text
if meta.get('attrib', None) and opcode in ('delete', 'merge'):
child.set(BASE_1_0 + meta.get('attrib'), opcode)
if len(meta_subtree) > 1: if len(meta_subtree) > 1:
for item in meta_subtree: for item in meta_subtree:
container_ele.append(item) container_ele.append(item)
return sub_root if sub_root == container_ele:
return None
else:
return sub_root
def build_xml(container, xmap=None, params=None, opcode=None): def build_xml(container, xmap=None, params=None, opcode=None):
@ -246,7 +254,9 @@ def build_xml(container, xmap=None, params=None, opcode=None):
subtree_list = list() subtree_list = list()
for param in to_list(params): for param in to_list(params):
subtree_list.append(build_xml_subtree(container_ele, xmap, param, opcode=opcode)) subtree_ele = build_xml_subtree(container_ele, xmap, param, opcode=opcode)
if subtree_ele is not None:
subtree_list.append(subtree_ele)
for item in subtree_list: for item in subtree_list:
container_ele.append(item) container_ele.append(item)

View file

@ -242,7 +242,9 @@ def main():
elif is_netconf(module): elif is_netconf(module):
config_object = NCConfiguration(module) config_object = NCConfiguration(module)
result = config_object.run() result = None
if config_object is not None:
result = config_object.run()
module.exit_json(**result) module.exit_json(**result)

View file

@ -16,318 +16,669 @@ DOCUMENTATION = """
--- ---
module: iosxr_logging module: iosxr_logging
version_added: "2.4" version_added: "2.4"
author: "Trishna Guha (@trishnaguha)" author:
short_description: Manage logging on network devices - "Trishna Guha (@trishnaguha)"
- "Kedar Kekan (@kedarX)"
short_description: Configuration management of system logging services on network devices
description: description:
- This module provides declarative management of logging - This module provides declarative management configuration of system logging (syslog)
on Cisco IOS XR devices. on Cisco IOS XR devices.
extends_documentation_fragment: iosxr
notes: notes:
- Tested against IOS XR 6.1.2 - Tested against IOS XRv 6.1.2
options: options:
dest: dest:
description: description:
- Destination of the logs. - Destination for system logging (syslog) messages.
choices: ['on', 'hostnameprefix', console', 'monitor', 'buffered'] choices: ['host', 'console', 'monitor', 'buffered', 'file']
name: name:
description: description:
- If value of C(dest) is I(file) it indicates file-name, - When C(dest) = I(file) name indicates file-name
for I(user) it indicates username and for I(host) indicates - When C(dest) = I(host) name indicates the host-name or ip-address of syslog server.
the host name to be notified. vrf:
description:
- vrf name when syslog server is configured, C(dest) = C(host)
default: default
version_added: 2.5
size: size:
description: description:
- Size of buffer. The acceptable value is in range from 307200 to - Size of buffer when C(dest) = C(buffered). The acceptable value is in the range I(307200 to 125000000 bytes). Default 307200
125000000 bytes. - Size of file when C(dest) = C(file). The acceptable value is in the range I(1 to 2097152)KB. Default 2 GB
default: 307200
facility: facility:
description: description:
- Set logging facility. - To configure the type of syslog facility in which system logging (syslog) messages are sent to syslog servers
Optional config for C(dest) = C(host)
default: local7 default: local7
hostnameprefix:
description:
- To append a hostname prefix to system logging (syslog) messages logged to syslog servers.
Optional config for C(dest) = C(host)
version_added: 2.5
level: level:
description: description:
- Set logging severity levels. - Specifies the severity level for the logging.
default: debugging default: debugging
aliases: ['severity']
aggregate: aggregate:
description: List of logging definitions. description: List of syslog logging configuration definitions.
state: state:
description: description:
- State of the logging configuration. - Existential state of the logging configuration on the node.
default: present default: present
choices: ['present', 'absent'] choices: ['present', 'absent']
""" """
EXAMPLES = """ EXAMPLES = """
- name: configure hostnameprefix logging - name: configure logging for syslog server host
iosxr_logging: iosxr_logging:
dest: hostnameprefix dest: host
name: 172.16.0.1 name: 10.10.10.1
level: critical
state: present state: present
- name: remove hostnameprefix logging configuration - name: add hostnameprefix configuration
iosxr_logging: iosxr_logging:
dest: hostnameprefix hostnameprefix: host1
name: 172.16.0.1
state: absent state: absent
- name: configure console logging level and facility - name: add facility configuration
iosxr_logging:
facility: local1
state: present
- name: configure console logging level
iosxr_logging: iosxr_logging:
dest: console dest: console
facility: local7
level: debugging level: debugging
state: present state: present
- name: enable logging to all - name: configure monitor logging level
iosxr_logging: iosxr_logging:
dest : on dest: monitor
level: errors
state: present
- name: configure buffer size - name: configure syslog to a file
iosxr_logging:
dest: file
name: file_name
size: 2048
level: errors
state: present
- name: configure buffered logging with size
iosxr_logging: iosxr_logging:
dest: buffered dest: buffered
size: 5000 size: 5100000
- name: Configure logging using aggregate - name: Configure logging using aggregate
iosxr_logging: iosxr_logging:
aggregate: aggregate:
- { dest: console, level: warning } - { dest: console, level: warning }
- { dest: buffered, size: 4800000 } - { dest: buffered, size: 4800000 }
- { dest: file, name: file3, size: 2048}
- { dest: host, name: host3, level: critical}
- name: Delete logging using aggregate - name: Delete logging using aggregate
iosxr_logging: iosxr_logging:
aggregate: aggregate:
- { dest: console, level: warning } - { dest: console, level: warning }
- { dest: buffered, size: 4800000 } - { dest: buffered, size: 4800000 }
- { dest: file, name: file3, size: 2048}
- { dest: host, name: host3, level: critical}
state: absent state: absent
""" """
RETURN = """ RETURN = """
commands: commands:
description: The list of configuration mode commands to send to the device description: The list of configuration mode commands to send to the device
returned: always returned: always (empty list when no commands to send)
type: list type: list
sample: sample:
- logging 10.10.10.1 vrf default severity debugging
- logging facility local7 - logging facility local7
- logging hostnameprefix 172.16.0.1 - logging hostnameprefix host1
- logging console critical
- logging buffered 2097153
- logging buffered warnings
- logging monitor errors
- logging file log_file maxfilesize 1024 severity info
xml:
description: NetConf rpc xml sent to device with transport C(netconf)
returned: always (empty list when no xml rpc to send)
type: list
version_added: 2.5
sample:
- '<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<syslog xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-infra-syslog-cfg">
<files>
<file xc:operation="delete">
<file-name>file1</file-name>
<file-log-attributes>
<max-file-size>2097152</max-file-size>
<severity>2</severity>
</file-log-attributes>
</file>
</files>
</syslog>
</config>'
""" """
import re import re
import collections
from copy import deepcopy from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.iosxr.iosxr import get_config, load_config from ansible.module_utils.network.iosxr.iosxr import get_config, load_config, build_xml
from ansible.module_utils.network.iosxr.iosxr import iosxr_argument_spec from ansible.module_utils.network.iosxr.iosxr import iosxr_argument_spec, etree_findall
from ansible.module_utils.network.iosxr.iosxr import is_netconf, is_cliconf, etree_find
from ansible.module_utils.network.common.utils import remove_default_spec from ansible.module_utils.network.common.utils import remove_default_spec
def validate_size(value, module): severity_level = {'emergency': '0',
if value: 'alert': '1',
if value and not int(307200) <= value <= int(125000000): 'critical': '2',
module.fail_json(msg='size must be between 307200 and 125000000') 'error': '3',
else: 'warning': '4',
return value 'notice': '5',
'info': '6',
'debug': '7',
'disable': '15'}
severity_transpose = {'emergencies': 'emergency',
'alerts': 'alert',
'critical': 'critical',
'errors': 'error',
'warning': 'warning',
'notifications': 'notice',
'informational': 'info',
'debugging': 'debug'}
def map_obj_to_commands(updates, module): class ConfigBase(object):
commands = list() def __init__(self, module):
want, have = updates self._flag = None
for w in want: self._module = module
dest = w['dest'] self._result = {'changed': False, 'warnings': []}
name = w['name'] self._want = list()
size = w['size'] self._have = list()
facility = w['facility']
level = w['level']
state = w['state']
del w['state']
if state == 'absent' and w in have: def validate_size(self, value, type=None):
if dest == 'hostnameprefix': if value:
commands.append('no logging hostnameprefix {}'.format(name)) if type == 'buffer':
elif dest: if value and not int(307200) <= value <= int(125000000):
commands.append('no logging {}'.format(dest)) self._module.fail_json(msg='buffer size must be between 307200 and 125000000')
else: elif type == 'file':
module.fail_json(msg='dest must be among console, monitor, buffered, hostnameprefix, on') if value and not int(1) <= value <= int(2097152):
self._module.fail_json(msg='file size must be between 1 and 2097152')
return value
if facility: def map_params_to_obj(self, required_if=None):
commands.append('no logging facility {}'.format(facility)) aggregate = self._module.params.get('aggregate')
if aggregate:
for item in aggregate:
for key in item:
if item.get(key) is None:
item[key] = self._module.params[key]
if state == 'present' and w not in have: d = item.copy()
if facility:
commands.append('logging facility {}'.format(facility))
if dest == 'hostnameprefix': if d['dest'] not in ('host', 'file'):
commands.append('logging hostnameprefix {}'.format(name)) d['name'] = None
elif dest == 'on': if d['dest'] == 'buffered':
commands.append('logging on') if d['size'] is not None:
d['size'] = str(self.validate_size(d['size'], 'buffer'))
elif dest == 'buffered' and size: else:
commands.append('logging buffered {}'.format(size)) d['size'] = str(307200)
elif d['dest'] == 'file':
else: if d['size'] is not None:
dest_cmd = 'logging {}'.format(dest) d['size'] = str(self.validate_size(d['size'], 'file'))
if level: else:
dest_cmd += ' {}'.format(level) d['size'] = str(2097152)
commands.append(dest_cmd)
return commands
def parse_facility(line):
match = re.search(r'logging facility (\S+)', line, re.M)
if match:
facility = match.group(1)
else:
facility = 'local7'
return facility
def parse_size(line, dest):
size = None
if dest == 'buffered':
match = re.search(r'logging buffered (\S+)', line, re.M)
if match:
try:
int_size = int(match.group(1))
except ValueError:
int_size = None
if int_size:
if isinstance(int_size, int):
size = str(match.group(1))
else: else:
size = str(307200) d['size'] = None
return size if self._flag == 'NC':
d['level'] = severity_transpose[d['level']]
self._want.append(d)
else:
params = self._module.params
if params['dest'] not in ('host', 'file'):
params['name'] = None
if params['dest'] == 'buffered':
if params['size'] is not None:
params['size'] = str(self.validate_size(params['size'], 'buffer'))
else:
params['size'] = str(307200)
elif params['dest'] == 'file':
if params['size'] is not None:
params['size'] = str(self.validate_size(params['size'], 'file'))
else:
params['size'] = str(2097152)
else:
params['size'] = None
if self._flag == 'NC':
params['level'] = severity_transpose[params['level']]
self._want.append({
'dest': params['dest'],
'name': params['name'],
'vrf': params['vrf'],
'size': params['size'],
'facility': params['facility'],
'level': params['level'],
'hostnameprefix': params['hostnameprefix'],
'state': params['state']
})
def parse_name(line, dest): class CliConfiguration(ConfigBase):
if dest == 'hostnameprefix': def __init__(self, module):
super(CliConfiguration, self).__init__(module)
self._file_list = set()
self._host_list = set()
def map_obj_to_commands(self):
commands = list()
for want_item in self._want:
dest = want_item['dest']
name = want_item['name']
size = want_item['size']
facility = want_item['facility']
level = want_item['level']
vrf = want_item['vrf']
hostnameprefix = want_item['hostnameprefix']
state = want_item['state']
del want_item['state']
have_size = None
have_console_level = None
have_monitor_level = None
have_prefix = None
have_facility = None
for item in self._have:
if item['dest'] == 'buffered':
have_size = item['size']
if item['dest'] == 'console':
have_console_level = item['level']
if item['dest'] == 'monitor':
have_monitor_level = item['level']
if item['dest'] is None and item['hostnameprefix'] is not None:
have_prefix = item['hostnameprefix']
if item['dest'] is None and item['hostnameprefix'] is None and item['facility'] is not None:
have_facility = item['facility']
if state == 'absent':
if dest == 'host' and name in self._host_list:
commands.append('no logging {0} vrf {1}'.format(name, vrf))
elif dest == 'file' and name in self._file_list:
commands.append('no logging file {0}'.format(name))
elif dest == 'console' and have_console_level is not None:
commands.append('no logging {0}'.format(dest))
elif dest == 'monitor' and have_monitor_level:
commands.append('no logging {0}'.format(dest))
elif dest == 'buffered' and have_size:
commands.append('no logging {0}'.format(dest))
if dest is None and hostnameprefix is not None and have_prefix == hostnameprefix:
commands.append('no logging hostnameprefix {0}'.format(hostnameprefix))
if dest is None and facility is not None and have_facility == facility:
commands.append('no logging facility {0}'.format(facility))
if state == 'present':
if dest == 'host' and name not in self._host_list:
if level == 'errors' or level == 'informational':
level = severity_transpose[level]
commands.append('logging {0} vrf {1} severity {2}'.format(name, vrf, level))
elif dest == 'file' and name not in self._file_list:
if level == 'errors' or level == 'informational':
level = severity_transpose[level]
commands.append('logging file {0} maxfilesize {1} severity {2}'.format(name, size, level))
elif dest == 'buffered' and (have_size is None or (have_size is not None and size != have_size)):
commands.append('logging buffered {0}'.format(size))
elif dest == 'console' and (have_console_level is None or
(have_console_level is not None and have_console_level != level)):
commands.append('logging console {0}'.format(level))
elif dest == 'monitor' and (have_monitor_level is None or
(have_monitor_level is not None and have_monitor_level != level)):
commands.append('logging monitor {0}'.format(level))
if dest is None and hostnameprefix is not None and (have_prefix is None or
(have_prefix is not None and hostnameprefix != have_prefix)):
commands.append('logging hostnameprefix {0}'.format(hostnameprefix))
if dest is None and hostnameprefix is None and facility != have_facility:
commands.append('logging facility {0}'.format(facility))
self._result['commands'] = commands
if commands:
commit = not self._module.check_mode
diff = load_config(self._module, commands, commit=commit)
if diff:
self._result['diff'] = dict(prepared=diff)
self._result['changed'] = True
def parse_facility(self, line):
match = re.search(r'logging facility (\S+)', line, re.M)
facility = None
if match:
facility = match.group(1)
return facility
def parse_size(self, line, dest):
size = None
if dest == 'buffered':
match = re.search(r'logging buffered (\S+)', line, re.M)
if match:
try:
int_size = int(match.group(1))
except ValueError:
int_size = None
if int_size is not None:
if isinstance(int_size, int):
size = str(match.group(1))
return size
def parse_hostnameprefix(self, line):
prefix = None
match = re.search(r'logging hostnameprefix (\S+)', line, re.M) match = re.search(r'logging hostnameprefix (\S+)', line, re.M)
if match: if match:
name = match.group(1) prefix = match.group(1)
else: return prefix
def parse_name(self, line, dest):
name = None name = None
if dest == 'file':
match = re.search(r'logging file (\S+)', line, re.M)
if match:
name = match.group(1)
elif dest == 'host':
match = re.search(r'logging (\S+)', line, re.M)
if match:
name = match.group(1)
return name return name
def parse_level(self, line, dest):
level_group = ('emergencies', 'alerts', 'critical', 'errors', 'warning',
'notifications', 'informational', 'debugging')
def parse_level(line, dest): level = None
level_group = ('emergencies', 'alerts', 'critical', 'errors', 'warning', match = re.search(r'logging {0} (\S+)'.format(dest), line, re.M)
'notifications', 'informational', 'debugging')
if dest == 'hostnameprefix':
level = 'debugging'
else:
match = re.search(r'logging {} (\S+)'.format(dest), line, re.M)
if match: if match:
if match.group(1) in level_group: if match.group(1) in level_group:
level = match.group(1) level = match.group(1)
else:
level = 'debugging'
else:
level = 'debugging'
return level return level
def parse_dest(self, line, group):
dest_group = ('console', 'monitor', 'buffered', 'file')
dest = None
if group in dest_group:
dest = group
elif 'vrf' in line:
dest = 'host'
def map_config_to_obj(module): return dest
obj = [] def parse_vrf(self, line, dest):
dest_group = ('console', 'hostnameprefix', 'monitor', 'buffered', 'on') vrf = None
if dest == 'host':
match = re.search(r'logging (\S+) vrf (\S+)', line, re.M)
if match:
vrf = match.group(2)
return vrf
data = get_config(module, config_filter='logging') def map_config_to_obj(self):
lines = data.split("\n") data = get_config(self._module, config_filter='logging')
lines = data.split("\n")
for line in lines: for line in lines:
match = re.search(r'logging (\S+)', line, re.M) match = re.search(r'logging (\S+)', line, re.M)
if match: if match:
if match.group(1) in dest_group: dest = self.parse_dest(line, match.group(1))
dest = match.group(1) name = self.parse_name(line, dest)
obj.append({ if dest == 'host' and name is not None:
self._host_list.add(name)
if dest == 'file' and name is not None:
self._file_list.add(name)
self._have.append({
'dest': dest, 'dest': dest,
'name': parse_name(line, dest), 'name': name,
'size': parse_size(line, dest), 'size': self.parse_size(line, dest),
'facility': parse_facility(line), 'facility': self.parse_facility(line),
'level': parse_level(line, dest) 'level': self.parse_level(line, dest),
'vrf': self.parse_vrf(line, dest),
'hostnameprefix': self.parse_hostnameprefix(line),
}) })
return obj def run(self):
self.map_params_to_obj()
self.map_config_to_obj()
self.map_obj_to_commands()
return self._result
def map_params_to_obj(module, required_if=None): class NCConfiguration(ConfigBase):
obj = [] def __init__(self, module):
super(NCConfiguration, self).__init__(module)
self._flag = 'NC'
self._log_file_meta = collections.OrderedDict()
self._log_host_meta = collections.OrderedDict()
self._log_console_meta = collections.OrderedDict()
self._log_monitor_meta = collections.OrderedDict()
self._log_buffered_size_meta = collections.OrderedDict()
self._log_buffered_level_meta = collections.OrderedDict()
self._log_facility_meta = collections.OrderedDict()
self._log_prefix_meta = collections.OrderedDict()
aggregate = module.params.get('aggregate') def map_obj_to_xml_rpc(self):
if aggregate: self._log_file_meta.update([
for item in aggregate: ('files', {'xpath': 'syslog/files', 'tag': True, 'operation': 'edit'}),
for key in item: ('file', {'xpath': 'syslog/files/file', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
if item.get(key) is None: ('a:name', {'xpath': 'syslog/files/file/file-name', 'operation': 'edit'}),
item[key] = module.params[key] ('file-attrib', {'xpath': 'syslog/files/file/file-log-attributes', 'tag': True, 'operation': 'edit'}),
('a:size', {'xpath': 'syslog/files/file/file-log-attributes/max-file-size', 'operation': 'edit'}),
('a:level', {'xpath': 'syslog/files/file/file-log-attributes/severity', 'operation': 'edit'}),
])
self._log_host_meta.update([
('host-server', {'xpath': 'syslog/host-server', 'tag': True, 'operation': 'edit'}),
('vrfs', {'xpath': 'syslog/host-server/vrfs', 'tag': True, 'operation': 'edit'}),
('vrf', {'xpath': 'syslog/host-server/vrfs/vrf', 'tag': True, 'operation': 'edit'}),
('a:vrf', {'xpath': 'syslog/host-server/vrfs/vrf/vrf-name', 'operation': 'edit'}),
('ipv4s', {'xpath': 'syslog/host-server/vrfs/vrf/ipv4s', 'tag': True, 'operation': 'edit'}),
('ipv4', {'xpath': 'syslog/host-server/vrfs/vrf/ipv4s/ipv4', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
('a:name', {'xpath': 'syslog/host-server/vrfs/vrf/ipv4s/ipv4/address', 'operation': 'edit'}),
('ipv4-sev', {'xpath': 'syslog/host-server/vrfs/vrf/ipv4s/ipv4/ipv4-severity-port', 'tag': True, 'operation': 'edit'}),
('a:level', {'xpath': 'syslog/host-server/vrfs/vrf/ipv4s/ipv4/ipv4-severity-port/severity', 'operation': 'edit'}),
])
self._log_console_meta.update([
('a:enable-console', {'xpath': 'syslog/enable-console-logging', 'operation': 'edit', 'attrib': "operation"}),
('console', {'xpath': 'syslog/console-logging', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
('a:console-level', {'xpath': 'syslog/console-logging/logging-level', 'operation': 'edit'}),
])
self._log_monitor_meta.update([
('monitor', {'xpath': 'syslog/monitor-logging', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
('a:monitor-level', {'xpath': 'syslog/monitor-logging/logging-level', 'operation': 'edit'}),
])
self._log_buffered_size_meta.update([
('buffered', {'xpath': 'syslog/buffered-logging', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
('a:size', {'xpath': 'syslog/buffered-logging/buffer-size', 'operation': 'edit'}),
])
self._log_buffered_level_meta.update([
('buffered', {'xpath': 'syslog/buffered-logging', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
('a:level', {'xpath': 'syslog/buffered-logging/logging-level', 'operation': 'edit'}),
])
self._log_facility_meta.update([
('facility', {'xpath': 'syslog/logging-facilities', 'tag': True, 'operation': 'edit', 'attrib': "operation"}),
('a:facility', {'xpath': 'syslog/logging-facilities/facility-level', 'operation': 'edit'}),
])
self._log_prefix_meta.update([
('a:hostnameprefix', {'xpath': 'syslog/host-name-prefix', 'operation': 'edit', 'attrib': "operation"}),
])
module._check_required_if(required_if, item) state = self._module.params['state']
d = item.copy()
if d['dest'] != 'hostnameprefix': _get_filter = build_xml('syslog', opcode="filter")
d['name'] = None running = get_config(self._module, source='running', config_filter=_get_filter)
if d['dest'] == 'buffered': file_ele = etree_findall(running, 'file')
if 'size' in d: file_list = list()
d['size'] = str(validate_size(d['size'], module)) if len(file_ele):
elif 'size' not in d: for file in file_ele:
d['size'] = str(307200) file_name = etree_find(file, 'file-name')
else: file_list.append(file_name.text if file_name is not None else None)
pass vrf_ele = etree_findall(running, 'vrf')
host_list = list()
for vrf in vrf_ele:
host_ele = etree_findall(vrf, 'ipv4')
for host in host_ele:
host_name = etree_find(host, 'address')
host_list.append(host_name.text if host_name is not None else None)
if d['dest'] != 'buffered': console_ele = etree_find(running, 'console-logging')
d['size'] = None console_level = etree_find(console_ele, 'logging-level') if console_ele is not None else None
have_console = console_level.text if console_level is not None else None
obj.append(d) monitor_ele = etree_find(running, 'monitor-logging')
monitor_level = etree_find(monitor_ele, 'logging-level') if monitor_ele is not None else None
have_monitor = monitor_level.text if monitor_level is not None else None
else: buffered_ele = etree_find(running, 'buffered-logging')
if module.params['dest'] != 'hostnameprefix': buffered_size = etree_find(buffered_ele, 'buffer-size') if buffered_ele is not None else None
module.params['name'] = None have_buffered = buffered_size.text if buffered_size is not None else None
if module.params['dest'] == 'buffered': facility_ele = etree_find(running, 'logging-facilities')
if not module.params['size']: facility_level = etree_find(facility_ele, 'facility-level') if facility_ele is not None else None
module.params['size'] = str(307200) have_facility = facility_level.text if facility_level is not None else None
else:
module.params['size'] = None
if module.params['size'] is None: prefix_ele = etree_find(running, 'host-name-prefix')
obj.append({ have_prefix = prefix_ele.text if prefix_ele is not None else None
'dest': module.params['dest'],
'name': module.params['name'],
'size': module.params['size'],
'facility': module.params['facility'],
'level': module.params['level'],
'state': module.params['state']
})
else: console_enable_ele = etree_find(running, 'enable-console-logging')
obj.append({ have_console_enable = console_enable_ele.text if console_enable_ele is not None else None
'dest': module.params['dest'],
'name': module.params['name'],
'size': str(validate_size(module.params['size'], module)),
'facility': module.params['facility'],
'level': module.params['level'],
'state': module.params['state']
})
return obj file_params = list()
host_params = list()
console_params = dict()
monitor_params = dict()
buffered_params = dict()
facility_params = dict()
prefix_params = dict()
opcode = None
if state == 'absent':
opcode = "delete"
for item in self._want:
if item['dest'] == 'file' and item['name'] in file_list:
item['level'] = severity_level[item['level']]
file_params.append(item)
elif item['dest'] == 'host' and item['name'] in host_list:
item['level'] = severity_level[item['level']]
host_params.append(item)
elif item['dest'] == 'console' and have_console and have_console_enable:
console_params.update({'console-level': item['level']})
elif item['dest'] == 'monitor' and have_monitor:
monitor_params.update({'monitor-level': item['level']})
elif item['dest'] == 'buffered' and have_buffered:
buffered_params['size'] = str(item['size']) if item['size'] else None
buffered_params['level'] = item['level'] if item['level'] else None
elif item['dest'] is None and item['hostnameprefix'] is None and \
item['facility'] is not None and have_facility:
facility_params.update({'facility': item['facility']})
elif item['dest'] is None and item['hostnameprefix'] is not None and have_prefix:
prefix_params.update({'hostnameprefix': item['hostnameprefix']})
elif state == 'present':
opcode = 'merge'
for item in self._want:
if item['dest'] == 'file':
item['level'] = severity_level[item['level']]
file_params.append(item)
elif item['dest'] == 'host':
item['level'] = severity_level[item['level']]
host_params.append(item)
elif item['dest'] == 'console':
console_params.update({'console-level': item['level']})
elif item['dest'] == 'monitor':
monitor_params.update({'monitor-level': item['level']})
elif item['dest'] == 'buffered':
buffered_params['size'] = str(item['size']) if item['size'] else None
buffered_params['level'] = item['level'] if item['level'] else None
elif item['dest'] is None and item['hostnameprefix'] is None and \
item['facility'] is not None:
facility_params.update({'facility': item['facility']})
elif item['dest'] is None and item['hostnameprefix'] is not None:
prefix_params.update({'hostnameprefix': item['hostnameprefix']})
self._result['xml'] = []
_edit_filter_list = list()
if opcode:
if len(file_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_file_meta,
params=file_params, opcode=opcode))
if len(host_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_host_meta,
params=host_params, opcode=opcode))
if len(console_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_console_meta,
params=console_params, opcode=opcode))
if len(monitor_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_monitor_meta,
params=monitor_params, opcode=opcode))
if len(buffered_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_buffered_size_meta,
params=buffered_params, opcode=opcode))
_edit_filter_list.append(build_xml('syslog', xmap=self._log_buffered_level_meta,
params=buffered_params, opcode=opcode))
if len(facility_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_facility_meta,
params=facility_params, opcode=opcode))
if len(prefix_params):
_edit_filter_list.append(build_xml('syslog', xmap=self._log_prefix_meta,
params=prefix_params, opcode=opcode))
diff = None
if len(_edit_filter_list):
commit = not self._module.check_mode
diff = load_config(self._module, _edit_filter_list, commit=commit, running=running,
nc_get_filter=_get_filter)
if diff:
if self._module._diff:
self._result['diff'] = dict(prepared=diff)
self._result['xml'] = _edit_filter_list
self._result['changed'] = True
def run(self):
self.map_params_to_obj()
self.map_obj_to_xml_rpc()
return self._result
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
element_spec = dict( element_spec = dict(
dest=dict(type='str', choices=['on', 'hostnameprefix', 'console', 'monitor', 'buffered']), dest=dict(type='str', choices=['host', 'console', 'monitor', 'buffered', 'file']),
name=dict(type='str'), name=dict(type='str'),
size=dict(type='int'), size=dict(type='int'),
vrf=dict(type='str', default='default'),
facility=dict(type='str', default='local7'), facility=dict(type='str', default='local7'),
level=dict(type='str', default='debugging'), hostnameprefix=dict(type='str'),
level=dict(type='str', default='informational', aliases=['severity'],
choices=['emergencies', 'alerts', 'critical', 'errors', 'warning',
'notifications', 'informational', 'debugging']),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),
) )
@ -336,37 +687,37 @@ def main():
# remove default in aggregate spec, to handle common arguments # remove default in aggregate spec, to handle common arguments
remove_default_spec(aggregate_spec) remove_default_spec(aggregate_spec)
mutually_exclusive = [('dest', 'facility', 'hostnameprefix')]
required_if = [('dest', 'host', ['name']),
('dest', 'file', ['name']),
('dest', 'buffered', ['size']),
('dest', 'console', ['level']),
('dest', 'monitor', ['level'])]
argument_spec = dict( argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec), aggregate=dict(type='list', elements='dict', options=aggregate_spec,
mutually_exclusive=mutually_exclusive, required_if=required_if),
) )
argument_spec.update(element_spec) argument_spec.update(element_spec)
argument_spec.update(iosxr_argument_spec) argument_spec.update(iosxr_argument_spec)
required_if = [('dest', 'hostnameprefix', ['name'])]
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
required_if=required_if, required_if=required_if,
supports_check_mode=True) supports_check_mode=True)
warnings = list() config_object = None
if is_cliconf(module):
result = {'changed': False} module.deprecate(msg="cli support for 'iosxr_logging' is deprecated. Use transport netconf instead",
version="4 releases from v2.5")
want = map_params_to_obj(module, required_if=required_if) config_object = CliConfiguration(module)
have = map_config_to_obj(module) elif is_netconf(module):
commands = map_obj_to_commands((want, have), module) config_object = NCConfiguration(module)
result['commands'] = commands
result['warnings'] = warnings
if commands:
commit = not module.check_mode
diff = load_config(module, commands, commit=commit)
if diff:
result['diff'] = dict(prepared=diff)
result['changed'] = True
if config_object:
result = config_object.run()
module.exit_json(**result) module.exit_json(**result)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -1,2 +1,3 @@
--- ---
- { include: cli.yaml, tags: ['cli'] } - { include: cli.yaml, tags: ['cli'] }
- { include: netconf.yaml, tags: ['netconf'] }

View file

@ -0,0 +1,22 @@
---
- name: collect all netconf test cases
find:
paths: "{{ role_path }}/tests/netconf"
patterns: "{{ testcase }}.yaml"
register: test_cases
delegate_to: localhost
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case (connection=netconf)
include: "{{ test_case_to_run }} ansible_connection=netconf"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
- name: run test case (connection=local)
include: "{{ test_case_to_run }} ansible_connection=local"
with_first_found: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

View file

@ -1,33 +1,33 @@
--- ---
# Remove old logging entries so that they don't conflict with tests # Remove old logging entries so that they don't conflict with tests
- name: Remove host logging - name: remove host logging
iosxr_logging: iosxr_logging:
dest: hostnameprefix dest: host
name: 172.16.0.1 name: 172.16.0.1
state: absent state: absent
provider: "{{ cli }}" provider: "{{ cli }}"
- name: Remove console logging - name: remove console logging
iosxr_logging: iosxr_logging:
dest: console dest: console
level: warning
state: absent state: absent
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
- name: Remove buffer - name: remove buffered logging
iosxr_logging: iosxr_logging:
dest: buffered dest: buffered
size: 4800000 size: 2097155
state: absent state: absent
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
# Start tests # Start tests
- name: Set up host logging - name: set up syslog host logging
iosxr_logging: iosxr_logging: &addhostlog
dest: hostnameprefix dest: host
name: 172.16.0.1 name: 172.16.0.1
level: errors
state: present state: present
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
@ -35,24 +35,19 @@
- assert: - assert:
that: that:
- 'result.changed == true' - 'result.changed == true'
- '"logging hostnameprefix 172.16.0.1" in result.commands' - '"logging 172.16.0.1 vrf default severity error" in result.commands'
- '"logging facility local7" in result.commands'
- name: Set up host logging again (idempotent) - name: set up syslog host logging (idempotent)
iosxr_logging: iosxr_logging: *addhostlog
dest: hostnameprefix
name: 172.16.0.1
state: present
provider: "{{ cli }}"
register: result register: result
- assert: - assert: &false
that: that:
- 'result.changed == false' - 'result.changed == false'
- name: Delete/disable host logging - name: delete/disable syslog host logging
iosxr_logging: iosxr_logging: &delhostlog
dest: hostnameprefix dest: host
name: 172.16.0.1 name: 172.16.0.1
state: absent state: absent
provider: "{{ cli }}" provider: "{{ cli }}"
@ -61,22 +56,16 @@
- assert: - assert:
that: that:
- 'result.changed == true' - 'result.changed == true'
- '"no logging hostnameprefix 172.16.0.1" in result.commands' - '"no logging 172.16.0.1 vrf default" in result.commands'
- name: Delete/disable host logging (idempotent) - name: delete/disable syslog host logging (idempotent)
iosxr_logging: iosxr_logging: *delhostlog
dest: hostnameprefix
name: 172.16.0.1
state: absent
provider: "{{ cli }}"
register: result register: result
- assert: - assert: *false
that:
- 'result.changed == false'
- name: Console logging with level warning - name: add console logging with level warning
iosxr_logging: iosxr_logging: &consolelog
dest: console dest: console
level: warning level: warning
state: present state: present
@ -88,10 +77,29 @@
- 'result.changed == true' - 'result.changed == true'
- '"logging console warning" in result.commands' - '"logging console warning" in result.commands'
- name: Configure Buffer size - name: console logging with level warning (idempotent)
iosxr_logging: iosxr_logging: *consolelog
register: result
- assert: *false
- name: remove console logging with level warning
iosxr_logging:
dest: console
level: warning
state: absent
provider: "{{ cli }}"
register: result
- assert: &true
that:
- 'result.changed == true'
- name: configure buffered logging size
iosxr_logging: &bufferlog
dest: buffered dest: buffered
size: 4800000 size: 4800000
state: present
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
@ -100,11 +108,28 @@
- 'result.changed == true' - 'result.changed == true'
- '"logging buffered 4800000" in result.commands' - '"logging buffered 4800000" in result.commands'
- name: Change logging parameters using aggregate - name: configure buffered logging size (idempotence)
iosxr_logging: *bufferlog
register: result
- assert: *false
- name: remove buffered logging size
iosxr_logging:
dest: buffered
size: 4800000
state: absent
provider: "{{ cli }}"
register: result
- assert: *true
- name: change logging parameters using aggregate
iosxr_logging: iosxr_logging:
aggregate: aggregate:
- { dest: console, level: notifications } - { dest: console, level: notifications }
- { dest: buffered, size: 4700000 } - { dest: buffered, size: 4700000 }
state: present
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
@ -114,7 +139,7 @@
- '"logging buffered 4700000" in result.commands' - '"logging buffered 4700000" in result.commands'
- '"logging console notifications" in result.commands' - '"logging console notifications" in result.commands'
- name: remove logging as collection tearDown - name: remove logging parameters using aggregate
iosxr_logging: iosxr_logging:
aggregate: aggregate:
- { dest: console, level: notifications } - { dest: console, level: notifications }

View file

@ -0,0 +1,174 @@
---
# Remove old logging entries so that they don't conflict with tests
- name: remove host logging
iosxr_logging:
dest: host
name: 172.16.0.1
state: absent
provider: "{{ netconf }}"
- name: remove console logging
iosxr_logging:
dest: console
state: absent
provider: "{{ netconf }}"
register: result
- name: remove buffered logging
iosxr_logging:
dest: buffered
size: 2097155
state: absent
provider: "{{ netconf }}"
register: result
# Start tests
- name: set up syslog host logging
iosxr_logging: &addhostlog
dest: host
name: 172.16.0.1
level: errors
state: present
provider: "{{ netconf }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"172.16.0.1" in result.xml[0]'
- name: set up syslog host logging (idempotent)
iosxr_logging: *addhostlog
register: result
- assert: &false
that:
- 'result.changed == false'
- name: delete/disable syslog host logging
iosxr_logging: &delhostlog
dest: host
name: 172.16.0.1
state: absent
provider: "{{ netconf }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"172.16.0.1" in result.xml[0]'
- '"delete" in result.xml[0]'
- name: delete/disable syslog host logging (idempotent)
iosxr_logging: *delhostlog
register: result
- assert: *false
- name: add console logging with level warning
iosxr_logging: &consolelog
dest: console
level: warning
state: present
provider: "{{ netconf }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"console" in result.xml[0]'
- '"warning" in result.xml[0]'
- name: console logging with level warning (idempotent)
iosxr_logging: *consolelog
register: result
- assert: *false
- name: remove console logging with level warning
iosxr_logging:
dest: console
level: warning
state: absent
provider: "{{ netconf }}"
register: result
- assert: &true
that:
- 'result.changed == true'
- name: configure buffered logging size
iosxr_logging: &bufferlog
dest: buffered
size: 4800000
state: present
provider: "{{ netconf }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"buffered" in result.xml[0]'
- '"4800000" in result.xml[0]'
- name: configure buffered logging size (idempotence)
iosxr_logging: *bufferlog
register: result
- assert: *false
- name: remove buffered logging size
iosxr_logging:
dest: buffered
size: 4800000
state: absent
provider: "{{ netconf }}"
register: result
- assert: *true
- name: change logging parameters using aggregate
iosxr_logging:
aggregate:
- { dest: console, level: notifications }
- { dest: buffered, size: 4700000 }
- { dest: monitor, level: alerts }
- { dest: host, name: 10.10.10.1, level: errors }
- { dest: host, name: 10.10.10.2 }
- { dest: file, name: file1, size: 2048, level: critical}
- { dest: file, name: file2, size: 2048 }
- { facility: local3 }
- { hostnameprefix: host3 }
state: present
provider: "{{ netconf }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"file1" in result.xml[0]'
- '"file2" in result.xml[0]'
- '"10.10.10.1" in result.xml[1]'
- '"10.10.10.2" in result.xml[1]'
- '"notice" in result.xml[2]'
- '"alert" in result.xml[3]'
- '"4700000" in result.xml[4]'
- '"info" in result.xml[5]'
- '"local3" in result.xml[6]'
- '"host3" in result.xml[7]'
- name: remove logging parameters using aggregate
iosxr_logging:
aggregate:
- { dest: console, level: notifications }
- { dest: buffered, size: 4700000 }
state: absent
provider: "{{ netconf }}"
register: result
- assert:
that:
- 'result.changed == true'
- '"console" in result.xml[0]'
- '"buffered" in result.xml[1]'
- '"buffered" in result.xml[2]'

View file

@ -82,7 +82,6 @@ lib/ansible/modules/network/ios/ios_l3_interface.py ansible-format-automatic-spe
lib/ansible/modules/network/ios/ios_vlan.py ansible-format-automatic-specification lib/ansible/modules/network/ios/ios_vlan.py ansible-format-automatic-specification
lib/ansible/modules/network/iosxr/iosxr_banner.py ansible-format-automatic-specification lib/ansible/modules/network/iosxr/iosxr_banner.py ansible-format-automatic-specification
lib/ansible/modules/network/iosxr/iosxr_interface.py ansible-format-automatic-specification lib/ansible/modules/network/iosxr/iosxr_interface.py ansible-format-automatic-specification
lib/ansible/modules/network/iosxr/iosxr_logging.py ansible-format-automatic-specification
lib/ansible/modules/network/nxos/nxos_logging.py ansible-format-automatic-specification lib/ansible/modules/network/nxos/nxos_logging.py ansible-format-automatic-specification
lib/ansible/modules/network/nxos/nxos_snapshot.py ansible-format-automatic-specification lib/ansible/modules/network/nxos/nxos_snapshot.py ansible-format-automatic-specification
lib/ansible/modules/network/vyos/vyos_vlan.py ansible-format-automatic-specification lib/ansible/modules/network/vyos/vyos_vlan.py ansible-format-automatic-specification