Merge pull request #2896 from towolf/add_diff_to_lineinfile

Add diff to lineinfile.py for both file contents and file attributes
This commit is contained in:
Brian Coca 2016-02-18 05:35:17 -08:00
commit cefdb9a494

View file

@ -167,10 +167,10 @@ def write_changes(module,lines,dest):
if valid: if valid:
module.atomic_move(tmpfile, os.path.realpath(dest)) module.atomic_move(tmpfile, os.path.realpath(dest))
def check_file_attrs(module, changed, message): def check_file_attrs(module, changed, message, diff):
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
if module.set_fs_attributes_if_different(file_args, False): if module.set_fs_attributes_if_different(file_args, False, diff=diff):
if changed: if changed:
message += " and " message += " and "
@ -183,6 +183,11 @@ def check_file_attrs(module, changed, message):
def present(module, dest, regexp, line, insertafter, insertbefore, create, def present(module, dest, regexp, line, insertafter, insertbefore, create,
backup, backrefs): backup, backrefs):
diff = {'before': '',
'after': '',
'before_header': '%s (content)' % dest,
'after_header': '%s (content)' % dest}
if not os.path.exists(dest): if not os.path.exists(dest):
if not create: if not create:
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest) module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
@ -195,7 +200,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
lines = f.readlines() lines = f.readlines()
f.close() f.close()
msg = "" if module._diff:
diff['before'] = ''.join(lines)
if regexp is not None: if regexp is not None:
mre = re.compile(regexp) mre = re.compile(regexp)
@ -271,6 +277,9 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
msg = 'line added' msg = 'line added'
changed = True changed = True
if module._diff:
diff['after'] = ''.join(lines)
backupdest = "" backupdest = ""
if changed and not module.check_mode: if changed and not module.check_mode:
if backup and os.path.exists(dest): if backup and os.path.exists(dest):
@ -278,10 +287,16 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
write_changes(module, lines, dest) write_changes(module, lines, dest)
if module.check_mode and not os.path.exists(dest): if module.check_mode and not os.path.exists(dest):
module.exit_json(changed=changed, msg=msg, backup=backupdest) module.exit_json(changed=changed, msg=msg, backup=backupdest, diff=diff)
msg, changed = check_file_attrs(module, changed, msg) attr_diff = {}
module.exit_json(changed=changed, msg=msg, backup=backupdest) msg, changed = check_file_attrs(module, changed, msg, attr_diff)
attr_diff['before_header'] = '%s (file attributes)' % dest
attr_diff['after_header'] = '%s (file attributes)' % dest
difflist = [diff, attr_diff]
module.exit_json(changed=changed, msg=msg, backup=backupdest, diff=difflist)
def absent(module, dest, regexp, line, backup): def absent(module, dest, regexp, line, backup):
@ -289,11 +304,19 @@ def absent(module, dest, regexp, line, backup):
if not os.path.exists(dest): if not os.path.exists(dest):
module.exit_json(changed=False, msg="file not present") module.exit_json(changed=False, msg="file not present")
msg = "" msg = ''
diff = {'before': '',
'after': '',
'before_header': '%s (content)' % dest,
'after_header': '%s (content)' % dest}
f = open(dest, 'rb') f = open(dest, 'rb')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
if module._diff:
diff['before'] = ''.join(lines)
if regexp is not None: if regexp is not None:
cre = re.compile(regexp) cre = re.compile(regexp)
found = [] found = []
@ -309,6 +332,10 @@ def absent(module, dest, regexp, line, backup):
lines = filter(matcher, lines) lines = filter(matcher, lines)
changed = len(found) > 0 changed = len(found) > 0
if module._diff:
diff['after'] = ''.join(lines)
backupdest = "" backupdest = ""
if changed and not module.check_mode: if changed and not module.check_mode:
if backup: if backup:
@ -318,8 +345,15 @@ def absent(module, dest, regexp, line, backup):
if changed: if changed:
msg = "%s line(s) removed" % len(found) msg = "%s line(s) removed" % len(found)
msg, changed = check_file_attrs(module, changed, msg) attr_diff = {}
module.exit_json(changed=changed, found=len(found), msg=msg, backup=backupdest) msg, changed = check_file_attrs(module, changed, msg, attr_diff)
attr_diff['before_header'] = '%s (file attributes)' % dest
attr_diff['after_header'] = '%s (file attributes)' % dest
difflist = [diff, attr_diff]
module.exit_json(changed=changed, found=len(found), msg=msg, backup=backupdest, diff=difflist)
def main(): def main():