From 959821f40e5906348b6ed295b45e1508657b4a68 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 30 May 2018 16:28:23 -0500 Subject: [PATCH] Backport #40833 for 2.5 - synchronize _remote_is_local (#40917) * Use _remote_is_local=True for local connection in synchronize (#40833) * All instances of local connection should use _remote_is_local=True. Fixes #40551 * Switch to instance attribute for synchronize * Add test that shows that synchronize _remote_is_local addresses tmpdir building (cherry picked from commit ad7ba91f75570152c538a48b9f7ab865943bdc62) * Add changelog entry for #40833 --- .../fragments/synchronize_remote_is_local.yaml | 2 ++ lib/ansible/plugins/action/synchronize.py | 3 +++ test/units/plugins/action/test_synchronize.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 changelogs/fragments/synchronize_remote_is_local.yaml diff --git a/changelogs/fragments/synchronize_remote_is_local.yaml b/changelogs/fragments/synchronize_remote_is_local.yaml new file mode 100644 index 0000000000..fd5d8e2acf --- /dev/null +++ b/changelogs/fragments/synchronize_remote_is_local.yaml @@ -0,0 +1,2 @@ +bugfixes: +- synchronize - Ensure the local connection created by synchronize uses _remote_is_local=True, which causes ActionBase to build a local tmpdir (https://github.com/ansible/ansible/pull/40833) diff --git a/lib/ansible/plugins/action/synchronize.py b/lib/ansible/plugins/action/synchronize.py index 54b053cb0e..2bb5b7c786 100644 --- a/lib/ansible/plugins/action/synchronize.py +++ b/lib/ansible/plugins/action/synchronize.py @@ -301,6 +301,9 @@ class ActionModule(ActionBase): new_connection = connection_loader.get('local', self._play_context, new_stdin) self._connection = new_connection + # Override _remote_is_local as an instance attribute specifically for the synchronize use case + # ensuring we set local tmpdir correctly + self._connection._remote_is_local = True self._override_module_replaced_vars(task_vars) # SWITCH SRC AND DEST HOST PER MODE diff --git a/test/units/plugins/action/test_synchronize.py b/test/units/plugins/action/test_synchronize.py index 5d5ea26a13..23e64e9149 100644 --- a/test/units/plugins/action/test_synchronize.py +++ b/test/units/plugins/action/test_synchronize.py @@ -42,6 +42,10 @@ with open('task_vars.json', 'wb') as f: ''' +class BreakPoint(Exception): + pass + + class TaskMock(object): args = {'src': u'/tmp/deleteme', 'dest': '/tmp/deleteme', @@ -246,3 +250,15 @@ class TestSynchronizeAction(unittest.TestCase): # delegate to other remote host with su enabled x = SynchronizeTester() x.runtest(fixturepath=os.path.join(self.fixturedir, 'delegate_remote_su')) + + @patch.object(ActionModule, '_low_level_execute_command', side_effect=BreakPoint) + @patch.object(ActionModule, '_remote_expand_user', side_effect=ActionModule._remote_expand_user, autospec=True) + def test_remote_user_not_in_local_tmpdir(self, spy_remote_expand_user, ll_ec): + x = SynchronizeTester() + SAM = ActionModule(x.task, x.connection, x._play_context, + x.loader, x.templar, x.shared_loader_obj) + try: + SAM.run(task_vars={'hostvars': {'foo': {}, 'localhost': {}}, 'inventory_hostname': 'foo'}) + except BreakPoint: + pass + self.assertEqual(spy_remote_expand_user.call_count, 0)