diff --git a/changelogs/fragments/fallback_uid.yml b/changelogs/fragments/fallback_uid.yml new file mode 100644 index 00000000000..75262930d77 --- /dev/null +++ b/changelogs/fragments/fallback_uid.yml @@ -0,0 +1,2 @@ +bugfixes: + - 'for those running uids for invalid users (containers), fallback to uid= when logging fixes #68007' diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index a24d2cd0d7a..6d47f5f5061 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -62,7 +62,12 @@ class FilterUserInjector(logging.Filter): This is a filter which injects the current user as the 'user' attribute on each record. We need to add this filter to all logger handlers so that 3rd party libraries won't print an exception due to user not being defined. """ - username = getpass.getuser() + + try: + username = getpass.getuser() + except KeyError: + # people like to make containers w/o actual valid passwd/shadow and use host uids + username = 'uid=%s' % os.getuid() def filter(self, record): record.user = FilterUserInjector.username