removal of chdir in controller (#57781)
* removal of chdir in controller also ensure fetch/put uses relative to task cwd
This commit is contained in:
parent
de87b25a45
commit
342d77b32d
3 changed files with 20 additions and 8 deletions
2
changelogs/fragments/remove_chdir.yml
Normal file
2
changelogs/fragments/remove_chdir.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- removed chdir from action plugins when using local connection, moved into plugin itself to avoid future issues with threads.
|
|
@ -1054,13 +1054,9 @@ class ActionBase(with_metaclass(ABCMeta, object)):
|
||||||
|
|
||||||
# Change directory to basedir of task for command execution when connection is local
|
# Change directory to basedir of task for command execution when connection is local
|
||||||
if self._connection.transport == 'local':
|
if self._connection.transport == 'local':
|
||||||
cwd = os.getcwd()
|
self._connection.cwd = to_bytes(self._loader.get_basedir(), errors='surrogate_or_strict')
|
||||||
os.chdir(to_bytes(self._loader.get_basedir()))
|
|
||||||
try:
|
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
||||||
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
|
||||||
finally:
|
|
||||||
if self._connection.transport == 'local':
|
|
||||||
os.chdir(cwd)
|
|
||||||
|
|
||||||
# stdout and stderr may be either a file-like or a bytes object.
|
# stdout and stderr may be either a file-like or a bytes object.
|
||||||
# Convert either one to a text type
|
# Convert either one to a text type
|
||||||
|
|
|
@ -39,6 +39,11 @@ class Connection(ConnectionBase):
|
||||||
transport = 'local'
|
transport = 'local'
|
||||||
has_pipelining = True
|
has_pipelining = True
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
|
super(Connection, self).__init__(*args, **kwargs)
|
||||||
|
self.cwd = None
|
||||||
|
|
||||||
def _connect(self):
|
def _connect(self):
|
||||||
''' connect to the local host; nothing to do here '''
|
''' connect to the local host; nothing to do here '''
|
||||||
|
|
||||||
|
@ -76,7 +81,8 @@ class Connection(ConnectionBase):
|
||||||
p = subprocess.Popen(
|
p = subprocess.Popen(
|
||||||
cmd,
|
cmd,
|
||||||
shell=isinstance(cmd, (text_type, binary_type)),
|
shell=isinstance(cmd, (text_type, binary_type)),
|
||||||
executable=executable, # cwd=...
|
executable=executable,
|
||||||
|
cwd=self.cwd,
|
||||||
stdin=subprocess.PIPE,
|
stdin=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
|
@ -123,11 +129,19 @@ class Connection(ConnectionBase):
|
||||||
display.debug("done with local.exec_command()")
|
display.debug("done with local.exec_command()")
|
||||||
return (p.returncode, stdout, stderr)
|
return (p.returncode, stdout, stderr)
|
||||||
|
|
||||||
|
def _ensure_abs(self, path):
|
||||||
|
if not os.path.isabs(path) and self.cwd is not None:
|
||||||
|
path = os.path.normpath(os.path.join(self.cwd, path))
|
||||||
|
return path
|
||||||
|
|
||||||
def put_file(self, in_path, out_path):
|
def put_file(self, in_path, out_path):
|
||||||
''' transfer a file from local to local '''
|
''' transfer a file from local to local '''
|
||||||
|
|
||||||
super(Connection, self).put_file(in_path, out_path)
|
super(Connection, self).put_file(in_path, out_path)
|
||||||
|
|
||||||
|
in_path = self._ensure_abs(in_path)
|
||||||
|
out_path = self._ensure_abs(out_path)
|
||||||
|
|
||||||
display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._play_context.remote_addr)
|
display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._play_context.remote_addr)
|
||||||
if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')):
|
if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')):
|
||||||
raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path)))
|
raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path)))
|
||||||
|
|
Loading…
Reference in a new issue