1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Nxos vxlan vtep vni netcfg fix (#23620)

* Code cleanup
Removed 'add' method from CustomNetworkConfig.  It is identical
to the one inherited from NetworkConfig

* Removed unused CustomNetworkConfig import

* Replaced
```
def get_existing(module, args):
    existing = {}
    netcfg = get_config(module)
    config = netcfg.get_section(parents)
```
with
```
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
```
get_config returns a string, not an object in 2.3.

* Removed non-functioning get_object method in CustomNetworkConfig in favor of the
inherited method.

Added child_objs property so that expand_selection would work.  The original
verion never worked correctly as it compared NetworkConfig obj's and str's.

* Removed ShellError method in favor or new load_config method.

* Removed ShellError method in favor or new load_config method.
fixes #20260

* nxos requires a "no" statement to change mcase group.  Corrected.
Corrected changed logic.

* Corrected deleted CustomNetworkConfig import
This commit is contained in:
Daniel 2017-04-17 11:39:21 -05:00 committed by Nathaniel Case
parent e518b6f5b4
commit bbb9f3164c
44 changed files with 35 additions and 112 deletions

View file

@ -66,6 +66,10 @@ class ConfigLine(object):
def children(self):
return _obj_to_text(self._children)
@property
def child_objs(self):
return self._children
@property
def parents(self):
return _obj_to_text(self._parents)
@ -368,19 +372,12 @@ class CustomNetworkConfig(NetworkConfig):
if S is None:
S = list()
S.append(configobj)
for child in configobj.children:
for child in configobj.child_objs:
if child in S:
continue
self.expand_section(child, S)
return S
def get_object(self, path):
for item in self.items:
if item.text == path[-1]:
parents = [p.text for p in item.parents]
if parents == path[:-1]:
return item
def to_block(self, section):
return '\n'.join([item.raw for item in section])
@ -398,55 +395,3 @@ class CustomNetworkConfig(NetworkConfig):
if not obj:
raise ValueError('path does not exist in config')
return self.expand_section(obj)
def add(self, lines, parents=None):
"""Adds one or lines of configuration
"""
ancestors = list()
offset = 0
obj = None
## global config command
if not parents:
for line in to_list(lines):
item = ConfigLine(line)
item.raw = line
if item not in self.items:
self.items.append(item)
else:
for index, p in enumerate(parents):
try:
i = index + 1
obj = self.get_section_objects(parents[:i])[0]
ancestors.append(obj)
except ValueError:
# add parent to config
offset = index * self.indent
obj = ConfigLine(p)
obj.raw = p.rjust(len(p) + offset)
if ancestors:
obj.parents = list(ancestors)
ancestors[-1].children.append(obj)
self.items.append(obj)
ancestors.append(obj)
# add child objects
for line in to_list(lines):
# check if child already exists
for child in ancestors[-1].children:
if child.text == line:
break
else:
offset = len(parents) * self.indent
item = ConfigLine(line)
item.raw = line.rjust(len(line) + offset)
item.parents = ancestors
ancestors[-1].children.append(item)
self.items.append(item)

View file

@ -549,7 +549,7 @@ def get_value(arg, config):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
try:
asn_regex = '.*router\sbgp\s(?P<existing_asn>\d+).*'

View file

@ -539,7 +539,7 @@ def get_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
try:
asn_regex = '.*router\sbgp\s(?P<existing_asn>\d+).*'

View file

@ -372,7 +372,7 @@ def get_custom_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
custom = [
'log_neighbor_changes',
'pwd',

View file

@ -503,7 +503,7 @@ def get_custom_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
custom = [
'allowas_in_max',

View file

@ -174,7 +174,7 @@ def get_route_target_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = ['evpn', 'vni {0} l2'.format(module.params['vni'])]
config = netcfg.get_section(parents)

View file

@ -166,7 +166,6 @@ import re
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
def execute_show_command(command, module, command_type='cli_show_ascii'):
cmds = [command]

View file

@ -127,7 +127,7 @@ from ansible.module_utils.netcfg import CustomNetworkConfig
def get_existing(module):
existing = []
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
if module.params['mode'] == 'maintenance':
parents = ['configure maintenance profile maintenance-mode']

View file

@ -148,7 +148,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig

View file

@ -238,7 +238,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -134,7 +134,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -122,7 +122,6 @@ import re
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
def execute_show_command(command, module, command_type='cli_show_ascii'):

View file

@ -169,7 +169,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig

View file

@ -281,7 +281,7 @@ def get_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = ['interface {0}'.format(module.params['interface'].capitalize())]
config = netcfg.get_section(parents)
if 'ospf' in config:

View file

@ -116,7 +116,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -130,7 +130,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -128,7 +128,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -109,7 +109,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -239,7 +239,7 @@ def get_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = ['router ospf {0}'.format(module.params['ospf'])]
if module.params['vrf'] != 'default':

View file

@ -192,7 +192,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import time

View file

@ -207,7 +207,7 @@ def get_portchannel_mode(interface, protocol, module, netcfg):
if protocol != 'LACP':
mode = 'on'
else:
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = ['interface {0}'.format(interface.capitalize())]
body = netcfg.get_section(parents)
@ -281,7 +281,7 @@ def get_portchannel(module, netcfg=None):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
interface_exist = check_interface(module, netcfg)
if interface_exist:

View file

@ -81,7 +81,6 @@ status:
from ansible.module_utils.nxos import nxos_argument_spec, run_commands
from ansible.module_utils.basic import AnsibleModule
def checkpoint(filename, module):
commands = ['terminal dont-ask', 'checkpoint file %s' % filename]
run_commands(module, commands)

View file

@ -84,7 +84,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import time
import collections

View file

@ -103,7 +103,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -88,7 +88,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -136,7 +136,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -95,7 +95,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -106,7 +106,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -117,7 +117,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -136,7 +136,7 @@ def state_present(module, candidate, prefix):
def state_absent(module, candidate, prefix):
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
commands = list()
parents = 'vrf context {0}'.format(module.params['vrf'])
invoke('set_route', module, commands, prefix)
@ -161,7 +161,7 @@ def fix_prefix_to_regex(prefix):
def get_existing(module, prefix, warnings):
key_map = ['tag', 'pref', 'route_name', 'next_hop']
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = 'vrf context {0}'.format(module.params['vrf'])
prefix_to_regex = fix_prefix_to_regex(prefix)

View file

@ -159,7 +159,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -113,7 +113,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -112,7 +112,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -150,7 +150,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli':

View file

@ -102,7 +102,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli':

View file

@ -124,7 +124,6 @@ import re
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
def execute_show_command(command, module, command_type='cli_show'):
transport = module.params['provider']['transport']

View file

@ -144,7 +144,7 @@ def get_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = ['vrf context {0}'.format(module.params['vrf'])]
parents.append('address-family {0} {1}'.format(module.params['afi'],

View file

@ -104,7 +104,6 @@ import re
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
WARNINGS = []

View file

@ -142,7 +142,6 @@ import re
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli':

View file

@ -89,7 +89,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -106,7 +106,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -84,7 +84,6 @@ changed:
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import CustomNetworkConfig
import re

View file

@ -202,7 +202,7 @@ def get_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
interface_string = 'interface {0}'.format(module.params['interface'].lower())
parents = [interface_string]

View file

@ -217,7 +217,7 @@ def get_custom_value(arg, config, module):
def get_existing(module, args):
existing = {}
netcfg = get_config(module)
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
custom = [
'assoc_vrf',
@ -283,6 +283,10 @@ def state_present(module, existing, proposed, candidate):
for peer in value:
commands.append('{0} {1}'.format(key, peer))
elif key == 'mcast-group' and value != existing_commands.get(key):
commands.append('no {0}'.format(key))
commands.append('{0} {1}'.format(key, value))
elif value is True:
commands.append(key)
@ -422,13 +426,17 @@ def main():
else:
candidate = CustomNetworkConfig(indent=3)
invoke('state_%s' % state, module, existing, proposed, candidate)
try:
response = load_config(module, candidate)
result.update(response)
except ShellError:
exc = get_exception()
module.fail_json(msg=str(exc))
result['changed'] = False
for k, v in proposed.items():
if k in existing:
if existing[k] != proposed[k] or state == 'absent':
result['changed'] = True
if k not in existing and state == 'present':
result['changed'] = True
if module.check_mode:
module.exit_json(commands=candidate)
else:
load_config(module, candidate)
else:
result['updates'] = []