@ -90,6 +90,11 @@ class NetworkModule(AnsibleModule):
super ( NetworkModule , self ) . __init__ ( * args , * * kwargs )
super ( NetworkModule , self ) . __init__ ( * args , * * kwargs )
self . connection = None
self . connection = None
self . _config = None
self . _config = None
self . _connected = False
@property
def connected ( self ) :
return self . _connected
@property
@property
def config ( self ) :
def config ( self ) :
@ -109,10 +114,10 @@ class NetworkModule(AnsibleModule):
try :
try :
self . connection = Cli ( self )
self . connection = Cli ( self )
self . connection . connect ( )
self . connection . connect ( )
self . execute ( ' terminal length 0 ' )
self . connection . send ( ' terminal length 0 ' )
if self . params [ ' authorize ' ] :
if self . params [ ' authorize ' ] :
self . connection . authorize ( )
self . connection . authorize ( )
self . _connected = True
except Exception , exc :
except Exception , exc :
self . fail_json ( msg = exc . message )
self . fail_json ( msg = exc . message )
@ -126,6 +131,8 @@ class NetworkModule(AnsibleModule):
def execute ( self , commands , * * kwargs ) :
def execute ( self , commands , * * kwargs ) :
try :
try :
if not self . connected :
self . connect ( )
return self . connection . send ( commands , * * kwargs )
return self . connection . send ( commands , * * kwargs )
except ShellError , exc :
except ShellError , exc :
self . fail_json ( msg = exc . message , command = exc . command )
self . fail_json ( msg = exc . message , command = exc . command )
@ -134,6 +141,7 @@ class NetworkModule(AnsibleModule):
def disconnect ( self ) :
def disconnect ( self ) :
self . connection . close ( )
self . connection . close ( )
self . _connected = False
def parse_config ( self , cfg ) :
def parse_config ( self , cfg ) :
return parse ( cfg , indent = 1 )
return parse ( cfg , indent = 1 )
@ -159,6 +167,5 @@ def get_module(**kwargs):
if not HAS_PARAMIKO :
if not HAS_PARAMIKO :
module . fail_json ( msg = ' paramiko is required but does not appear to be installed ' )
module . fail_json ( msg = ' paramiko is required but does not appear to be installed ' )
module . connect ( )
return module
return module