replace - better handling of file exceptions (#74686)
This commit is contained in:
parent
bab7acb1ad
commit
51b5659598
2 changed files with 9 additions and 3 deletions
2
changelogs/fragments/74686-replace-handle-file-exc.yml
Normal file
2
changelogs/fragments/74686-replace-handle-file-exc.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- replace - better handling of file operation exceptions (https://github.com/ansible/ansible/pull/74686).
|
|
@ -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']:
|
||||
|
|
Loading…
Reference in a new issue