1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add atomic_move in ini_file module

Fixes #34181
This commit is contained in:
Giovanni Sciortino 2018-02-26 17:40:40 +01:00 committed by Toshio Kuratomi
parent 641f8b4ef6
commit 602f059875

View file

@ -106,6 +106,8 @@ EXAMPLES = '''
import os
import re
import tempfile
import traceback
from ansible.module_utils.basic import AnsibleModule
@ -239,11 +241,20 @@ def do_ini(module, filename, section=None, option=None, value=None,
if changed and not module.check_mode:
if backup:
backup_file = module.backup_local(filename)
ini_file = open(filename, 'w')
try:
ini_file.writelines(ini_lines)
finally:
ini_file.close()
tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
f = os.fdopen(tmpfd, 'w')
f.writelines(ini_lines)
f.close()
except IOError:
module.fail_json(msg="Unable to create temporary file %s", traceback=traceback.format_exc())
try:
module.atomic_move(tmpfile, filename)
except IOError:
module.ansible.fail_json(msg='Unable to move temporary \
file %s to %s, IOError' % (tmpfile, filename), traceback=traceback.format_exc())
return (changed, backup_file, diff, msg)