|
|
@ -154,8 +154,6 @@ responses:
|
|
|
|
type: list
|
|
|
|
type: list
|
|
|
|
sample: ['...', '...']
|
|
|
|
sample: ['...', '...']
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_config(module):
|
|
|
|
def get_config(module):
|
|
|
|
config = module.params['config'] or dict()
|
|
|
|
config = module.params['config'] or dict()
|
|
|
@ -164,34 +162,6 @@ def get_config(module):
|
|
|
|
return config
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_candidate(lines, parents, config, strategy):
|
|
|
|
|
|
|
|
candidate = list()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if strategy == 'strict':
|
|
|
|
|
|
|
|
for index, cmd in enumerate(lines):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
if cmd != config[index]:
|
|
|
|
|
|
|
|
candidate.append(cmd)
|
|
|
|
|
|
|
|
except IndexError:
|
|
|
|
|
|
|
|
candidate.append(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif strategy == 'exact':
|
|
|
|
|
|
|
|
if len(lines) != len(config):
|
|
|
|
|
|
|
|
candidate = list(lines)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
for cmd, cfg in itertools.izip(lines, config):
|
|
|
|
|
|
|
|
if cmd != cfg:
|
|
|
|
|
|
|
|
candidate = list(lines)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
for cmd in lines:
|
|
|
|
|
|
|
|
if cmd not in config:
|
|
|
|
|
|
|
|
candidate.append(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return candidate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
|
|
argument_spec = dict(
|
|
|
|
argument_spec = dict(
|
|
|
@ -220,45 +190,35 @@ def main():
|
|
|
|
contents = get_config(module)
|
|
|
|
contents = get_config(module)
|
|
|
|
config = module.parse_config(contents)
|
|
|
|
config = module.parse_config(contents)
|
|
|
|
|
|
|
|
|
|
|
|
if parents:
|
|
|
|
if not module.params['force']:
|
|
|
|
_config = list()
|
|
|
|
contents = get_config(module)
|
|
|
|
for item in config:
|
|
|
|
config = NetworkConfig(contents=contents, indent=2)
|
|
|
|
p = [p.text for p in item.parents]
|
|
|
|
|
|
|
|
if parents == p:
|
|
|
|
|
|
|
|
_config.append(item)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
candidate = NetworkConfig(indent=2)
|
|
|
|
children = [c.text for c in _config.children]
|
|
|
|
candidate.add(lines, parents=parents)
|
|
|
|
except AttributeError:
|
|
|
|
|
|
|
|
children = [c.text for c in _config]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commands = candidate.difference(config, path=parents, match=match, replace=replace)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
children = [c.text for c in config if not c.parents]
|
|
|
|
commands = parents
|
|
|
|
|
|
|
|
commands.extend(lines)
|
|
|
|
|
|
|
|
|
|
|
|
result = dict(changed=False)
|
|
|
|
result = dict(changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
candidate = build_candidate(lines, parents, children, match)
|
|
|
|
if commands:
|
|
|
|
|
|
|
|
|
|
|
|
if candidate:
|
|
|
|
|
|
|
|
if replace == 'line':
|
|
|
|
|
|
|
|
candidate[:0] = parents
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
candidate = list(parents)
|
|
|
|
|
|
|
|
candidate.extend(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if before:
|
|
|
|
if before:
|
|
|
|
candidate[:0] = before
|
|
|
|
commands[:0] = before
|
|
|
|
|
|
|
|
|
|
|
|
if after:
|
|
|
|
if after:
|
|
|
|
candidate.extend(after)
|
|
|
|
commands.extend(after)
|
|
|
|
|
|
|
|
|
|
|
|
if not module.check_mode:
|
|
|
|
if not module.check_mode:
|
|
|
|
response = module.configure(candidate)
|
|
|
|
commands = [str(c).strip() for c in commands]
|
|
|
|
|
|
|
|
response = module.configure(commands)
|
|
|
|
result['responses'] = response
|
|
|
|
result['responses'] = response
|
|
|
|
result['changed'] = True
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
|
|
|
|
result['updates'] = candidate
|
|
|
|
result['updates'] = commands
|
|
|
|
return module.exit_json(**result)
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
from ansible.module_utils.urls import *
|
|
|
|