diff options
author | Tobias Wolf <towolf@gmail.com> | 2016-03-14 15:42:32 +0100 |
---|---|---|
committer | Tobias Wolf <towolf@gmail.com> | 2016-03-14 15:42:32 +0100 |
commit | 0edf689688706e2750f27b106f383c61e4870450 (patch) | |
tree | 287d0265bc6fd2245cfc53265c6e6c5058889508 | |
parent | eef9c54874bda4846f3c2e8ef479b34c0f02e82e (diff) | |
download | ansible-modules-core-0edf689688706e2750f27b106f383c61e4870450.tar.gz |
Add diff and check mode to cron module
supports not writing backup file in check mode and new environment
vars.
![example screenshot](http://i.imgur.com/mkAThq8.png)
-rw-r--r-- | system/cron.py | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/system/cron.py b/system/cron.py index d216d71a..d5809341 100644 --- a/system/cron.py +++ b/system/cron.py @@ -538,7 +538,7 @@ def main(): insertafter=dict(required=False), insertbefore=dict(required=False), ), - supports_check_mode = False, + supports_check_mode = True, mutually_exclusive=[ ['reboot', 'special_time'], ['insertafter', 'insertbefore'], @@ -573,6 +573,17 @@ def main(): module.debug('cron instantiated - name: "%s"' % name) + if module._diff: + diff = dict() + diff['before'] = crontab.render() + if crontab.cron_file: + diff['before_header'] = crontab.cron_file + else: + if crontab.user: + diff['before_header'] = 'crontab for user "%s"' % crontab.user + else: + diff['before_header'] = 'crontab' + # --- user input validation --- if (special_time or reboot) and \ @@ -593,14 +604,22 @@ def main(): special_time = "reboot" # if requested make a backup before making a change - if backup: + if backup and not module.check_mode: (backuph, backup_file) = tempfile.mkstemp(prefix='crontab') crontab.write(backup_file) if crontab.cron_file and not name and not do_install: - changed = crontab.remove_job_file() - module.exit_json(changed=changed,cron_file=cron_file,state=state) + if module._diff: + diff['after'] = '' + diff['after_header'] = '/dev/null' + else: + diff = dict() + if module.check_mode: + changed = os.path.isfile(crontab.cron_file) + else: + changed = crontab.remove_job_file() + module.exit_json(changed=changed,cron_file=cron_file,state=state,diff=diff) if env: if ' ' in name: @@ -642,14 +661,27 @@ def main(): ) if changed: - crontab.write() + if not module.check_mode: + crontab.write() + if module._diff: + diff['after'] = crontab.render() + if crontab.cron_file: + diff['after_header'] = crontab.cron_file + else: + if crontab.user: + diff['after_header'] = 'crontab for user "%s"' % crontab.user + else: + diff['after_header'] = 'crontab' + + res_args['diff'] = diff # retain the backup only if crontab or cron file have changed if backup: if changed: res_args['backup_file'] = backup_file else: - os.unlink(backup_file) + if not module.check_mode: + os.unlink(backup_file) if cron_file: res_args['cron_file'] = cron_file |