From eb1141ee799fd8da80052c2e9d5722afab9f1682 Mon Sep 17 00:00:00 2001 From: Chris Lamb Date: Thu, 3 Mar 2016 19:21:06 +0000 Subject: [PATCH] Ignore EPIPE to avoid tracebacks when piping output to other commands For example: $ ansible web --list-hosts | head -n1 hosts (7): ERROR! Unexpected Exception: [Errno 32] Broken pipe Traceback (most recent call last): File "/home/lamby/git/private/lamby-ansible2/.venv/bin/ansible", line 114, in display.display("to see the full traceback, use -vvv") File "/home/lamby/git/private/lamby-ansible2/.venv/local/lib/python2.7/site-packages/ansible/utils/display.py", line 133, in display sys.stdout.flush() IOError: [Errno 32] Broken pipe Such a pipe target will close up shop early when its seen enough input, causing ansible to print an ugly traceback. Signed-off-by: Chris Lamb --- lib/ansible/utils/display.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index 0031e2c369..5d056e5d22 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -28,6 +28,7 @@ import time import locale import logging import getpass +import errno from struct import unpack, pack from termios import TIOCGWINSZ from multiprocessing import Lock @@ -134,7 +135,14 @@ class Display: fileobj = sys.stderr fileobj.write(msg2) - fileobj.flush() + + try: + fileobj.flush() + except IOError as e: + # Ignore EPIPE in case fileobj has been prematurely closed, eg. + # when piping to "head -n1" + if e.errno != errno.EPIPE: + raise if logger and not screen_only: msg2 = nocolor.lstrip(u'\n')