Handle byte/string conversion on read/write with text module_utils (#19188)

Fixes ansible/ansible-modules-core#5840, moved from ansible/ansible-modules-core#5847
This commit is contained in:
Evan Kaufman 2016-12-11 00:00:01 -08:00 committed by René Moser
parent 7a07a594f7
commit aaf13e07fe

View file

@ -22,6 +22,8 @@ import re
import os
import tempfile
from ansible.module_utils._text import to_text, to_bytes
ANSIBLE_METADATA = {'status': ['stableinterface'],
'supported_by': 'community',
'version': '1.0'}
@ -105,7 +107,7 @@ def write_changes(module,contents,dest):
tmpfd, tmpfile = tempfile.mkstemp()
f = os.fdopen(tmpfd,'wb')
f.write(contents)
f.write(to_bytes(contents))
f.close()
validate = module.params.get('validate', None)
@ -157,7 +159,7 @@ def main():
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
else:
f = open(dest, 'rb')
contents = f.read()
contents = to_text(f.read(), errors='surrogate_or_strict')
f.close()
mre = re.compile(params['regexp'], re.MULTILINE)