@ -180,6 +180,7 @@ session_name:
sample : ansible_1479315771
"""
import re
import time
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . eos import run_commands , load_config
@ -205,7 +206,7 @@ def validate_vrf(value, module):
if value not in configured_vrfs :
module . fail_json ( msg = ' vrf ` %s ` is not configured on the system ' % value )
def map_obj_to_commands ( updates , module ):
def map_obj_to_commands ( updates , module , warnings ):
commands = list ( )
want , have = updates
@ -220,22 +221,38 @@ def map_obj_to_commands(updates, module):
if want [ ' http ' ] is False :
add ( ' no protocol http ' )
else :
port = want [ ' http_port ' ] or 80
add ( ' protocol http port %s ' % port )
if have [ ' http ' ] is False and want [ ' http ' ] in ( False , None ) :
warnings . append ( ' protocol http is not enabled, not configuring http port value ' )
else :
port = want [ ' http_port ' ] or 80
add ( ' protocol http port %s ' % port )
if any ( ( needs_update ( ' https ' ) , needs_update ( ' https_port ' ) ) ) :
if want [ ' https ' ] is False :
add ( ' no protocol https ' )
else :
port = want [ ' https_port ' ] or 443
add ( ' protocol https port %s ' % port )
if have [ ' https ' ] is False and want [ ' https ' ] in ( False , None ) :
warnings . append ( ' protocol https is not enabled, not configuring https port value ' )
else :
port = want [ ' https_port ' ] or 443
add ( ' protocol https port %s ' % port )
if any ( ( needs_update ( ' local_http ' ) , needs_update ( ' local_http_port ' ) ) ) :
if want [ ' local_http ' ] is False :
add ( ' no protocol http localhost ' )
else :
port = want [ ' local_http_port ' ] or 8080
add ( ' protocol http localhost port %s ' % port )
if have [ ' local_http ' ] is False and want [ ' local_http ' ] in ( False , None ) :
warnings . append ( ' protocol local_http is not enabled, not configuring local_http port value ' )
else :
port = want [ ' local_http_port ' ] or 8080
add ( ' protocol http localhost port %s ' % port )
if any ( ( needs_update ( ' socket ' ) , needs_update ( ' socket ' ) ) ) :
if want [ ' socket ' ] is False :
add ( ' no protocol unix-socket ' )
else :
add ( ' protocol unix-socket ' )
if needs_update ( ' vrf ' ) :
add ( ' vrf %s ' % want [ ' vrf ' ] )
@ -290,6 +307,34 @@ def map_params_to_obj(module):
return obj
def verify_state ( updates , module ) :
want , have = updates
invalid_state = [ ( ' http ' , ' httpServer ' ) ,
( ' https ' , ' httpsServer ' ) ,
( ' local_http ' , ' localHttpServer ' ) ,
( ' socket ' , ' unixSocketServer ' ) ]
timeout = module . params [ ' timeout ' ] or 30
state = module . params [ ' state ' ]
while invalid_state :
out = run_commands ( module , [ ' show management api http-commands | json ' ] )
for index , item in enumerate ( invalid_state ) :
want_key , eapi_key = item
if want [ want_key ] is not None :
if want [ want_key ] == out [ 0 ] [ eapi_key ] [ ' running ' ] :
del invalid_state [ index ]
elif state == ' stopped ' :
if not out [ 0 ] [ eapi_key ] [ ' running ' ] :
del invalid_state [ index ]
else :
del invalid_state [ index ]
time . sleep ( 1 )
timeout - = 1
if timeout == 0 :
module . fail_json ( msg = ' timeout expired before eapi running state changed ' )
def collect_facts ( module , result ) :
out = run_commands ( module , [ ' show management api http-commands | json ' ] )
facts = dict ( eos_eapi_urls = dict ( ) )
@ -318,6 +363,7 @@ def main():
vrf = dict ( default = ' default ' ) ,
config = dict ( ) ,
state = dict ( default = ' started ' , choices = [ ' stopped ' , ' started ' ] ) ,
)
@ -326,12 +372,17 @@ def main():
module = AnsibleModule ( argument_spec = argument_spec ,
supports_check_mode = True )
result = { ' changed ' : False }
warnings = list ( )
if module . params [ ' config ' ] :
warnings . append ( ' config parameter is no longer necessary and will be ignored ' )
want = map_params_to_obj ( module )
have = map_config_to_obj ( module )
commands = map_obj_to_commands ( ( want , have ) , module )
commands = map_obj_to_commands ( ( want , have ) , module , warnings )
result [ ' commands ' ] = commands
if commands :
@ -342,8 +393,14 @@ def main():
result [ ' session_name ' ] = response . get ( ' session ' )
result [ ' changed ' ] = True
if result [ ' changed ' ] :
verify_state ( ( want , have ) , module )
collect_facts ( module , result )
if warnings :
result [ ' warnings ' ] = warnings
module . exit_json ( * * result )