updated single quote handling to not strip and error on odd number (#67500)
Fixes #67274
This commit is contained in:
parent
eab914426b
commit
bd26b6c0b4
6 changed files with 42 additions and 10 deletions
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- edgeos_config - fixed issue of handling single quotation marks. Now fails when unmatched (odd numbers)
|
|
@ -186,15 +186,34 @@ def get_candidate(module):
|
|||
return config_to_commands(contents)
|
||||
|
||||
|
||||
def diff_config(commands, config):
|
||||
config = [to_native(c).replace("'", '') for c in config.splitlines()]
|
||||
def check_command(module, command):
|
||||
"""Tests against a command line to be valid otherwise raise errors
|
||||
|
||||
Error on uneven single quote which breaks ansible waiting for further input. Ansible
|
||||
will handle even single quote failures correctly.
|
||||
|
||||
:param command: the command line from current or new config
|
||||
:type command: string
|
||||
:raises ValueError:
|
||||
* if contains odd number of single quotes
|
||||
:return: command string unchanged
|
||||
:rtype: string
|
||||
"""
|
||||
if command.count("'") % 2 != 0:
|
||||
module.fail_json(msg="Unmatched single (') quote found in command: " + command)
|
||||
|
||||
return command
|
||||
|
||||
|
||||
def diff_config(module, commands, config):
|
||||
config = [to_native(check_command(module, c)) for c in config.splitlines()]
|
||||
|
||||
updates = list()
|
||||
visited = set()
|
||||
delete_commands = [line for line in commands if line.startswith('delete')]
|
||||
|
||||
for line in commands:
|
||||
item = to_native(line).replace("'", '')
|
||||
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`')
|
||||
|
@ -234,7 +253,7 @@ def run(module, result):
|
|||
candidate = get_candidate(module)
|
||||
|
||||
# create loadable config that includes only the configuration updates
|
||||
commands = diff_config(candidate, config)
|
||||
commands = diff_config(module, candidate, config)
|
||||
|
||||
result['commands'] = commands
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
set system host-name 'router'
|
||||
set system domain-name 'acme.com'
|
||||
set system domain-search domain 'acme.com'
|
||||
set system name-server '208.67.220.220'
|
||||
set system name-server '208.67.222.222'
|
||||
set interfaces ethernet eth0 address '1.2.3.4/24'
|
||||
set system name-server 208.67.220.220
|
||||
set system name-server 208.67.222.222
|
||||
set interfaces ethernet eth0 address 1.2.3.4/24
|
||||
set interfaces ethernet eth0 description 'Outside'
|
||||
set interfaces ethernet eth1 address '10.77.88.1/24'
|
||||
set interfaces ethernet eth1 address 10.77.88.1/24
|
||||
set interfaces ethernet eth1 description 'Inside'
|
||||
set interfaces ethernet eth1 disable
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
set system host-name er01
|
||||
delete interfaces ethernet eth0 address
|
||||
set interfaces ethernet eth1 address '10.77.88.1/24'
|
||||
set interfaces ethernet eth1 address 10.77.88.1/24
|
||||
set interfaces ethernet eth1 description 'Inside'
|
||||
set interfaces ethernet eth1 disable
|
||||
|
|
|
@ -4,7 +4,7 @@ interfaces {
|
|||
}
|
||||
ethernet eth1 {
|
||||
address 10.77.88.1/24
|
||||
description Inside
|
||||
description 'Inside'
|
||||
disable
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,3 +92,14 @@ class TestEdgeosConfigModule(TestEdgeosModule):
|
|||
'set system interfaces ethernet eth0 description Outside']
|
||||
set_module_args(dict(lines=lines, match='none'))
|
||||
self.execute_module(changed=True, commands=lines, sort=False)
|
||||
|
||||
def test_edgeos_config_single_quote_wrapped_values(self):
|
||||
lines = ["set system interfaces ethernet eth0 description 'tests single quotes'"]
|
||||
set_module_args(dict(lines=lines))
|
||||
commands = ["set system interfaces ethernet eth0 description 'tests single quotes'"]
|
||||
self.execute_module(changed=True, commands=commands)
|
||||
|
||||
def test_edgeos_config_single_quote_wrapped_values_failure(self):
|
||||
lines = ["set system interfaces ethernet eth0 description 'test's single quotes'"]
|
||||
set_module_args(dict(lines=lines))
|
||||
self.execute_module(failed=True)
|
||||
|
|
Loading…
Reference in a new issue