@ -50,10 +50,6 @@ options:
- Admin distance of the static route .
aggregate :
description : List of static route definitions
purge :
description :
- Purge static routes not defined in the aggregates parameter .
default : no
state :
description :
- State of the static route configuration .
@ -67,22 +63,32 @@ EXAMPLES = """
prefix : 192.168 .2 .0
mask : 24
next_hop : 10.0 .0 .1
- name : configure static route prefix / mask
vyos_static_route :
prefix : 192.168 .2 .0 / 16
next_hop : 10.0 .0 .1
- name : remove configuration
vyos_static_route :
prefix : 192.168 .2 .0
mask : 16
next_hop : 10.0 .0 .1
state : absent
- name : configure aggregates of static routes
vyos_static_route :
aggregate :
- { prefix : 192.168 .2 .0 , mask : 24 , next_hop : 10.0 .0 .1 }
- { prefix : 192.168 .3 .0 , mask : 16 , next_hop : 10.0 .2 .1 }
- { prefix : 192.168 .3 .0 / 16 , next_hop : 10.0 .2 .1 }
- name : Remove static route collections
vyos_static_route :
aggregate :
- { prefix : 172.24 .1 .0 / 24 , next_hop : 192.168 .42 .64 }
- { prefix : 172.24 .3 .0 / 24 , next_hop : 192.168 .42 .64 }
state : absent
"""
RETURN = """
@ -93,10 +99,12 @@ commands:
sample :
- set protocols static route 192.168 .2 .0 / 16 next - hop 10.0 .0 .1
"""
import re
from copy import deepcopy
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . network_common import remove_default_spec
from ansible . module_utils . vyos import get_config , load_config
from ansible . module_utils . vyos import vyos_argument_spec , check_args
@ -156,20 +164,23 @@ def config_to_dict(module):
return obj
def map_params_to_obj ( module ):
def map_params_to_obj ( module , required_together = None ):
obj = [ ]
if ' aggregate ' in module . params and module . params [ ' aggregate ' ] :
for c in module . params [ ' aggregate ' ] :
d = c . copy ( )
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 ]
module . _check_required_together ( required_together , item )
d = item . copy ( )
if ' / ' in d [ ' prefix ' ] :
d [ ' mask ' ] = d [ ' prefix ' ] . split ( ' / ' ) [ 1 ]
d [ ' prefix ' ] = d [ ' prefix ' ] . split ( ' / ' ) [ 0 ]
if ' state ' not in d :
d [ ' state ' ] = module . params [ ' state ' ]
if ' admin_distance ' not in d :
d [ ' admin_distance ' ] = str ( module . params [ ' admin_distance ' ] )
if ' admin_distance ' in d :
d [ ' admin_distance ' ] = str ( d [ ' admin_distance ' ] )
obj . append ( d )
else :
@ -197,16 +208,27 @@ def map_params_to_obj(module):
def main ( ) :
""" main entry point for module execution
"""
argu ment_spec = dict (
ele ment_spec = dict (
prefix = dict ( type = ' str ' ) ,
mask = dict ( type = ' str ' ) ,
next_hop = dict ( type = ' str ' ) ,
admin_distance = dict ( type = ' int ' ) ,
aggregate = dict ( type = ' list ' ) ,
purge = dict ( type = ' bool ' ) ,
state = dict ( default = ' present ' , choices = [ ' present ' , ' absent ' ] )
)
aggregate_spec = deepcopy ( element_spec )
aggregate_spec [ ' prefix ' ] = 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 ( vyos_argument_spec )
argument_spec . update ( vyos_argument_spec )
required_one_of = [ [ ' aggregate ' , ' prefix ' ] ]
required_together = [ [ ' prefix ' , ' next_hop ' ] ]
@ -215,6 +237,7 @@ def main():
module = AnsibleModule ( argument_spec = argument_spec ,
required_one_of = required_one_of ,
required_together = required_together ,
mutually_exclusive = mutually_exclusive ,
supports_check_mode = True )
warnings = list ( )
@ -223,7 +246,7 @@ def main():
result = { ' changed ' : False }
if warnings :
result [ ' warnings ' ] = warnings
want = map_params_to_obj ( module )
want = map_params_to_obj ( module , required_together = required_together )
have = config_to_dict ( module )
commands = spec_to_commands ( ( want , have ) , module )