mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 11:10:21 -07:00
Added track option for nxos_static_route module (#48710)
This commit is contained in:
parent
4f80c45c51
commit
01b06dd5f2
2 changed files with 97 additions and 24 deletions
|
@ -64,6 +64,10 @@ options:
|
||||||
aggregate:
|
aggregate:
|
||||||
description: List of static route definitions
|
description: List of static route definitions
|
||||||
version_added: 2.5
|
version_added: 2.5
|
||||||
|
track:
|
||||||
|
description:
|
||||||
|
- Track value (range 1 - 512). Track must already be configured on the device before adding the route.
|
||||||
|
version_added: "2.8"
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Manage the state of the resource.
|
- Manage the state of the resource.
|
||||||
|
@ -89,7 +93,7 @@ commands:
|
||||||
import re
|
import re
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from ansible.module_utils.network.nxos.nxos import get_config, load_config
|
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
|
||||||
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
|
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.network.common.config import CustomNetworkConfig
|
from ansible.module_utils.network.common.config import CustomNetworkConfig
|
||||||
|
@ -100,7 +104,7 @@ def reconcile_candidate(module, candidate, prefix, w):
|
||||||
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
|
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
|
||||||
state = w['state']
|
state = w['state']
|
||||||
|
|
||||||
set_command = set_route_command(prefix, w)
|
set_command = set_route_command(prefix, w, module)
|
||||||
remove_command = remove_route_command(prefix, w)
|
remove_command = remove_route_command(prefix, w)
|
||||||
|
|
||||||
parents = []
|
parents = []
|
||||||
|
@ -144,9 +148,31 @@ def remove_route_command(prefix, w):
|
||||||
return 'no ip route {0} {1}'.format(prefix, w['next_hop'])
|
return 'no ip route {0} {1}'.format(prefix, w['next_hop'])
|
||||||
|
|
||||||
|
|
||||||
def set_route_command(prefix, w):
|
def get_configured_track(module, ctrack):
|
||||||
|
check_track = '{0}'.format(ctrack)
|
||||||
|
track_exists = False
|
||||||
|
command = 'show track'
|
||||||
|
try:
|
||||||
|
body = run_commands(module, [command])
|
||||||
|
match = re.findall(r'Track\s+(\d+)', body[0])
|
||||||
|
except IndexError:
|
||||||
|
return None
|
||||||
|
if check_track in match:
|
||||||
|
track_exists = True
|
||||||
|
return track_exists
|
||||||
|
|
||||||
|
|
||||||
|
def set_route_command(prefix, w, module):
|
||||||
route_cmd = 'ip route {0} {1}'.format(prefix, w['next_hop'])
|
route_cmd = 'ip route {0} {1}'.format(prefix, w['next_hop'])
|
||||||
|
|
||||||
|
if w['track']:
|
||||||
|
if w['track'] in range(1, 512):
|
||||||
|
if get_configured_track(module, w['track']):
|
||||||
|
route_cmd += ' track {0}'.format(w['track'])
|
||||||
|
else:
|
||||||
|
module.fail_json(msg='Track {0} not configured on device'.format(w['track']))
|
||||||
|
else:
|
||||||
|
module.fail_json(msg='Invalid track number, valid range is 1-512.')
|
||||||
if w['route_name'] and w['route_name'] != 'default':
|
if w['route_name'] and w['route_name'] != 'default':
|
||||||
route_cmd += ' name {0}'.format(w['route_name'])
|
route_cmd += ' name {0}'.format(w['route_name'])
|
||||||
if w['tag']:
|
if w['tag']:
|
||||||
|
@ -233,7 +259,8 @@ def map_params_to_obj(module):
|
||||||
'tag': module.params['tag'],
|
'tag': module.params['tag'],
|
||||||
'route_name': module.params['route_name'],
|
'route_name': module.params['route_name'],
|
||||||
'pref': module.params['pref'],
|
'pref': module.params['pref'],
|
||||||
'state': module.params['state']
|
'state': module.params['state'],
|
||||||
|
'track': module.params['track']
|
||||||
})
|
})
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
|
@ -248,6 +275,7 @@ def main():
|
||||||
route_name=dict(type='str'),
|
route_name=dict(type='str'),
|
||||||
pref=dict(type='str', aliases=['admin_distance']),
|
pref=dict(type='str', aliases=['admin_distance']),
|
||||||
state=dict(choices=['absent', 'present'], default='present'),
|
state=dict(choices=['absent', 'present'], default='present'),
|
||||||
|
track=dict(type='int'),
|
||||||
)
|
)
|
||||||
|
|
||||||
aggregate_spec = deepcopy(element_spec)
|
aggregate_spec = deepcopy(element_spec)
|
||||||
|
|
|
@ -4,8 +4,14 @@
|
||||||
when: ansible_connection == "local"
|
when: ansible_connection == "local"
|
||||||
|
|
||||||
- block:
|
- block:
|
||||||
|
- name: configure track
|
||||||
|
nxos_config:
|
||||||
|
lines:
|
||||||
|
- track 1 ip sla 1
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
|
||||||
- name: create static route
|
- name: create static route
|
||||||
nxos_static_route: &configure
|
nxos_static_route: &configure_static
|
||||||
prefix: "192.168.20.64/24"
|
prefix: "192.168.20.64/24"
|
||||||
next_hop: "192.0.2.3"
|
next_hop: "192.0.2.3"
|
||||||
route_name: testing
|
route_name: testing
|
||||||
|
@ -20,8 +26,8 @@
|
||||||
that:
|
that:
|
||||||
- "result.changed == true"
|
- "result.changed == true"
|
||||||
|
|
||||||
- name: "Conf Idempotence"
|
- name: "Conf static Idempotence"
|
||||||
nxos_static_route: *configure
|
nxos_static_route: *configure_static
|
||||||
with_items: "{{ vrfs }}"
|
with_items: "{{ vrfs }}"
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
|
@ -30,7 +36,7 @@
|
||||||
- "result.changed == false"
|
- "result.changed == false"
|
||||||
|
|
||||||
- name: change static route
|
- name: change static route
|
||||||
nxos_static_route: &configure1
|
nxos_static_route: &change_static
|
||||||
prefix: "192.168.20.64/24"
|
prefix: "192.168.20.64/24"
|
||||||
next_hop: "192.0.2.3"
|
next_hop: "192.0.2.3"
|
||||||
route_name: default
|
route_name: default
|
||||||
|
@ -43,15 +49,55 @@
|
||||||
|
|
||||||
- assert: *true
|
- assert: *true
|
||||||
|
|
||||||
- name: "Conf1 Idempotence"
|
- name: "Change Idempotence"
|
||||||
nxos_static_route: *configure1
|
nxos_static_route: *change_static
|
||||||
with_items: "{{ vrfs }}"
|
with_items: "{{ vrfs }}"
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert: *false
|
- assert: *false
|
||||||
|
|
||||||
|
- name: configure static route with track
|
||||||
|
nxos_static_route: &config_static_track
|
||||||
|
prefix: "192.168.20.64/24"
|
||||||
|
next_hop: "192.0.2.3"
|
||||||
|
route_name: default
|
||||||
|
pref: 10
|
||||||
|
tag: default
|
||||||
|
track: 1
|
||||||
|
vrf: "{{ item }}"
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
with_items: "{{ vrfs }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: "Config track Idempotence"
|
||||||
|
nxos_static_route: *config_static_track
|
||||||
|
with_items: "{{ vrfs }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
|
- name: configure static route with not configured track
|
||||||
|
nxos_static_route: &config_static_track
|
||||||
|
prefix: "192.168.20.64/24"
|
||||||
|
next_hop: "192.0.2.3"
|
||||||
|
route_name: default
|
||||||
|
pref: 10
|
||||||
|
tag: default
|
||||||
|
track: 2
|
||||||
|
vrf: "{{ item }}"
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
with_items: "{{ vrfs }}"
|
||||||
|
register: result
|
||||||
|
ignore_errors: yes
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.failed == True"
|
||||||
|
|
||||||
- name: remove static route
|
- name: remove static route
|
||||||
nxos_static_route: &remove
|
nxos_static_route: &remove_static
|
||||||
prefix: "192.168.20.64/24"
|
prefix: "192.168.20.64/24"
|
||||||
next_hop: "192.0.2.3"
|
next_hop: "192.0.2.3"
|
||||||
route_name: testing
|
route_name: testing
|
||||||
|
@ -65,7 +111,7 @@
|
||||||
- assert: *true
|
- assert: *true
|
||||||
|
|
||||||
- name: "Remove Idempotence"
|
- name: "Remove Idempotence"
|
||||||
nxos_static_route: *remove
|
nxos_static_route: *remove_static
|
||||||
with_items: "{{ vrfs }}"
|
with_items: "{{ vrfs }}"
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
|
@ -79,17 +125,13 @@
|
||||||
provider: "{{ connection }}"
|
provider: "{{ connection }}"
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert:
|
- assert: *true
|
||||||
that:
|
|
||||||
- "result.changed == true"
|
|
||||||
|
|
||||||
- name: configure static route aggregate(Idempotence)
|
- name: configure static route aggregate(Idempotence)
|
||||||
nxos_static_route: *conf_agg
|
nxos_static_route: *conf_agg
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert:
|
- assert: *false
|
||||||
that:
|
|
||||||
- "result.changed == false"
|
|
||||||
|
|
||||||
- name: remove static route aggregate
|
- name: remove static route aggregate
|
||||||
nxos_static_route: &remove_agg
|
nxos_static_route: &remove_agg
|
||||||
|
@ -100,19 +142,22 @@
|
||||||
state: absent
|
state: absent
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert:
|
- assert: *true
|
||||||
that:
|
|
||||||
- "result.changed == true"
|
|
||||||
|
|
||||||
- name: remove static route aggregate(Idempotence)
|
- name: remove static route aggregate(Idempotence)
|
||||||
nxos_static_route: *remove_agg
|
nxos_static_route: *remove_agg
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert:
|
- assert: *false
|
||||||
that:
|
|
||||||
- "result.changed == false"
|
|
||||||
|
|
||||||
always:
|
always:
|
||||||
|
- name: remove track
|
||||||
|
nxos_config:
|
||||||
|
lines:
|
||||||
|
- no track 1
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
ignore_errors: yes
|
||||||
|
|
||||||
- name: remove static route
|
- name: remove static route
|
||||||
nxos_static_route:
|
nxos_static_route:
|
||||||
prefix: "192.168.20.64/24"
|
prefix: "192.168.20.64/24"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue