@ -23,7 +23,7 @@ NET_COMMON_ARGS = dict(
port = dict ( type = ' int ' ) ,
port = dict ( type = ' int ' ) ,
username = dict ( required = True ) ,
username = dict ( required = True ) ,
password = dict ( no_log = True ) ,
password = dict ( no_log = True ) ,
transport = dict ( choices= [ ' cli ' , ' nxapi ' ] ) ,
transport = dict ( default= ' cli ' , choices= [ ' cli ' , ' nxapi ' ] ) ,
use_ssl = dict ( default = False , type = ' bool ' ) ,
use_ssl = dict ( default = False , type = ' bool ' ) ,
provider = dict ( )
provider = dict ( )
)
)
@ -107,11 +107,24 @@ class Nxapi(object):
self . module . fail_json ( * * headers )
self . module . fail_json ( * * headers )
response = self . module . from_json ( response . read ( ) )
response = self . module . from_json ( response . read ( ) )
if ' error ' in response :
result = list ( )
err = response [ ' error ' ]
self . module . fail_json ( msg = ' json-rpc error % ' % str ( err ) )
output = response [ ' ins_api ' ] [ ' outputs ' ] [ ' output ' ]
if isinstance ( output , list ) :
for item in response [ ' ins_api ' ] [ ' outputs ' ] [ ' output ' ] :
if item [ ' code ' ] != ' 200 ' :
self . module . fail_json ( msg = item [ ' msg ' ] , command = item [ ' input ' ] ,
code = item [ ' code ' ] )
else :
result . append ( item [ ' body ' ] )
elif output [ ' code ' ] != ' 200 ' :
self . module . fail_json ( msg = item [ ' msg ' ] , command = item [ ' input ' ] ,
code = item [ ' code ' ] )
else :
result . append ( output [ ' body ' ] )
return response
return res ult
class Cli ( object ) :
class Cli ( object ) :
@ -150,7 +163,8 @@ class NetworkModule(AnsibleModule):
provider = params . get ( ' provider ' ) or dict ( )
provider = params . get ( ' provider ' ) or dict ( )
for key , value in provider . items ( ) :
for key , value in provider . items ( ) :
if key in NET_COMMON_ARGS . keys ( ) :
if key in NET_COMMON_ARGS . keys ( ) :
params [ key ] = value
if not params . get ( key ) and value is not None :
params [ key ] = value
return params
return params
def connect ( self ) :
def connect ( self ) :
@ -159,11 +173,9 @@ class NetworkModule(AnsibleModule):
else :
else :
self . connection = Cli ( self )
self . connection = Cli ( self )
try :
self . connection . connect ( )
self . connection . connect ( )
if self . params [ ' transport ' ] == ' cli ' :
self . execute ( ' terminal length 0 ' )
self . execute ( ' terminal length 0 ' )
except Exception , exc :
self . fail_json ( msg = exc . message )
def configure ( self , commands ) :
def configure ( self , commands ) :
commands = to_list ( commands )
commands = to_list ( commands )
@ -176,10 +188,7 @@ class NetworkModule(AnsibleModule):
return responses
return responses
def execute ( self , commands , * * kwargs ) :
def execute ( self , commands , * * kwargs ) :
try :
return self . connection . send ( commands , * * kwargs )
return self . connection . send ( commands , * * kwargs )
except Exception , exc :
self . fail_json ( msg = exc . message )
def disconnect ( self ) :
def disconnect ( self ) :
self . connection . close ( )
self . connection . close ( )
@ -194,10 +203,7 @@ class NetworkModule(AnsibleModule):
if self . params [ ' transport ' ] == ' cli ' :
if self . params [ ' transport ' ] == ' cli ' :
return self . execute ( cmd ) [ 0 ]
return self . execute ( cmd ) [ 0 ]
else :
else :
resp = self . execute ( cmd )
return self . execute ( cmd )
if not resp . get ( ' ins_api ' ) . get ( ' outputs ' ) . get ( ' output ' ) . get ( ' body ' ) :
self . fail_json ( msg = " Unrecognized response: %s " % str ( resp ) )
return resp [ ' ins_api ' ] [ ' outputs ' ] [ ' output ' ] [ ' body ' ]
def get_module ( * * kwargs ) :
def get_module ( * * kwargs ) :
""" Return instance of NetworkModule
""" Return instance of NetworkModule