@ -30,6 +30,7 @@
import os
import time
from ansible . module_utils . _text import to_text , to_native
from ansible . module_utils . basic import env_fallback , return_values
from ansible . module_utils . connection import exec_command
from ansible . module_utils . network_common import to_list , ComplexList
@ -64,18 +65,17 @@ ARGS_DEFAULT_VALUE = {
' validate_certs ' : True
}
def check_args ( module , warnings ) :
provider = module . params [ ' provider ' ] or { }
for key in eos_argument_spec :
if module . _name == ' eos_user ' :
if ( key not in [ ' username ' , ' password ' , ' provider ' , ' transport ' , ' authorize ' ] and
module . params [ key ] ) :
warnings . append ( ' argument %s has been deprecated and will be '
' removed in a future version ' % key )
warnings . append ( ' argument %s has been deprecated and will be removed in a future version ' % key )
else :
if key not in [ ' provider ' , ' authorize ' ] and module . params [ key ] :
warnings . append ( ' argument %s has been deprecated and will be '
' removed in a future version ' % key )
warnings . append ( ' argument %s has been deprecated and will be removed in a future version ' % key )
# set argument's default value if not provided in input
# This is done to avoid unwanted argument deprecation warning
@ -89,6 +89,7 @@ def check_args(module, warnings):
if provider . get ( param ) :
module . no_log_values . update ( return_values ( provider [ param ] ) )
def load_params ( module ) :
provider = module . params . get ( ' provider ' ) or dict ( )
for key , value in iteritems ( provider ) :
@ -96,6 +97,7 @@ def load_params(module):
if module . params . get ( key ) is None and value is not None :
module . params [ key ] = value
def get_connection ( module ) :
global _DEVICE_CONNECTION
if not _DEVICE_CONNECTION :
@ -131,6 +133,7 @@ class Cli:
def check_authorization ( self ) :
for cmd in [ ' show clock ' , ' prompt() ' ] :
rc , out , err = self . exec_command ( cmd )
out = to_text ( out , errors = ' surrogate_then_replace ' )
return out . endswith ( ' # ' )
def get_config ( self , flags = [ ] ) :
@ -145,8 +148,9 @@ class Cli:
except KeyError :
conn = get_connection ( self )
rc , out , err = self . exec_command ( cmd )
out = to_text ( out , errors = ' surrogate_then_replace ' )
if rc != 0 :
self . _module . fail_json ( msg = err)
self . _module . fail_json ( msg = to_text( err, errors = ' surrogate_then_replace ' ) )
cfg = str ( out ) . strip ( )
self . _device_configs [ cmd ] = cfg
return cfg
@ -158,9 +162,9 @@ class Cli:
for cmd in to_list ( commands ) :
rc , out , err = self . exec_command ( cmd )
out = to_text ( out , errors = ' surrogate_then_replace ' )
if check_rc and rc != 0 :
self . _module . fail_json ( msg = err)
self . _module . fail_json ( msg = to_text( err, errors = ' surrogate_then_replace ' ) )
try :
out = self . _module . from_json ( out )
@ -185,9 +189,9 @@ class Cli:
rc , out , err = self . exec_command ( command )
if rc != 0 :
return ( rc , out , err)
return ( rc , out , to_text( err, errors = ' surrogate_then_replace ' ) )
return ( rc , ' ok ' , ' ' )
return ( rc , ' ok ' , ' ' )
def configure ( self , commands ) :
""" Sends configuration commands to the remote device
@ -199,11 +203,11 @@ class Cli:
rc , out , err = self . exec_command ( ' configure ' )
if rc != 0 :
self . _module . fail_json ( msg = ' unable to enter configuration mode ' , output = err)
self . _module . fail_json ( msg = ' unable to enter configuration mode ' , output = to_text( err, errors = ' surrogate_then_replace ' ) )
rc , out , err = self . send_config ( commands )
if rc != 0 :
self . _module . fail_json ( msg = err)
self . _module . fail_json ( msg = to_text( err, errors = ' surrogate_then_replace ' ) )
self . exec_command ( ' end ' )
return { }
@ -221,7 +225,7 @@ class Cli:
pass
if not all ( ( bool ( use_session ) , self . supports_sessions ) ) :
return configure ( self , commands )
return self . configure ( self , commands )
conn = get_connection ( self )
session = ' ansible_ %s ' % int ( time . time ( ) )
@ -229,7 +233,7 @@ class Cli:
rc , out , err = self . exec_command ( ' configure session %s ' % session )
if rc != 0 :
self . _module . fail_json ( msg = ' unable to enter configuration mode ' , output = err)
self . _module . fail_json ( msg = ' unable to enter configuration mode ' , output = to_text( err, errors = ' surrogate_then_replace ' ) )
if replace :
self . exec_command ( ' rollback clean-config ' , check_rc = True )
@ -237,11 +241,11 @@ class Cli:
rc , out , err = self . send_config ( commands )
if rc != 0 :
self . exec_command ( ' abort ' )
self . _module . fail_json ( msg = err, commands = commands )
self . _module . fail_json ( msg = to_text( err, errors = ' surrogate_then_replace ' ) , commands = commands )
rc , out , err = self . exec_command ( ' show session-config diffs ' )
if rc == 0 and out :
result [ ' diff ' ] = out. strip ( )
result [ ' diff ' ] = to_text( out, errors = ' surrogate_then_replace ' ) . strip ( )
if commit :
self . exec_command ( ' commit ' )
@ -250,13 +254,14 @@ class Cli:
return result
class Eapi :
def __init__ ( self , module ) :
self . _module = module
self . _enable = None
self . _session_support = None
self . _device_configs = { }
self . _device_configs = { }
host = module . params [ ' provider ' ] [ ' host ' ]
port = module . params [ ' provider ' ] [ ' port ' ]
@ -312,7 +317,7 @@ class Eapi:
try :
data = response . read ( )
response = self . _module . from_json ( data)
response = self . _module . from_json ( to_text( data, errors = ' surrogate_then_replace ' ) )
except ValueError :
self . _module . fail_json ( msg = ' unable to load response from device ' , data = data )
@ -394,7 +399,7 @@ class Eapi:
there will be no returned diff or session values
"""
if not self . supports_sessions :
return configure ( self , co mmands )
return self . configure ( self , co nfig )
session = ' ansible_ %s ' % int ( time . time ( ) )
result = { ' session ' : session }
@ -425,16 +430,17 @@ class Eapi:
return result
is_json = lambda x : str ( x ) . endswith ( ' | json ' )
is_text = lambda x : not str ( x ) . endswith ( ' | json ' )
supports_sessions = lambda x : get_connection ( module ) . supports_sessions
def is_json ( cmd ) :
return to_native ( cmd , errors = ' surrogate_then_replace ' ) . endswith ( ' | json ' )
def is_eapi ( module ) :
transport = module . params [ ' transport ' ]
provider_transport = ( module . params [ ' provider ' ] or { } ) . get ( ' transport ' )
return ' eapi ' in ( transport , provider_transport )
def to_command ( module , commands ) :
if is_eapi ( module ) :
default_output = ' json '
@ -450,15 +456,17 @@ def to_command(module, commands):
return transform ( to_list ( commands ) )
def get_config ( module , flags = [ ] ) :
conn = get_connection ( module )
return conn . get_config ( flags )
def run_commands ( module , commands ) :
conn = get_connection ( module )
return conn . run_commands ( to_command ( module , commands ) )
def load_config ( module , config , commit = False , replace = False ) :
conn = get_connection ( module )
return conn . load_config ( config , commit , replace )