pull/85750/merge
Abhijeet Kasurde 17 hours ago committed by GitHub
commit 96198d5345
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
---
bugfixes:
- cron - handle FileNotFoundError and OSError exception while writing cron file (https://github.com/ansible/ansible/issues/85726).

@ -311,9 +311,15 @@ class CronTab:
if backup_file:
fileh = open(backup_file, 'wb')
elif self.cron_file:
fileh = open(self.b_cron_file, 'wb')
try:
fileh = open(self.b_cron_file, 'wb')
except (OSError, FileNotFoundError) as ex:
self.module.fail_json(msg=f'Unable to open {self.cron_file} for writing: {to_native(ex)}')
else:
filed, path = tempfile.mkstemp(prefix='crontab')
try:
filed, path = tempfile.mkstemp(prefix='crontab')
except OSError as ex:
self.module.fail_json(msg=f'Unable to create temporary file for writing crontab as {ex}')
os.chmod(path, S_IRWU_RWG_RWO)
fileh = os.fdopen(filed, 'wb')
@ -662,7 +668,10 @@ def main():
# if requested make a backup before making a change
if backup and not module.check_mode:
(dummy, backup_file) = tempfile.mkstemp(prefix='crontab')
try:
(dummy, backup_file) = tempfile.mkstemp(prefix='crontab')
except OSError as ex:
module.fail_json(msg=f"Unable to create temporary file for backup: {to_native(ex)}")
crontab.write(backup_file)
if env:

@ -319,3 +319,19 @@
- assert:
that: not cron_file_stats.stat.exists
- name: Add cron job with invalid cron_file path
ansible.builtin.cron:
name: "Bad cron entry"
job: "/usr/bin/true"
minute: "0"
cron_file: "bad/file"
user: root
ignore_errors: yes
register: non_existent_cron_file_job
- name: Check if we fail due to invalid cron_file path
assert:
that:
- non_existent_cron_file_job.failed
- "'Unable to open' in non_existent_cron_file_job.msg"

Loading…
Cancel
Save