mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-11-25 02:13:41 +00:00
* 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:
co-authored by
Susmita Sen
parent
7f4388b0de
commit
8129a5e8d3
@@ -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).
|
||||||
@@ -240,10 +240,17 @@ class ActionModule(ActionBase):
|
|||||||
src_host = '127.0.0.1'
|
src_host = '127.0.0.1'
|
||||||
inventory_hostname = task_vars.get('inventory_hostname')
|
inventory_hostname = task_vars.get('inventory_hostname')
|
||||||
dest_host_inventory_vars = task_vars['hostvars'].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_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]
|
if hostid is not None]
|
||||||
|
|
||||||
localhost_ports = set()
|
localhost_ports = set()
|
||||||
@@ -371,7 +378,10 @@ class ActionModule(ActionBase):
|
|||||||
src = self._process_origin(src_host, src, user)
|
src = self._process_origin(src_host, src, user)
|
||||||
dest = self._process_remote(_tmp_args, dest_host, dest, user, inv_port in localhost_ports)
|
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:
|
if self._templar is not None:
|
||||||
password = self._templar.template(password)
|
password = self._templar.template(password)
|
||||||
else:
|
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'
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"inventory_hostname": "el6host",
|
||||||
|
"ansible_user": "root",
|
||||||
|
"ansible_host": "el6host.example.org",
|
||||||
|
"hostvars": {
|
||||||
|
"el6host": {}
|
||||||
|
},
|
||||||
|
"vars": {
|
||||||
|
"ansible_host": "el6host.example.org"
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
@@ -55,6 +55,9 @@ class TaskMock(object):
|
|||||||
become_user = None
|
become_user = None
|
||||||
become_method = None
|
become_method = None
|
||||||
check_mode = False
|
check_mode = False
|
||||||
|
delegate_to = None
|
||||||
|
diff = False
|
||||||
|
no_log = None
|
||||||
|
|
||||||
|
|
||||||
class StdinMock(object):
|
class StdinMock(object):
|
||||||
@@ -64,6 +67,10 @@ class StdinMock(object):
|
|||||||
class ConnectionMock(object):
|
class ConnectionMock(object):
|
||||||
ismock = True
|
ismock = True
|
||||||
_play_context = None
|
_play_context = None
|
||||||
|
always_pipeline_modules = False
|
||||||
|
become = None
|
||||||
|
has_pipelining = False
|
||||||
|
socket_path = None
|
||||||
# transport = 'ssh'
|
# transport = 'ssh'
|
||||||
transport = None
|
transport = None
|
||||||
_new_stdin = StdinMock()
|
_new_stdin = StdinMock()
|
||||||
@@ -121,6 +128,9 @@ class SynchronizeTester(object):
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
def runtest(self, fixturepath='fixtures/synchronize/basic'):
|
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')
|
metapath = os.path.join(fixturepath, 'meta.yaml')
|
||||||
with open(metapath, 'rb') as f:
|
with open(metapath, 'rb') as f:
|
||||||
@@ -214,6 +224,15 @@ class TestSynchronizeAction(unittest.TestCase):
|
|||||||
x = SynchronizeTester()
|
x = SynchronizeTester()
|
||||||
x.runtest(fixturepath=os.path.join(self.fixturedir, 'basic'))
|
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)
|
@patch('ansible_collections.ansible.posix.plugins.action.synchronize.connection_loader', FakePluginLoader)
|
||||||
def test_basic_become(self):
|
def test_basic_become(self):
|
||||||
x = SynchronizeTester()
|
x = SynchronizeTester()
|
||||||
|
|||||||
Reference in New Issue
Block a user