mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 12:21:26 -07:00
enhancement to iosxr shared module to lazy connect to device
this update will only connect to the remote device when the first request is made to run a command instead of building the connection immediately
This commit is contained in:
parent
81a4df6206
commit
c2fac6c808
1 changed files with 10 additions and 2 deletions
|
@ -80,6 +80,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):
|
||||||
|
@ -99,7 +104,8 @@ 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')
|
||||||
|
self._connected = True
|
||||||
except Exception, exc:
|
except Exception, exc:
|
||||||
self.fail_json(msg=exc.message)
|
self.fail_json(msg=exc.message)
|
||||||
|
|
||||||
|
@ -114,12 +120,15 @@ 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)
|
return self.connection.send(commands)
|
||||||
except ShellError, exc:
|
except ShellError, exc:
|
||||||
self.fail_json(msg=exc.message, command=exc.command)
|
self.fail_json(msg=exc.message, command=exc.command)
|
||||||
|
|
||||||
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)
|
||||||
|
@ -140,6 +149,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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue