From fd4fdbf01f92fc68fed68502d3e84b8d8260429c Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Wed, 24 Jan 2018 10:39:31 +0530 Subject: [PATCH] ios_logging handling platform difference (#35232) * ios_logging handling platform difference Signed-off-by: Trishna Guha * fix unit test Signed-off-by: Trishna Guha --- .../modules/network/ios/ios_logging.py | 39 ++++++++++++++----- .../modules/network/ios/test_ios_logging.py | 5 +++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/network/ios/ios_logging.py b/lib/ansible/modules/network/ios/ios_logging.py index 7e3bc5654e2..74a3bc193b8 100644 --- a/lib/ansible/modules/network/ios/ios_logging.py +++ b/lib/ansible/modules/network/ios/ios_logging.py @@ -123,8 +123,9 @@ import re from copy import deepcopy from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.network.common.utils import remove_default_spec +from ansible.module_utils.network.common.utils import remove_default_spec, validate_ip_address from ansible.module_utils.network.ios.ios import get_config, load_config +from ansible.module_utils.network.ios.ios import get_capabilities from ansible.module_utils.network.ios.ios import ios_argument_spec, check_args @@ -136,7 +137,7 @@ def validate_size(value, module): return value -def map_obj_to_commands(updates, module): +def map_obj_to_commands(updates, module, os_version): commands = list() want, have = updates for w in want: @@ -150,7 +151,10 @@ def map_obj_to_commands(updates, module): if state == 'absent' and w in have: if dest == 'host': - commands.append('no logging host {0}'.format(name)) + if '12.' in os_version: + commands.append('no logging {0}'.format(name)) + else: + commands.append('no logging host {0}'.format(name)) elif dest: commands.append('no logging {0}'.format(dest)) else: @@ -164,7 +168,10 @@ def map_obj_to_commands(updates, module): commands.append('logging facility {0}'.format(facility)) if dest == 'host': - commands.append('logging host {0}'.format(name)) + if '12.' in os_version: + commands.append('logging {0}'.format(name)) + else: + commands.append('logging host {0}'.format(name)) elif dest == 'on': commands.append('logging on') @@ -176,11 +183,11 @@ def map_obj_to_commands(updates, module): commands.append('logging buffered {0}'.format(size)) else: - dest_cmd = 'logging {0}'.format(dest) - if level: - dest_cmd += ' {0}'.format(level) - - commands.append(dest_cmd) + if dest: + dest_cmd = 'logging {0}'.format(dest) + if level: + dest_cmd += ' {0}'.format(level) + commands.append(dest_cmd) return commands @@ -268,6 +275,14 @@ def map_config_to_obj(module): 'facility': parse_facility(line, dest), 'level': parse_level(line, dest) }) + elif validate_ip_address(match.group(1)): + dest = 'host' + obj.append({ + 'dest': dest, + 'name': match.group(1), + 'facility': parse_facility(line, dest), + 'level': parse_level(line, dest) + }) else: ip_match = re.search(r'\d+\.\d+\.\d+\.\d+', match.group(1), re.M) if ip_match: @@ -278,6 +293,7 @@ def map_config_to_obj(module): 'facility': parse_facility(line, dest), 'level': parse_level(line, dest) }) + return obj @@ -373,6 +389,9 @@ def main(): required_if=required_if, supports_check_mode=True) + device_info = get_capabilities(module) + os_version = device_info['device_info']['network_os_version'] + warnings = list() check_args(module, warnings) @@ -383,7 +402,7 @@ def main(): want = map_params_to_obj(module, required_if=required_if) have = map_config_to_obj(module) - commands = map_obj_to_commands((want, have), module) + commands = map_obj_to_commands((want, have), module, os_version) result['commands'] = commands if commands: diff --git a/test/units/modules/network/ios/test_ios_logging.py b/test/units/modules/network/ios/test_ios_logging.py index 91c0006798e..681b03fbed4 100644 --- a/test/units/modules/network/ios/test_ios_logging.py +++ b/test/units/modules/network/ios/test_ios_logging.py @@ -42,11 +42,16 @@ class TestIosLoggingModule(TestIosModule): self.mock_load_config = patch('ansible.modules.network.ios.ios_logging.load_config') self.load_config = self.mock_load_config.start() + self.mock_get_capabilities = patch('ansible.modules.network.ios.ios_logging.get_capabilities') + self.get_capabilities = self.mock_get_capabilities.start() + self.get_capabilities.return_value = {'device_info': {'network_os_version': '15.6(2)T'}} + def tearDown(self): super(TestIosLoggingModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() + self.mock_get_capabilities.stop() def load_fixtures(self, commands=None): self.get_config.return_value = load_fixture('ios_logging_config.cfg')