summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Kaufman <evan@digitalflophouse.com>2016-08-03 16:29:47 -0700
committerToshio Kuratomi <a.badger@gmail.com>2016-11-01 07:58:59 -0700
commit061f8a8a9a92eb69c067bafcf91db80dfe2cce9f (patch)
treebf79b1241fc3c647d10b1371447fe735548f7f86
parent0feb7e3445df8cd19f3754e2eab63994437e18c0 (diff)
downloadansible-modules-core-061f8a8a9a92eb69c067bafcf91db80dfe2cce9f.tar.gz
Ensure trailing newline is written to cron file
Records whether existing cron file (or CRONCMD output) has a terminating newline, and ensures a trailing newline is written as necessary EVEN IF NO CHANGE WAS MADE to the target env/job Fixes #2316 (cherry picked from commit 10b0580ff48b7d7937836425a3f2c37322ad34a8)
-rw-r--r--system/cron.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/system/cron.py b/system/cron.py
index 654ca410..f13032c8 100644
--- a/system/cron.py
+++ b/system/cron.py
@@ -224,6 +224,7 @@ class CronTab(object):
self.root = (os.getuid() == 0)
self.lines = None
self.ansible = "#Ansible: "
+ self.terminated= True
if cron_file:
if os.path.isabs(cron_file):
@@ -242,7 +243,9 @@ class CronTab(object):
# read the cronfile
try:
f = open(self.cron_file, 'r')
- self.lines = f.read().splitlines()
+ read_cron_file = f.read()
+ self.terminated = read_cron_file.endswith(('\r', '\n'))
+ self.lines = read_cron_file.splitlines()
f.close()
except IOError:
# cron file does not exist
@@ -256,6 +259,8 @@ class CronTab(object):
if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
raise CronTabError("Unable to read crontab")
+ self.terminated = out.endswith(('\r', '\n'))
+
lines = out.splitlines()
count = 0
for l in lines:
@@ -464,8 +469,8 @@ class CronTab(object):
crons.append(cron)
result = '\n'.join(crons)
- if result and result[-1] not in ['\n', '\r']:
- result += '\n'
+ if result:
+ result = result.rstrip('\r\n') + '\n'
return result
def _read_user_execute(self):
@@ -660,6 +665,10 @@ def main():
crontab.remove_job(name)
changed = True
+ # no changes to env/job, but existing crontab needs a terminating newline
+ if not changed and not crontab.terminated:
+ changed = True
+
res_args = dict(
jobs = crontab.get_jobnames(),
envs = crontab.get_envnames(),