diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 65964dc955..b4f71b17a9 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -718,7 +718,14 @@ class ActionBase(with_metaclass(ABCMeta, object)): cmd = executable + ' -c ' + pipes.quote(cmd) display.debug("_low_level_execute_command(): executing: %s" % (cmd,)) - rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable) + + # Change directory to basedir of task for command execution + cwd = os.getcwd() + os.chdir(self._loader.get_basedir()) + try: + rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable) + finally: + os.chdir(cwd) # stdout and stderr may be either a file-like or a bytes object. # Convert either one to a text type diff --git a/lib/ansible/plugins/connection/local.py b/lib/ansible/plugins/connection/local.py index c66218b882..0c7bb5ccb7 100644 --- a/lib/ansible/plugins/connection/local.py +++ b/lib/ansible/plugins/connection/local.py @@ -69,7 +69,6 @@ class Connection(ConnectionBase): executable = C.DEFAULT_EXECUTABLE.split()[0] if C.DEFAULT_EXECUTABLE else None display.vvv(u"EXEC {0}".format(cmd), host=self._play_context.remote_addr) - # FIXME: cwd= needs to be set to the basedir of the playbook display.debug("opening command with Popen()") if isinstance(cmd, (text_type, binary_type)): diff --git a/test/units/plugins/action/test_action.py b/test/units/plugins/action/test_action.py index 44358cf22d..8a634bf604 100644 --- a/test/units/plugins/action/test_action.py +++ b/test/units/plugins/action/test_action.py @@ -607,8 +607,10 @@ class TestActionBase(unittest.TestCase): self.assertRaises(AnsibleError, action_base._execute_module) def test_action_base_sudo_only_if_user_differs(self): + fake_loader = MagicMock() + fake_loader.get_basedir.return_value = os.getcwd() play_context = PlayContext() - action_base = DerivedActionBase(None, None, play_context, None, None, None) + action_base = DerivedActionBase(None, None, play_context, fake_loader, None, None) action_base._connection = MagicMock(exec_command=MagicMock(return_value=(0, '', ''))) action_base._connection._shell = MagicMock(append_command=MagicMock(return_value=('JOINED CMD')))