From 4130c914c4bf7d449f010c8bd23e5573713a3423 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Litza Date: Thu, 4 Feb 2021 20:43:20 +0100 Subject: [PATCH] Small edgeos_config improvements (#199) * edgeos_config: Match space after set/delete commands This makes matching the commands a bit more robust and also removes magic strings from the code by centralizing them in variables. * edgeos_config: Use simple string replacements instead of re * edgeos_config: Simplify logical flow We do not need to check for each .startswith twice, we can simply use an else: block * Update changelogs/fragments/199-edgeos-improvements.yaml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- .../fragments/199-edgeos-improvements.yaml | 3 +++ .../modules/network/edgeos/edgeos_config.py | 24 +++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/199-edgeos-improvements.yaml diff --git a/changelogs/fragments/199-edgeos-improvements.yaml b/changelogs/fragments/199-edgeos-improvements.yaml new file mode 100644 index 00000000..dfe7b5b2 --- /dev/null +++ b/changelogs/fragments/199-edgeos-improvements.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: +- edgeos_config - match the space after ``set`` and ``delete`` commands (https://github.com/ansible-collections/community.network/pull/199). diff --git a/plugins/modules/network/edgeos/edgeos_config.py b/plugins/modules/network/edgeos/edgeos_config.py index cc7899df..a7cc4de5 100644 --- a/plugins/modules/network/edgeos/edgeos_config.py +++ b/plugins/modules/network/edgeos/edgeos_config.py @@ -138,8 +138,6 @@ sample: /playbooks/ansible/backup/edgeos_config.2016-07-16@22:28:34 """ -import re - from ansible.module_utils._text import to_native from ansible.module_utils.basic import AnsibleModule from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import NetworkConfig @@ -147,10 +145,12 @@ DEFAULT_COMMENT = 'configured by edgeos_config' +SET_CMD = 'set ' +DELETE_CMD = 'delete ' def config_to_commands(config): - set_format = config.startswith('set') or config.startswith('delete') + set_format = config.startswith(SET_CMD) or config.startswith(DELETE_CMD) candidate = NetworkConfig(indent=4, contents=config) if not set_format: candidate = [c.line for c in candidate.items] @@ -163,7 +163,7 @@ def config_to_commands(config): break commands.append(item) - commands = ['set %s' % cmd.replace(' {', '') for cmd in commands] + commands = [SET_CMD + cmd.replace(' {', '') for cmd in commands] else: commands = to_native(candidate).split('\n') @@ -204,15 +204,12 @@ def diff_config(module, commands, config): updates = list() visited = set() - delete_commands = [line for line in commands if line.startswith('delete')] + delete_commands = [line for line in commands if line.startswith(DELETE_CMD)] for line in commands: item = to_native(check_command(module, line)) - if not item.startswith('set') and not item.startswith('delete'): - raise ValueError('line must start with either `set` or `delete`') - - elif item.startswith('set'): + if item.startswith(SET_CMD): if item not in config: updates.append(line) @@ -220,21 +217,24 @@ def diff_config(module, commands, config): # If there is a corresponding delete command in the desired config, make sure to append # the set command even though it already exists in the running config else: - ditem = re.sub('set', 'delete', item) + ditem = item.replace(SET_CMD, DELETE_CMD, 1) for line in delete_commands: if ditem.startswith(line): updates.append(item) - elif item.startswith('delete'): + elif item.startswith(DELETE_CMD): if not config: updates.append(line) else: - item = re.sub(r'delete', 'set', item) + item = item.replace(DELETE_CMD, SET_CMD, 1) for entry in config: if entry.startswith(item) and line not in visited: updates.append(line) visited.add(line) + else: + raise ValueError('line must start with either `set` or `delete`') + return list(updates)