summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelogs/fragments/80258-defensive-display-non-utf8.yml3
-rw-r--r--lib/ansible/utils/display.py6
2 files changed, 9 insertions, 0 deletions
diff --git a/changelogs/fragments/80258-defensive-display-non-utf8.yml b/changelogs/fragments/80258-defensive-display-non-utf8.yml
new file mode 100644
index 0000000000..b99b287a66
--- /dev/null
+++ b/changelogs/fragments/80258-defensive-display-non-utf8.yml
@@ -0,0 +1,3 @@
+bugfixes:
+- Display - Defensively configure writing to stdout and stderr with the replace encoding error handler that will replace invalid characters
+ (https://github.com/ansible/ansible/issues/80258)
diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py
index e521f2a401..7d98ad47e9 100644
--- a/lib/ansible/utils/display.py
+++ b/lib/ansible/utils/display.py
@@ -228,6 +228,12 @@ class Display(metaclass=Singleton):
except Exception as ex:
self.warning(f"failed to patch stdout/stderr for fork-safety: {ex}")
+ try:
+ sys.stdout.reconfigure(errors='replace')
+ sys.stderr.reconfigure(errors='replace')
+ except Exception as ex:
+ self.warning(f"failed to reconfigure stdout/stderr with the replace error handler: {ex}")
+
def set_queue(self, queue):
"""Set the _final_q on Display, so that we know to proxy display over the queue
instead of directly writing to stdout/stderr from forks