mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-30 21:09:09 -07:00
Use a decorator to ensure jit connection, instead of an explicit call to _connect
This commit is contained in:
parent
f7839dee11
commit
9754c67138
5 changed files with 27 additions and 6 deletions
|
@ -42,10 +42,11 @@ except ImportError:
|
|||
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound
|
||||
from ansible.plugins.connections import ConnectionBase
|
||||
from ansible.plugins.connections import ConnectionBase, ensure_connect
|
||||
from ansible.plugins import shell_loader
|
||||
from ansible.utils.path import makedirs_safe
|
||||
|
||||
|
||||
class Connection(ConnectionBase):
|
||||
'''WinRM connections over HTTP/HTTPS.'''
|
||||
|
||||
|
@ -151,6 +152,7 @@ class Connection(ConnectionBase):
|
|||
self.protocol = self._winrm_connect()
|
||||
return self
|
||||
|
||||
@ensure_connect
|
||||
def exec_command(self, cmd, tmp_path, executable='/bin/sh', in_data=None):
|
||||
|
||||
cmd = cmd.encode('utf-8')
|
||||
|
@ -172,6 +174,7 @@ class Connection(ConnectionBase):
|
|||
raise AnsibleError("failed to exec cmd %s" % cmd)
|
||||
return (result.status_code, '', result.std_out.encode('utf-8'), result.std_err.encode('utf-8'))
|
||||
|
||||
@ensure_connect
|
||||
def put_file(self, in_path, out_path):
|
||||
self._display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._connection_info.remote_addr)
|
||||
if not os.path.exists(in_path):
|
||||
|
@ -210,6 +213,7 @@ class Connection(ConnectionBase):
|
|||
traceback.print_exc()
|
||||
raise AnsibleError("failed to transfer file to %s" % out_path)
|
||||
|
||||
@ensure_connect
|
||||
def fetch_file(self, in_path, out_path):
|
||||
out_path = out_path.replace('\\', '/')
|
||||
self._display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self._connection_info.remote_addr)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue