Merge pull request #3184 from towolf/add_diff_to_apt_repository

Add diff support to apt_repository module
This commit is contained in:
Brian Coca 2016-03-04 18:06:32 -05:00
commit c998730152

View file

@ -287,7 +287,22 @@ class SourcesList(object):
os.remove(filename) os.remove(filename)
def dump(self): def dump(self):
return '\n'.join([str(i) for i in self]) dumpstruct = {}
for filename, sources in self.files.items():
if sources:
lines = []
for n, valid, enabled, source, comment in sources:
chunks = []
if not enabled:
chunks.append('# ')
chunks.append(source)
if comment:
chunks.append(' # ')
chunks.append(comment)
chunks.append('\n')
lines.append(''.join(chunks))
dumpstruct[filename] = ''.join(lines)
return dumpstruct
def _choice(self, new, old): def _choice(self, new, old):
if new is None: if new is None:
@ -479,7 +494,17 @@ def main():
sources_after = sourceslist.dump() sources_after = sourceslist.dump()
changed = sources_before != sources_after changed = sources_before != sources_after
if not module.check_mode and changed: if changed and module._diff:
diff = []
for filename in set(sources_before.keys()).union(sources_after.keys()):
diff.append({'before': sources_before.get(filename, ''),
'after': sources_after.get(filename, ''),
'before_header': (filename, '/dev/null')[filename not in sources_before],
'after_header': (filename, '/dev/null')[filename not in sources_after]})
else:
diff = {}
if changed and not module.check_mode:
try: try:
sourceslist.save() sourceslist.save()
if update_cache: if update_cache:
@ -488,7 +513,7 @@ def main():
except OSError, err: except OSError, err:
module.fail_json(msg=unicode(err)) module.fail_json(msg=unicode(err))
module.exit_json(changed=changed, repo=repo, state=state) module.exit_json(changed=changed, repo=repo, state=state, diff=diff)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *