From 53b19611e11fb4ef40b89636a2b537af37021d0d Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 7 Apr 2017 10:20:18 -0700 Subject: [PATCH] Fix a traceback with python3 and diff output When retrieving file contents for diffing we need to get the contents as binary. Otherwise python3 will try to convert the file to text and fail with non-decodable contents. Fixes #23171 (cherry picked from commit 2fdb8e7f9043ca74898d08664b495aa8a2653d6b) --- lib/ansible/plugins/action/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index d9c61e2f29..bd66d52ca0 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -950,12 +950,12 @@ class ActionBase(with_metaclass(ABCMeta, object)): else: display.debug("Reading local copy of the file %s" % source) try: - src = open(source) - src_contents = src.read() + with open(source, 'rb') as src: + src_contents = src.read() except Exception as e: raise AnsibleError("Unexpected error while reading source (%s) for diff: %s " % (source, str(e))) - if "\x00" in src_contents: + if b"\x00" in src_contents: diff['src_binary'] = 1 else: diff['after_header'] = source