From 00c023ed3c8a925bee71a0476d8aaf9a4b8b81b8 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 11 Apr 2018 11:53:05 -0400 Subject: [PATCH] ensure 'home' translations (#36755) * ensure 'home' translations * removed slash as it created issues on diff plats (cherry picked from commit cc1c7c63db77b38f7a5276f61a7f7897ce6217f4) * fixed bug introduced by orig commit, this should only affect single ~ paths --- changelogs/fragments/home_translation.yml | 2 ++ lib/ansible/plugins/action/__init__.py | 8 ++++++-- lib/ansible/plugins/connection/__init__.py | 2 ++ lib/ansible/plugins/connection/chroot.py | 2 ++ lib/ansible/plugins/connection/libvirt_lxc.py | 1 + lib/ansible/plugins/connection/lxc.py | 1 + lib/ansible/plugins/connection/lxd.py | 1 + 7 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/home_translation.yml 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)