Add atomic_move in ini_file module

Fixes #34181
pull/36750/merge
Giovanni Sciortino 7 years ago committed by Toshio Kuratomi
parent 641f8b4ef6
commit 602f059875

@ -106,6 +106,8 @@ EXAMPLES = '''
import os import os
import re import re
import tempfile
import traceback
from ansible.module_utils.basic import AnsibleModule 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 changed and not module.check_mode:
if backup: if backup:
backup_file = module.backup_local(filename) backup_file = module.backup_local(filename)
ini_file = open(filename, 'w')
try: try:
ini_file.writelines(ini_lines) tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
finally: f = os.fdopen(tmpfd, 'w')
ini_file.close() 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) return (changed, backup_file, diff, msg)

Loading…
Cancel
Save