From 4184d9665ed8909b51d511804c68b58d2ceff260 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Thu, 6 Nov 2025 17:57:23 -0800 Subject: [PATCH] Fix getuser fallback error handling (#86144) --- changelogs/fragments/getuser-exception-handling.yml | 4 ++++ lib/ansible/plugins/connection/local.py | 3 ++- lib/ansible/utils/display.py | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/getuser-exception-handling.yml diff --git a/changelogs/fragments/getuser-exception-handling.yml b/changelogs/fragments/getuser-exception-handling.yml new file mode 100644 index 00000000000..cdba2b3c40d --- /dev/null +++ b/changelogs/fragments/getuser-exception-handling.yml @@ -0,0 +1,4 @@ +bugfixes: + - display - Fix ``getuser`` fallback error handling on Python 3.13 and later. + (https://github.com/ansible/ansible/issues/86142) + - local connection - Fix ``getuser`` fallback error handling on Python 3.13 and later. diff --git a/lib/ansible/plugins/connection/local.py b/lib/ansible/plugins/connection/local.py index 6b7581a2f45..6fd1ea41fe1 100644 --- a/lib/ansible/plugins/connection/local.py +++ b/lib/ansible/plugins/connection/local.py @@ -67,7 +67,8 @@ class Connection(ConnectionBase): self.cwd = None try: self.default_user = getpass.getuser() - except KeyError: + except (ImportError, KeyError, OSError): + # deprecated: description='only OSError is required for Python 3.13+' python_version='3.12' display.vv("Current user (uid=%s) does not seem to exist on this system, leaving user empty." % os.getuid()) self.default_user = "" diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index 717703b840a..750ba84e3cc 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -174,7 +174,8 @@ class FilterUserInjector(logging.Filter): try: username = getpass.getuser() - except KeyError: + except (ImportError, KeyError, OSError): + # deprecated: description='only OSError is required for Python 3.13+' python_version='3.12' # people like to make containers w/o actual valid passwd/shadow and use host uids username = 'uid=%s' % os.getuid()