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.
(cherry picked from commit 54859a2132cc08bb748c1a532495b9152a36d90a)
... | ... |
@@ -89,7 +89,7 @@ import os |
89 | 89 |
import traceback |
90 | 90 |
|
91 | 91 |
from ansible.module_utils.basic import AnsibleModule |
92 |
-from ansible.module_utils._text import to_native |
|
92 |
+from ansible.module_utils._text import to_bytes, to_native |
|
93 | 93 |
|
94 | 94 |
def main(): |
95 | 95 |
|
... | ... |
@@ -169,7 +169,7 @@ def main(): |
169 | 169 |
if changed and not module.check_mode: |
170 | 170 |
try: |
171 | 171 |
f = open(path, 'wb') |
172 |
- f.write(str(crypttab)) |
|
172 |
+ f.write(to_bytes(crypttab, errors='surrogate_or_strict')) |
|
173 | 173 |
finally: |
174 | 174 |
f.close() |
175 | 175 |
|