ini_file: diff support.

This commit is contained in:
Luca Berruti 2016-08-25 21:05:25 +02:00 committed by Matt Clay
parent 73401182c1
commit 6d8b140f46

View file

@ -133,6 +133,10 @@ def match_active_opt(option, line):
def do_ini(module, filename, section=None, option=None, value=None, def do_ini(module, filename, section=None, option=None, value=None,
state='present', backup=False, no_extra_spaces=False, create=False): state='present', backup=False, no_extra_spaces=False, create=False):
diff = {'before': '',
'after': '',
'before_header': '%s (content)' % filename,
'after_header': '%s (content)' % filename}
if not os.path.exists(filename): if not os.path.exists(filename):
if not create: if not create:
@ -148,6 +152,9 @@ def do_ini(module, filename, section=None, option=None, value=None,
finally: finally:
ini_file.close() ini_file.close()
if module._diff:
diff['before'] = ''.join(ini_lines)
# append a fake section line to simplify the logic # append a fake section line to simplify the logic
ini_lines.append('[') ini_lines.append('[')
@ -213,6 +220,8 @@ def do_ini(module, filename, section=None, option=None, value=None,
ini_lines.append(assignment_format % (option, value)) ini_lines.append(assignment_format % (option, value))
changed = True changed = True
if module._diff:
diff['after'] = ''.join(ini_lines)
backup_file = None backup_file = None
if changed and not module.check_mode: if changed and not module.check_mode:
@ -224,7 +233,7 @@ def do_ini(module, filename, section=None, option=None, value=None,
finally: finally:
ini_file.close() ini_file.close()
return (changed, backup_file) return (changed, backup_file, diff)
# ============================================================== # ==============================================================
# main # main
@ -255,13 +264,13 @@ def main():
no_extra_spaces = module.params['no_extra_spaces'] no_extra_spaces = module.params['no_extra_spaces']
create = module.params['create'] create = module.params['create']
(changed,backup_file) = do_ini(module, dest, section, option, value, state, backup, no_extra_spaces, create) (changed,backup_file,diff) = do_ini(module, dest, section, option, value, state, backup, no_extra_spaces, create)
if not module.check_mode and os.path.exists(dest): if not module.check_mode and os.path.exists(dest):
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
changed = module.set_fs_attributes_if_different(file_args, changed) changed = module.set_fs_attributes_if_different(file_args, changed)
results = { 'changed': changed, 'msg': "OK", 'dest': dest } results = { 'changed': changed, 'msg': "OK", 'dest': dest, 'diff': diff }
if backup_file is not None: if backup_file is not None:
results['backup_file'] = backup_file results['backup_file'] = backup_file