replace - better handling of file exceptions (#74686)

This commit is contained in:
David Shrewsbury 2021-05-13 17:00:22 -04:00 committed by GitHub
parent bab7acb1ad
commit 51b5659598
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- replace - better handling of file operation exceptions (https://github.com/ansible/ansible/pull/74686).

View file

@ -171,6 +171,7 @@ RETURN = r'''#'''
import os
import re
import tempfile
from traceback import format_exc
from ansible.module_utils._text import to_text, to_bytes
from ansible.module_utils.basic import AnsibleModule
@ -242,9 +243,12 @@ def main():
if not os.path.exists(path):
module.fail_json(rc=257, msg='Path %s does not exist !' % path)
else:
f = open(path, 'rb')
contents = to_text(f.read(), errors='surrogate_or_strict', encoding=encoding)
f.close()
try:
with open(path, 'rb') as f:
contents = to_text(f.read(), errors='surrogate_or_strict', encoding=encoding)
except (OSError, IOError) as e:
module.fail_json(msg='Unable to read the contents of %s: %s' % (path, to_text(e)),
exception=format_exc())
pattern = u''
if params['after'] and params['before']: