mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 14:41:23 -07:00
nxos_static_route DI module (#33797)
* nxos_static_route DI module Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * add version_added
This commit is contained in:
parent
7381554e42
commit
4a4da2b774
2 changed files with 132 additions and 35 deletions
|
@ -39,6 +39,8 @@ options:
|
||||||
description:
|
description:
|
||||||
- Destination prefix of static route.
|
- Destination prefix of static route.
|
||||||
required: true
|
required: true
|
||||||
|
aliases:
|
||||||
|
- address
|
||||||
next_hop:
|
next_hop:
|
||||||
description:
|
description:
|
||||||
- Next hop address or interface of static route.
|
- Next hop address or interface of static route.
|
||||||
|
@ -64,6 +66,11 @@ options:
|
||||||
- Preference or administrative difference of route (range 1-255).
|
- Preference or administrative difference of route (range 1-255).
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
|
aliases:
|
||||||
|
- admin_distance
|
||||||
|
aggregate:
|
||||||
|
description: List of static route definitions
|
||||||
|
version_added: 2.5
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Manage the state of the resource.
|
- Manage the state of the resource.
|
||||||
|
@ -87,30 +94,32 @@ commands:
|
||||||
sample: ["ip route 192.168.20.0/24 3.3.3.3 name testing 100"]
|
sample: ["ip route 192.168.20.0/24 3.3.3.3 name testing 100"]
|
||||||
'''
|
'''
|
||||||
import re
|
import re
|
||||||
|
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
|
||||||
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args
|
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
|
||||||
|
from ansible.module_utils.network.common.utils import remove_default_spec
|
||||||
|
|
||||||
|
|
||||||
def reconcile_candidate(module, candidate, prefix):
|
def reconcile_candidate(module, candidate, prefix, w):
|
||||||
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
|
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
|
||||||
state = module.params['state']
|
state = w['state']
|
||||||
|
|
||||||
set_command = set_route_command(module, prefix)
|
set_command = set_route_command(prefix, w)
|
||||||
remove_command = remove_route_command(module, prefix)
|
remove_command = remove_route_command(prefix, w)
|
||||||
|
|
||||||
parents = []
|
parents = []
|
||||||
commands = []
|
commands = []
|
||||||
if module.params['vrf'] == 'default':
|
if w['vrf'] == 'default':
|
||||||
config = netcfg.get_section(set_command)
|
config = netcfg.get_section(set_command)
|
||||||
if config and state == 'absent':
|
if config and state == 'absent':
|
||||||
commands = [remove_command]
|
commands = [remove_command]
|
||||||
elif not config and state == 'present':
|
elif not config and state == 'present':
|
||||||
commands = [set_command]
|
commands = [set_command]
|
||||||
else:
|
else:
|
||||||
parents = ['vrf context {0}'.format(module.params['vrf'])]
|
parents = ['vrf context {0}'.format(w['vrf'])]
|
||||||
config = netcfg.get_section(parents)
|
config = netcfg.get_section(parents)
|
||||||
if not isinstance(config, list):
|
if not isinstance(config, list):
|
||||||
config = config.split('\n')
|
config = config.split('\n')
|
||||||
|
@ -163,19 +172,19 @@ def get_existing(module, prefix, warnings):
|
||||||
return group_route
|
return group_route
|
||||||
|
|
||||||
|
|
||||||
def remove_route_command(module, prefix):
|
def remove_route_command(prefix, w):
|
||||||
return 'no ip route {0} {1}'.format(prefix, module.params['next_hop'])
|
return 'no ip route {0} {1}'.format(prefix, w['next_hop'])
|
||||||
|
|
||||||
|
|
||||||
def set_route_command(module, prefix):
|
def set_route_command(prefix, w):
|
||||||
route_cmd = 'ip route {0} {1}'.format(prefix, module.params['next_hop'])
|
route_cmd = 'ip route {0} {1}'.format(prefix, w['next_hop'])
|
||||||
|
|
||||||
if module.params['route_name']:
|
if w['route_name']:
|
||||||
route_cmd += ' name {0}'.format(module.params['route_name'])
|
route_cmd += ' name {0}'.format(w['route_name'])
|
||||||
if module.params['tag']:
|
if w['tag']:
|
||||||
route_cmd += ' tag {0}'.format(module.params['tag'])
|
route_cmd += ' tag {0}'.format(w['tag'])
|
||||||
if module.params['pref']:
|
if w['pref']:
|
||||||
route_cmd += ' {0}'.format(module.params['pref'])
|
route_cmd += ' {0}'.format(w['pref'])
|
||||||
|
|
||||||
return route_cmd
|
return route_cmd
|
||||||
|
|
||||||
|
@ -236,17 +245,54 @@ def normalize_prefix(module, prefix):
|
||||||
return normalized_prefix
|
return normalized_prefix
|
||||||
|
|
||||||
|
|
||||||
|
def map_params_to_obj(module):
|
||||||
|
obj = []
|
||||||
|
aggregate = module.params.get('aggregate')
|
||||||
|
if aggregate:
|
||||||
|
for item in aggregate:
|
||||||
|
for key in item:
|
||||||
|
if item.get(key) is None:
|
||||||
|
item[key] = module.params[key]
|
||||||
|
|
||||||
|
d = item.copy()
|
||||||
|
obj.append(d)
|
||||||
|
else:
|
||||||
|
obj.append({
|
||||||
|
'prefix': module.params['prefix'],
|
||||||
|
'next_hop': module.params['next_hop'],
|
||||||
|
'vrf': module.params['vrf'],
|
||||||
|
'tag': module.params['tag'],
|
||||||
|
'route_name': module.params['route_name'],
|
||||||
|
'pref': module.params['pref'],
|
||||||
|
'state': module.params['state']
|
||||||
|
})
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = dict(
|
element_spec = dict(
|
||||||
prefix=dict(required=True, type='str'),
|
prefix=dict(type='str', aliases=['address']),
|
||||||
next_hop=dict(required=True, type='str'),
|
next_hop=dict(type='str'),
|
||||||
vrf=dict(type='str', default='default'),
|
vrf=dict(type='str', default='default'),
|
||||||
tag=dict(type='str'),
|
tag=dict(type='str'),
|
||||||
route_name=dict(type='str'),
|
route_name=dict(type='str'),
|
||||||
pref=dict(type='str'),
|
pref=dict(type='str', aliases=['admin_distance']),
|
||||||
state=dict(choices=['absent', 'present'], default='present'),
|
state=dict(choices=['absent', 'present'], default='present'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
aggregate_spec = deepcopy(element_spec)
|
||||||
|
aggregate_spec['prefix'] = dict(required=True)
|
||||||
|
aggregate_spec['next_hop'] = dict(required=True)
|
||||||
|
|
||||||
|
# remove default in aggregate spec, to handle common arguments
|
||||||
|
remove_default_spec(aggregate_spec)
|
||||||
|
|
||||||
|
argument_spec = dict(
|
||||||
|
aggregate=dict(type='list', elements='dict', options=aggregate_spec)
|
||||||
|
)
|
||||||
|
|
||||||
|
argument_spec.update(element_spec)
|
||||||
argument_spec.update(nxos_argument_spec)
|
argument_spec.update(nxos_argument_spec)
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -255,24 +301,25 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
check_args(module, warnings)
|
result = {'changed': False, 'commands': []}
|
||||||
result = dict(changed=False, warnings=warnings)
|
if warnings:
|
||||||
|
result['warnings'] = warnings
|
||||||
|
|
||||||
prefix = normalize_prefix(module, module.params['prefix'])
|
want = map_params_to_obj(module)
|
||||||
|
for w in want:
|
||||||
|
prefix = normalize_prefix(module, w['prefix'])
|
||||||
|
candidate = CustomNetworkConfig(indent=3)
|
||||||
|
reconcile_candidate(module, candidate, prefix, w)
|
||||||
|
|
||||||
candidate = CustomNetworkConfig(indent=3)
|
if candidate:
|
||||||
reconcile_candidate(module, candidate, prefix)
|
candidate = candidate.items_text()
|
||||||
|
load_config(module, candidate)
|
||||||
if candidate:
|
result['commands'].extend(candidate)
|
||||||
candidate = candidate.items_text()
|
result['changed'] = True
|
||||||
load_config(module, candidate)
|
else:
|
||||||
result['commands'] = candidate
|
result['commands'] = []
|
||||||
result['changed'] = True
|
|
||||||
else:
|
|
||||||
result['commands'] = []
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -45,6 +45,47 @@
|
||||||
|
|
||||||
- assert: *false
|
- assert: *false
|
||||||
|
|
||||||
|
- name: configure static route(aggregate)
|
||||||
|
nxos_static_route: &conf_agg
|
||||||
|
aggregate:
|
||||||
|
- { prefix: "192.168.22.64/24", next_hop: "3.3.3.3" }
|
||||||
|
- { prefix: "192.168.24.64/24", next_hop: "3.3.3.3" }
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == true"
|
||||||
|
|
||||||
|
- name: configure static route aggregate(Idempotence)
|
||||||
|
nxos_static_route: *conf_agg
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == false"
|
||||||
|
|
||||||
|
- name: remove static route aggregate
|
||||||
|
nxos_static_route: &remove_agg
|
||||||
|
aggregate:
|
||||||
|
- { prefix: "192.168.22.64/24", next_hop: "3.3.3.3" }
|
||||||
|
- { prefix: "192.168.24.64/24", next_hop: "3.3.3.3" }
|
||||||
|
state: absent
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == true"
|
||||||
|
|
||||||
|
- name: remove static route aggregate(Idempotence)
|
||||||
|
nxos_static_route: *remove_agg
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == false"
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: remove static route
|
- name: remove static route
|
||||||
nxos_static_route:
|
nxos_static_route:
|
||||||
|
@ -58,4 +99,13 @@
|
||||||
provider: "{{ connection }}"
|
provider: "{{ connection }}"
|
||||||
ignore_errors: yes
|
ignore_errors: yes
|
||||||
|
|
||||||
|
- name: remove static route aggregate
|
||||||
|
nxos_static_route:
|
||||||
|
aggregate:
|
||||||
|
- { prefix: "192.168.22.64/24", next_hop: "3.3.3.3" }
|
||||||
|
- { prefix: "192.168.24.64/24", next_hop: "3.3.3.3" }
|
||||||
|
state: absent
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
ignore_errors: yes
|
||||||
|
|
||||||
- debug: msg="END TRANSPORT:{{ connection.transport }} nxos_static_route sanity test"
|
- debug: msg="END TRANSPORT:{{ connection.transport }} nxos_static_route sanity test"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue