Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small edgeos_config improvements #199

Merged
merged 4 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/199-edgeos-improvements.yaml
Original file line number Diff line number Diff line change
@@ -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).
24 changes: 12 additions & 12 deletions plugins/modules/network/edgeos/edgeos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@
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
from ansible_collections.community.network.plugins.module_utils.network.edgeos.edgeos import load_config, get_config, run_commands


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]
Expand All @@ -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')
Expand Down Expand Up @@ -204,37 +204,37 @@ 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)

# 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)


Expand Down