From fefda12827c2932c2d740af074e9a8d96202ed72 Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Thu, 29 Apr 2021 15:48:32 +0200 Subject: [PATCH] Only remove crontabs if they are empty --- lib/ansible/modules/cron.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/ansible/modules/cron.py b/lib/ansible/modules/cron.py index a84a4bada7f..da51e6017d1 100644 --- a/lib/ansible/modules/cron.py +++ b/lib/ansible/modules/cron.py @@ -647,18 +647,6 @@ def main(): (backuph, backup_file) = tempfile.mkstemp(prefix='crontab') crontab.write(backup_file) - if crontab.cron_file and not do_install: - 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: module.fail_json(msg="Invalid name for environment variable") @@ -701,6 +689,17 @@ def main(): if len(old_job) > 0: crontab.remove_job(name) changed = True + if crontab.cron_file and crontab.is_empty(): + 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) # no changes to env/job, but existing crontab needs a terminating newline if not changed and crontab.n_existing != '':