From 5f3a6b78db093f8d1b062bbd70ac6bf375fdca04 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 29 Nov 2022 10:08:32 -0500 Subject: [PATCH] local connection: avoid tb when running in container with invalid user (#79414) * local connection: avoid tb when running in container with invalid user * clog * cannot use uid, leave empty and ~/ will resolve itself * get back to what it did --- changelogs/fragments/local_bad_user.yml | 2 ++ lib/ansible/plugins/connection/local.py | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/local_bad_user.yml diff --git a/changelogs/fragments/local_bad_user.yml b/changelogs/fragments/local_bad_user.yml new file mode 100644 index 00000000000..af336a63f93 --- /dev/null +++ b/changelogs/fragments/local_bad_user.yml @@ -0,0 +1,2 @@ +bugfixes: + - connection local now avoids traceback on invalid user being used to execuet ansible (valid in host, but not in container). diff --git a/lib/ansible/plugins/connection/local.py b/lib/ansible/plugins/connection/local.py index 182e21cd7df..27afd105c81 100644 --- a/lib/ansible/plugins/connection/local.py +++ b/lib/ansible/plugins/connection/local.py @@ -18,12 +18,12 @@ DOCUMENTATION = ''' - The remote user is ignored, the user with which the ansible CLI was executed is used instead. ''' +import fcntl +import getpass import os import pty import shutil import subprocess -import fcntl -import getpass import ansible.constants as C from ansible.errors import AnsibleError, AnsibleFileNotFound @@ -47,7 +47,11 @@ class Connection(ConnectionBase): super(Connection, self).__init__(*args, **kwargs) self.cwd = None - self.default_user = getpass.getuser() + try: + self.default_user = getpass.getuser() + except KeyError: + display.vv("Current user (uid=%s) does not seem to exist on this system, leaving user empty." % os.getuid()) + self.default_user = "" def _connect(self): ''' connect to the local host; nothing to do here '''