From 4b69c8f50152e434bb53013b46542f7ddb52240f Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Thu, 29 Apr 2021 17:05:35 +0200 Subject: [PATCH] Also detect crontab which only contains whitespace as empty. --- lib/ansible/modules/cron.py | 5 ++- test/integration/targets/cron/tasks/main.yml | 36 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/cron.py b/lib/ansible/modules/cron.py index da51e6017d1..5c2937108f3 100644 --- a/lib/ansible/modules/cron.py +++ b/lib/ansible/modules/cron.py @@ -283,7 +283,10 @@ class CronTab(object): if len(self.lines) == 0: return True else: - return False + for line in self.lines: + if line.strip(): + return False + return True def write(self, backup_file=None): """ diff --git a/test/integration/targets/cron/tasks/main.yml b/test/integration/targets/cron/tasks/main.yml index 9a48a328729..ed9343a6160 100644 --- a/test/integration/targets/cron/tasks/main.yml +++ b/test/integration/targets/cron/tasks/main.yml @@ -160,6 +160,42 @@ - assert: that: not cron_file_stats.stat.exists +# BusyBox does not have /etc/cron.d +- name: Removing a cron file, which contains only whitespace + when: ansible_distribution != 'Alpine' + block: + - name: Check file does not exist + stat: + path: /etc/cron.d/cron_remove_whitespace + register: cron_file_stats + + - assert: + that: not cron_file_stats.stat.exists + + - name: Cron file creation + cron: + cron_file: cron_remove_whitespace + name: "integration test cron" + job: 'ls' + user: root + + - name: Add whitespace to cron file + shell: 'printf "\n \n\t\n" >> /etc/cron.d/cron_remove_whitespace' + + - name: Cron file deletion + cron: + cron_file: cron_remove_whitespace + name: "integration test cron" + state: absent + + - name: Check file succesfull deletion + stat: + path: /etc/cron.d/cron_remove_whitespace + register: cron_file_stats + + - assert: + that: not cron_file_stats.stat.exists + - name: System cron tab does not get removed block: - name: Add cron job