From 54859a2132cc08bb748c1a532495b9152a36d90a Mon Sep 17 00:00:00 2001 From: Jonathan Piron Date: Wed, 27 Sep 2017 20:58:02 +0200 Subject: [PATCH] Fix crypttab python3 compatibility issue (#30457) In python2 str gives byte string. In Python3 it gives unicode string so it can't be written in a binary mode opened file. Use to_bytes helper function to ensure content being written will be properly encoded in both python2 and python3. --- lib/ansible/modules/system/crypttab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/system/crypttab.py b/lib/ansible/modules/system/crypttab.py index db15dc8f517..ef6bb8e1044 100644 --- a/lib/ansible/modules/system/crypttab.py +++ b/lib/ansible/modules/system/crypttab.py @@ -89,7 +89,7 @@ import os import traceback from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils._text import to_native +from ansible.module_utils._text import to_bytes, to_native def main(): @@ -169,7 +169,7 @@ def main(): if changed and not module.check_mode: try: f = open(path, 'wb') - f.write(str(crypttab)) + f.write(to_bytes(crypttab, errors='surrogate_or_strict')) finally: f.close()