Add diff support to apt_repository module

The returned list of diffs aims to simulate how a file system diff would
look before and after writing the sources list files.

![screenshot](http://i.imgur.com/dH6QXtY.png)

n.b. Ternary conditional  is due to failing integration test for
python 2.4
This commit is contained in:
Tobias Wolf 2016-03-04 12:03:35 +01:00 committed by Matt Clay
parent 33079b2b7f
commit 3a00d2bb38

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 *