From 8647d0a8733b3250673eecc6eb55c94917ce8c9e Mon Sep 17 00:00:00 2001 From: Emmanuel MICKIEWICZ Date: Sat, 15 Feb 2020 15:52:36 +0100 Subject: [PATCH] Removing a cron file when the 'name' parameter is specified is now allowed (#57471). Added integration tests to validate new behavior. (#65640) Also added user input validation to return a clear error when deleting an environment variables without specifying a name. --- lib/ansible/modules/system/cron.py | 5 ++++- test/integration/targets/cron/tasks/main.yml | 23 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/system/cron.py b/lib/ansible/modules/system/cron.py index 4aaac4cc5b6..ed93499bb8d 100644 --- a/lib/ansible/modules/system/cron.py +++ b/lib/ansible/modules/system/cron.py @@ -642,6 +642,9 @@ def main(): # --- user input validation --- + if env and not name: + module.fail_json(msg="You must specify 'name' while working with environment variables (env=yes)") + if (special_time or reboot) and \ (True in [(x != '*') for x in [minute, hour, day, month, weekday]]): module.fail_json(msg="You must specify time and date fields or special time.") @@ -668,7 +671,7 @@ def main(): (backuph, backup_file) = tempfile.mkstemp(prefix='crontab') crontab.write(backup_file) - if crontab.cron_file and not name and not do_install: + if crontab.cron_file and not do_install: if module._diff: diff['after'] = '' diff['after_header'] = '/dev/null' diff --git a/test/integration/targets/cron/tasks/main.yml b/test/integration/targets/cron/tasks/main.yml index cc892c8e06e..155fc2d5809 100644 --- a/test/integration/targets/cron/tasks/main.yml +++ b/test/integration/targets/cron/tasks/main.yml @@ -99,3 +99,26 @@ - assert: that: remove_cron_file is not changed + +- name: Removing a cron file when the name is specified is allowed (#57471) + block: + - name: Cron file creation + cron: + cron_file: cron_filename + name: "integration test cron" + job: 'ls' + user: root + + - name: Cron file deletion + cron: + cron_file: cron_filename + name: "integration test cron" + state: absent + + - name: Check file succesfull deletion + stat: + path: /etc/cron.d/cron_filename + register: cron_file_stats + + - assert: + that: not cron_file_stats.stat.exists