ACA-4558: synchronize: use ansible_host from play vars when absent from hostvars (#779) (#798)

* synchronize: use ansible_host from play vars when absent from hostvars

The synchronize action plugin built the rsync/ssh destination
exclusively from task_vars['hostvars'][inventory_hostname]. When
ansible_host was set via play-level vars: rather than inventory/
group_vars/host_vars, it was absent from that snapshot and the plugin
silently fell back to bare inventory_hostname, producing 'Host not
found' errors when ansible_host differed from the inventory name.

Fixes #469

(cherry picked from commit 2f2a8e9b96)

Co-authored-by: Susmita Sen <sussen@redhat.com>
This commit is contained in:
Hideki Saito
2026-09-17 07:45:56 +09:00
committed by GitHub
co-authored by Susmita Sen
parent 7f4388b0de
commit 8129a5e8d3
6 changed files with 71 additions and 3 deletions
@@ -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).
+13 -3
View File
@@ -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:
@@ -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'
@@ -0,0 +1,11 @@
{
"inventory_hostname": "el6host",
"ansible_user": "root",
"ansible_host": "el6host.example.org",
"hostvars": {
"el6host": {}
},
"vars": {
"ansible_host": "el6host.example.org"
}
}
@@ -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()