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

fix nxos_static_route issues #37614

Merged
merged 2 commits into from Mar 26, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 28 additions & 52 deletions lib/ansible/modules/network/nxos/nxos_static_route.py
Expand Up @@ -52,13 +52,13 @@
default: default
tag:
description:
- Route tag value (numeric).
- Route tag value (numeric) or keyword 'default'.
route_name:
description:
- Name of the route. Used with the name parameter on the CLI.
- Name of the route or keyword 'default'. Used with the name parameter on the CLI.
pref:
description:
- Preference or administrative difference of route (range 1-255).
- Preference or administrative difference of route (range 1-255) or keyword 'default'.
aliases:
- admin_distance
aggregate:
Expand All @@ -67,8 +67,8 @@
state:
description:
- Manage the state of the resource.
required: true
choices: ['present','absent']
default: 'present'
'''

EXAMPLES = '''
Expand Down Expand Up @@ -105,78 +105,54 @@ def reconcile_candidate(module, candidate, prefix, w):

parents = []
commands = []
yrc = remove_command.replace('no ', '')
if w['vrf'] == 'default':
config = netcfg.get_section(set_command)
if config and state == 'absent':
netcfg = str(netcfg).split('\n')
ncfg = []
for line in netcfg:
# remove ip route commands of non-default vrfs from
# the running config just in case the same commands
# exist in default and non-default vrfs
if ' ip route' not in line:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, are these whitespaces for sub command ip route when using vrf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is correct

ncfg.append(line)
if any(yrc in s for s in ncfg) and state == 'absent':
commands = [remove_command]
elif not config and state == 'present':
commands = [set_command]
elif set_command not in ncfg and state == 'present':
if any(yrc in s for s in ncfg):
commands = [remove_command, set_command]
else:
commands = [set_command]
else:
parents = ['vrf context {0}'.format(w['vrf'])]
config = netcfg.get_section(parents)
if not isinstance(config, list):
config = config.split('\n')
config = [line.strip() for line in config]
if set_command in config and state == 'absent':
if any(yrc in s for s in config) and state == 'absent':
commands = [remove_command]
elif set_command not in config and state == 'present':
commands = [set_command]
if any(yrc in s for s in config):
commands = [remove_command, set_command]
else:
commands = [set_command]

if commands:
candidate.add(commands, parents=parents)


def fix_prefix_to_regex(prefix):
prefix = prefix.replace('.', r'\.').replace('/', r'\/')
return prefix


def get_existing(module, prefix, warnings):
key_map = ['tag', 'pref', 'route_name', 'next_hop']
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
parents = 'vrf context {0}'.format(module.params['vrf'])
prefix_to_regex = fix_prefix_to_regex(prefix)

route_regex = r'.*ip\sroute\s{0}\s(?P<next_hop>\S+)(\sname\s(?P<route_name>\S+))?(\stag\s(?P<tag>\d+))?(\s(?P<pref>\d+))?.*'.format(prefix_to_regex)

if module.params['vrf'] == 'default':
config = str(netcfg)
else:
config = netcfg.get_section(parents)

if config:
try:
match_route = re.match(route_regex, config, re.DOTALL)
group_route = match_route.groupdict()

for key in key_map:
if key not in group_route:
group_route[key] = ''
group_route['prefix'] = prefix
group_route['vrf'] = module.params['vrf']
except (AttributeError, TypeError):
group_route = {}
else:
group_route = {}
msg = ("VRF {0} didn't exist.".format(module.params['vrf']))
if msg not in warnings:
warnings.append(msg)

return group_route


def remove_route_command(prefix, w):
return 'no ip route {0} {1}'.format(prefix, w['next_hop'])


def set_route_command(prefix, w):
route_cmd = 'ip route {0} {1}'.format(prefix, w['next_hop'])

if w['route_name']:
if w['route_name'] and w['route_name'] != 'default':
route_cmd += ' name {0}'.format(w['route_name'])
if w['tag']:
route_cmd += ' tag {0}'.format(w['tag'])
if w['pref']:
if w['tag'] != 'default' and w['tag'] != '0':
route_cmd += ' tag {0}'.format(w['tag'])
if w['pref'] and w['pref'] != 'default':
route_cmd += ' {0}'.format(w['pref'])

return route_cmd
Expand Down
3 changes: 3 additions & 0 deletions test/integration/targets/nxos_static_route/defaults/main.yaml
@@ -1,2 +1,5 @@
---
testcase: "*"
vrfs:
- default
- myvrf
Expand Up @@ -11,8 +11,9 @@
route_name: testing
pref: 100
tag: 5500
vrf: testing
vrf: "{{ item }}"
provider: "{{ connection }}"
with_items: "{{ vrfs }}"
register: result

- assert: &true
Expand All @@ -21,28 +22,51 @@

- name: "Conf Idempotence"
nxos_static_route: *configure
with_items: "{{ vrfs }}"
register: result

- assert: &false
that:
- "result.changed == false"

- name: change static route
nxos_static_route: &configure1
prefix: "192.168.20.64/24"
next_hop: "3.3.3.3"
route_name: default
pref: 10
tag: default
vrf: "{{ item }}"
provider: "{{ connection }}"
with_items: "{{ vrfs }}"
register: result

- assert: *true

- name: "Conf1 Idempotence"
nxos_static_route: *configure1
with_items: "{{ vrfs }}"
register: result

- assert: *false

- name: remove static route
nxos_static_route: &remove
prefix: "192.168.20.64/24"
next_hop: "3.3.3.3"
route_name: testing
pref: 100
tag: 5500
vrf: testing
vrf: "{{ item }}"
state: absent
provider: "{{ connection }}"
with_items: "{{ vrfs }}"
register: result

- assert: *true

- name: "Remove Idempotence"
nxos_static_route: *remove
with_items: "{{ vrfs }}"
register: result

- assert: *false
Expand Down Expand Up @@ -96,9 +120,10 @@
route_name: testing
pref: 100
tag: 5500
vrf: testing
vrf: "{{ item }}"
state: absent
provider: "{{ connection }}"
with_items: "{{ vrfs }}"
ignore_errors: yes

- name: remove static route aggregate
Expand Down
1 change: 0 additions & 1 deletion test/sanity/validate-modules/ignore.txt
Expand Up @@ -1326,7 +1326,6 @@ lib/ansible/modules/network/nxos/nxos_pim_interface.py E326
lib/ansible/modules/network/nxos/nxos_pim_rp_address.py E326
lib/ansible/modules/network/nxos/nxos_reboot.py E325
lib/ansible/modules/network/nxos/nxos_smu.py E324
lib/ansible/modules/network/nxos/nxos_static_route.py E324
lib/ansible/modules/network/nxos/nxos_system.py E325
lib/ansible/modules/network/nxos/nxos_vpc.py E324
lib/ansible/modules/network/nxos/nxos_vpc_interface.py E325
Expand Down