mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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)
This commit is contained in:
parent
7b93cd7db2
commit
368cf59e4b
1 changed files with 38 additions and 6 deletions
|
@ -538,7 +538,7 @@ def main():
|
||||||
insertafter=dict(required=False),
|
insertafter=dict(required=False),
|
||||||
insertbefore=dict(required=False),
|
insertbefore=dict(required=False),
|
||||||
),
|
),
|
||||||
supports_check_mode = False,
|
supports_check_mode = True,
|
||||||
mutually_exclusive=[
|
mutually_exclusive=[
|
||||||
['reboot', 'special_time'],
|
['reboot', 'special_time'],
|
||||||
['insertafter', 'insertbefore'],
|
['insertafter', 'insertbefore'],
|
||||||
|
@ -573,6 +573,17 @@ def main():
|
||||||
|
|
||||||
module.debug('cron instantiated - name: "%s"' % name)
|
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 ---
|
# --- user input validation ---
|
||||||
|
|
||||||
if (special_time or reboot) and \
|
if (special_time or reboot) and \
|
||||||
|
@ -593,14 +604,22 @@ def main():
|
||||||
special_time = "reboot"
|
special_time = "reboot"
|
||||||
|
|
||||||
# if requested make a backup before making a change
|
# 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')
|
(backuph, backup_file) = tempfile.mkstemp(prefix='crontab')
|
||||||
crontab.write(backup_file)
|
crontab.write(backup_file)
|
||||||
|
|
||||||
|
|
||||||
if crontab.cron_file and not name and not do_install:
|
if crontab.cron_file and not name and not do_install:
|
||||||
changed = crontab.remove_job_file()
|
if module._diff:
|
||||||
module.exit_json(changed=changed,cron_file=cron_file,state=state)
|
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 env:
|
||||||
if ' ' in name:
|
if ' ' in name:
|
||||||
|
@ -642,14 +661,27 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
if changed:
|
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
|
# retain the backup only if crontab or cron file have changed
|
||||||
if backup:
|
if backup:
|
||||||
if changed:
|
if changed:
|
||||||
res_args['backup_file'] = backup_file
|
res_args['backup_file'] = backup_file
|
||||||
else:
|
else:
|
||||||
os.unlink(backup_file)
|
if not module.check_mode:
|
||||||
|
os.unlink(backup_file)
|
||||||
|
|
||||||
if cron_file:
|
if cron_file:
|
||||||
res_args['cron_file'] = cron_file
|
res_args['cron_file'] = cron_file
|
||||||
|
|
Loading…
Reference in a new issue