From 726ab792534bc27e9a671e80ec35817bd692f996 Mon Sep 17 00:00:00 2001 From: Orion Poplawski Date: Mon, 20 May 2024 14:17:13 -0600 Subject: [PATCH] Handle condition going from privileged to non-privileged user in which the cwd disables descent. Fall back to Mitogen's temporary directory. Fixes #636. --- ansible_mitogen/runner.py | 9 +++++++-- docs/changelog.rst | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index 8da1b6705..fbd2eff20 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -383,10 +383,15 @@ def _setup_cwd(self): """ For situations like sudo to a non-privileged account, CWD could be $HOME of the old account, which could have mode go=, which means it is - impossible to restore the old directory, so don't even try. + impossible to restore the old directory. Fallback to a neutral temp if so. """ if self.cwd: - os.chdir(self.cwd) + try: + os.chdir(self.cwd) + except OSError: + LOG.debug('%r: could not CHDIR to %r fallback to %r', + self, self.cwd, self.good_temp_dir) + os.chdir(self.good_temp_dir) def _setup_environ(self): """ diff --git a/docs/changelog.rst b/docs/changelog.rst index 5d77910e8..53c1d6c81 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -21,6 +21,7 @@ To avail of fixes in an unreleased version, please download a ZIP file Unreleased ---------- +* :gh:issue:`636` os.chdir fails if the sudo/become user lacks adequate permissions to chdir prior to task * :gh:issue:`952` Fix Ansible `--ask-become-pass`, add test coverage * :gh:issue:`957` Fix Ansible exception when executing against 10s of hosts "ValueError: filedescriptor out of range in select()"