diff --git a/changelogs/fragments/home_translation.yml b/changelogs/fragments/home_translation.yml new file mode 100644 index 00000000000..f22d124080b --- /dev/null +++ b/changelogs/fragments/home_translation.yml @@ -0,0 +1,2 @@ +bugfixes: + - correctly deal with user homedir (~) translations https://github.com/ansible/ansible/pull/36755 diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 3d7d94c1c37..c5cacfe36fa 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -567,8 +567,12 @@ class ActionBase(with_metaclass(ABCMeta, object)): split_path = path.split(os.path.sep, 1) expand_path = split_path[0] - if sudoable and expand_path == '~' and self._play_context.become and self._play_context.become_user: - expand_path = '~%s' % self._play_context.become_user + if expand_path == '~': + if sudoable and self._play_context.become and self._play_context.become_user: + expand_path = '~%s' % self._play_context.become_user + else: + # use remote user instead, if none set default to current user + expand_path = '~%s' % self._play_context.remote_user or self._connection.default_user or '' # use shell to construct appropriate command and execute cmd = self._connection._shell.expand_user(expand_path) diff --git a/lib/ansible/plugins/connection/__init__.py b/lib/ansible/plugins/connection/__init__.py index 0e77f9722fd..536925abfc9 100644 --- a/lib/ansible/plugins/connection/__init__.py +++ b/lib/ansible/plugins/connection/__init__.py @@ -60,6 +60,8 @@ class ConnectionBase(AnsiblePlugin): supports_persistence = False force_persistence = False + default_user = None + def __init__(self, play_context, new_stdin, shell=None, *args, **kwargs): super(ConnectionBase, self).__init__() diff --git a/lib/ansible/plugins/connection/chroot.py b/lib/ansible/plugins/connection/chroot.py index e0b5a1ed5dd..abf0d178a33 100644 --- a/lib/ansible/plugins/connection/chroot.py +++ b/lib/ansible/plugins/connection/chroot.py @@ -65,6 +65,8 @@ class Connection(ConnectionBase): # Have to look into that before re-enabling this become_methods = frozenset(C.BECOME_METHODS).difference(('su',)) + default_user = 'root' + def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) diff --git a/lib/ansible/plugins/connection/libvirt_lxc.py b/lib/ansible/plugins/connection/libvirt_lxc.py index 0cc8940bb7b..c731e71ad1c 100644 --- a/lib/ansible/plugins/connection/libvirt_lxc.py +++ b/lib/ansible/plugins/connection/libvirt_lxc.py @@ -54,6 +54,7 @@ class Connection(ConnectionBase): # checksums (so copy, for instance, doesn't work right) # Have to look into that before re-enabling this become_methods = frozenset(C.BECOME_METHODS).difference(('su',)) + default_user = 'root' def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) diff --git a/lib/ansible/plugins/connection/lxc.py b/lib/ansible/plugins/connection/lxc.py index a8af7785e98..5219b28d680 100644 --- a/lib/ansible/plugins/connection/lxc.py +++ b/lib/ansible/plugins/connection/lxc.py @@ -55,6 +55,7 @@ class Connection(ConnectionBase): transport = 'lxc' has_pipelining = True become_methods = frozenset(C.BECOME_METHODS) + default_user = 'root' def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) diff --git a/lib/ansible/plugins/connection/lxd.py b/lib/ansible/plugins/connection/lxd.py index 9cc8b8929cd..84d32b9b272 100644 --- a/lib/ansible/plugins/connection/lxd.py +++ b/lib/ansible/plugins/connection/lxd.py @@ -43,6 +43,7 @@ class Connection(ConnectionBase): transport = "lxd" has_pipelining = True + default_user = 'root' def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)