From cca084c89d4e0390fdad8a44d1d21c7201bb1f37 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Mon, 4 Apr 2016 21:50:59 -0400 Subject: [PATCH] enhancement to ios shared module connection this enhancement will cause the module to connect to the remote ios device the first time a command wants to run instead of building a connection immediately --- lib/ansible/module_utils/ios.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/ios.py b/lib/ansible/module_utils/ios.py index ab194a7696f..29c5e92ddf0 100644 --- a/lib/ansible/module_utils/ios.py +++ b/lib/ansible/module_utils/ios.py @@ -90,6 +90,11 @@ class NetworkModule(AnsibleModule): super(NetworkModule, self).__init__(*args, **kwargs) self.connection = None self._config = None + self._connected = False + + @property + def connected(self): + return self._connected @property def config(self): @@ -109,10 +114,10 @@ class NetworkModule(AnsibleModule): try: self.connection = Cli(self) self.connection.connect() - self.execute('terminal length 0') - + self.connection.send('terminal length 0') if self.params['authorize']: self.connection.authorize() + self._connected = True except Exception, exc: self.fail_json(msg=exc.message) @@ -126,6 +131,8 @@ class NetworkModule(AnsibleModule): def execute(self, commands, **kwargs): try: + if not self.connected: + self.connect() return self.connection.send(commands, **kwargs) except ShellError, exc: self.fail_json(msg=exc.message, command=exc.command) @@ -134,6 +141,7 @@ class NetworkModule(AnsibleModule): def disconnect(self): self.connection.close() + self._connected = False def parse_config(self, cfg): return parse(cfg, indent=1) @@ -159,6 +167,5 @@ def get_module(**kwargs): if not HAS_PARAMIKO: module.fail_json(msg='paramiko is required but does not appear to be installed') - module.connect() return module