diff --git a/changelogs/fragments/469-synchronize-ansible-host.yml b/changelogs/fragments/469-synchronize-ansible-host.yml new file mode 100644 index 0000000..2c9f66d --- /dev/null +++ b/changelogs/fragments/469-synchronize-ansible-host.yml @@ -0,0 +1,7 @@ +bugfixes: + - synchronize - fall back to ``ansible_host`` and ``ansible_password``/``ansible_ssh_pass`` + from the current host's task vars when absent from the cached ``hostvars`` + snapshot (for example when set via play-level ``vars:``), and correctly + template ``ansible_host`` before building the rsync destination, fixing + "Host not found" errors when ``ansible_host`` differs from + ``inventory_hostname`` (https://github.com/ansible-collections/ansible.posix/issues/469). diff --git a/plugins/action/synchronize.py b/plugins/action/synchronize.py index 8f857c0..37d0bbf 100644 --- a/plugins/action/synchronize.py +++ b/plugins/action/synchronize.py @@ -240,10 +240,17 @@ class ActionModule(ActionBase): src_host = '127.0.0.1' inventory_hostname = task_vars.get('inventory_hostname') dest_host_inventory_vars = task_vars['hostvars'].get(inventory_hostname) - dest_host = dest_host_inventory_vars.get('ansible_host', inventory_hostname) + # If the user did not set ansible_host, ansible fills it in with the address + # of the delegate host when delegating, so only use ansible_host when not delegating. + dest_host = ((task_vars.get('ansible_host') if delegate_to is None else None) + or dest_host_inventory_vars.get('ansible_host') + or inventory_hostname) + if self._templar is not None: + dest_host = self._templar.template(dest_host) dest_host_ids = [hostid for hostid in (dest_host_inventory_vars.get('inventory_hostname'), - dest_host_inventory_vars.get('ansible_host')) + dest_host_inventory_vars.get('ansible_host'), + dest_host) if hostid is not None] localhost_ports = set() @@ -371,7 +378,10 @@ class ActionModule(ActionBase): src = self._process_origin(src_host, src, user) dest = self._process_remote(_tmp_args, dest_host, dest, user, inv_port in localhost_ports) - password = dest_host_inventory_vars.get('ansible_ssh_pass', None) or dest_host_inventory_vars.get('ansible_password', None) + password = (task_vars.get('ansible_ssh_pass') + or task_vars.get('ansible_password') + or dest_host_inventory_vars.get('ansible_ssh_pass', None) + or dest_host_inventory_vars.get('ansible_password', None)) if self._templar is not None: password = self._templar.template(password) else: diff --git a/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/meta.yaml b/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/meta.yaml new file mode 100644 index 0000000..ce8cd72 --- /dev/null +++ b/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/meta.yaml @@ -0,0 +1,20 @@ +--- +# Test that ansible_host set via play-level vars: (present in task_vars but +# absent from the hostvars snapshot for the target host) is correctly used +# to build the rsync destination instead of falling back to inventory_hostname. +# Regression test for https://github.com/ansible-collections/ansible.posix/issues/469 +fixtures: + taskvars_in: taskvars_in.json + taskvars_out: taskvars_out.json +connection: + transport: ssh +hostvars: + 127.0.0.1: {} + ::1: {} + localhost: {} +asserts: + - hasattr(SAM._connection, 'ismock') + - self.execute_called + - self.final_module_args['_local_rsync_path'] == 'rsync' + - self.final_module_args['src'] == '/tmp/deleteme' + - self.final_module_args['dest'] == 'root@el6host.example.org:/tmp/deleteme' diff --git a/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/taskvars_in.json b/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/taskvars_in.json new file mode 100644 index 0000000..8ef2ec5 --- /dev/null +++ b/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/taskvars_in.json @@ -0,0 +1,11 @@ +{ + "inventory_hostname": "el6host", + "ansible_user": "root", + "ansible_host": "el6host.example.org", + "hostvars": { + "el6host": {} + }, + "vars": { + "ansible_host": "el6host.example.org" + } +} diff --git a/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/taskvars_out.json b/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/taskvars_out.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/unit/plugins/action/fixtures/synchronize/ansible_host_from_task_vars/taskvars_out.json @@ -0,0 +1 @@ +{} diff --git a/tests/unit/plugins/action/test_synchronize.py b/tests/unit/plugins/action/test_synchronize.py index 1c4a241..abf2918 100644 --- a/tests/unit/plugins/action/test_synchronize.py +++ b/tests/unit/plugins/action/test_synchronize.py @@ -55,6 +55,9 @@ class TaskMock(object): become_user = None become_method = None check_mode = False + delegate_to = None + diff = False + no_log = None class StdinMock(object): @@ -64,6 +67,10 @@ class StdinMock(object): class ConnectionMock(object): ismock = True _play_context = None + always_pipeline_modules = False + become = None + has_pipelining = False + socket_path = None # transport = 'ssh' transport = None _new_stdin = StdinMock() @@ -121,6 +128,9 @@ class SynchronizeTester(object): return {} def runtest(self, fixturepath='fixtures/synchronize/basic'): + # Reset task and connection to avoid state leaking between tests + self.task = TaskMock() + self.connection = ConnectionMock() metapath = os.path.join(fixturepath, 'meta.yaml') with open(metapath, 'rb') as f: @@ -214,6 +224,15 @@ class TestSynchronizeAction(unittest.TestCase): x = SynchronizeTester() x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic')) + @patch('ansible_collections.ansible.posix.plugins.action.synchronize.connection_loader', FakePluginLoader) + def test_ansible_host_from_task_vars(self): + # ansible_host set only via play-level vars (present in top-level + # task_vars, absent from the hostvars snapshot for the target host) + # must still be used to build the rsync destination. + # Regression test for https://github.com/ansible-collections/ansible.posix/issues/469 + x = SynchronizeTester() + x.runtest(fixturepath=os.path.join(self.fixturedir, 'ansible_host_from_task_vars')) + @patch('ansible_collections.ansible.posix.plugins.action.synchronize.connection_loader', FakePluginLoader) def test_basic_become(self): x = SynchronizeTester()