From 6b4977e3f46699fa410fe7d40c932fa622bfd494 Mon Sep 17 00:00:00 2001 From: Rick Elrod Date: Thu, 27 Aug 2020 09:12:27 -0500 Subject: Backport cron module regression fix (#71244) Change: - Backport of #71207 Test Plan: - CI Tickets: - Fixes #71213 - Fixes #71217 - Fixes #71216 - Fixes #71226 - Refs #71207 Signed-off-by: Rick Elrod --- changelogs/fragments/fix-cron-file-regression.yaml | 2 ++ lib/ansible/modules/system/cron.py | 1 + test/integration/targets/cron/tasks/main.yml | 26 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 changelogs/fragments/fix-cron-file-regression.yaml diff --git a/changelogs/fragments/fix-cron-file-regression.yaml b/changelogs/fragments/fix-cron-file-regression.yaml new file mode 100644 index 0000000000..b3ab90f59c --- /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/system/cron.py b/lib/ansible/modules/system/cron.py index d2cab3c87c..9817bda3cd 100644 --- a/lib/ansible/modules/system/cron.py +++ b/lib/ansible/modules/system/cron.py @@ -258,6 +258,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 829b375369..3b7e30fb37 100644 --- a/test/integration/targets/cron/tasks/main.yml +++ b/test/integration/targets/cron/tasks/main.yml @@ -100,6 +100,32 @@ - 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) + - name: Allow non-ascii chars in job (#69492) block: - name: Cron file creation -- cgit v1.2.1