diff --git a/changelogs/fragments/fix-cron-file-regression.yaml b/changelogs/fragments/fix-cron-file-regression.yaml new file mode 100644 index 00000000000..b3ab90f59c1 --- /dev/null +++ b/changelogs/fragments/fix-cron-file-regression.yaml @@ -0,0 +1,2 @@ +bugfixes: + - cron - cron file should not be empty after adding var (https://github.com/ansible/ansible/pull/71207) diff --git a/lib/ansible/modules/cron.py b/lib/ansible/modules/cron.py index 98c88c09c44..7239cda261a 100644 --- a/lib/ansible/modules/cron.py +++ b/lib/ansible/modules/cron.py @@ -255,6 +255,7 @@ class CronTab(object): try: f = open(self.b_cron_file, 'rb') self.n_existing = to_native(f.read(), errors='surrogate_or_strict') + self.lines = self.n_existing.splitlines() f.close() except IOError: # cron file does not exist diff --git a/test/integration/targets/cron/tasks/main.yml b/test/integration/targets/cron/tasks/main.yml index 950d52dc4fc..3537b48de23 100644 --- a/test/integration/targets/cron/tasks/main.yml +++ b/test/integration/targets/cron/tasks/main.yml @@ -100,6 +100,33 @@ - assert: that: remove_cron_file is not changed +- name: Non regression test - cron file should not be empty after adding var (#71207) + when: ansible_distribution != 'Alpine' + block: + - name: Cron file creation + cron: + cron_file: cron_filename + name: "simple cron job" + job: 'echo "_o/"' + user: root + + - name: Add var to the cron file + cron: + cron_file: cron_filename + env: yes + name: FOO + value: bar + user: root + + - name: "Ensure cron_file still contains job string" + replace: + path: /etc/cron.d/cron_filename + regexp: "_o/" + replace: "OK" + register: find_chars + failed_when: (find_chars is not changed) or (find_chars is failed) + +# BusyBox does not have /etc/cron.d - name: Removing a cron file when the name is specified is allowed (#57471) block: - name: Cron file creation