summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Kaufman <evan@digitalflophouse.com>2016-08-14 23:51:06 -0700
committerToshio Kuratomi <a.badger@gmail.com>2016-10-23 13:55:01 -0700
commit9b30c8dc3ab8bcaad87aeccda57891481f13f10c (patch)
treeba6210add2b87c624ef9cd738f42d8e9b07a3fd3
parentc4f47f6182235caa1758a2c5473be9fbc6622e61 (diff)
downloadansible-modules-core-9b30c8dc3ab8bcaad87aeccda57891481f13f10c.tar.gz
Record existing cron file as string property, rather than only recording termination
This seems less hackish, and feels more proper for diff generation (cherry picked from commit b0c94e957ecffad4d96ec6c8723289a7cd2807e9)
-rw-r--r--system/cron.py20
1 files changed, 11 insertions, 9 deletions
diff --git a/system/cron.py b/system/cron.py
index 634d1d6b..b7739a1c 100644
--- a/system/cron.py
+++ b/system/cron.py
@@ -224,7 +224,7 @@ class CronTab(object):
self.root = (os.getuid() == 0)
self.lines = None
self.ansible = "#Ansible: "
- self.terminated= True
+ self.existing = ''
if cron_file:
if os.path.isabs(cron_file):
@@ -243,9 +243,8 @@ class CronTab(object):
# read the cronfile
try:
f = open(self.cron_file, 'r')
- read_cron_file = f.read()
- self.terminated = read_cron_file.endswith(('\r', '\n'))
- self.lines = read_cron_file.splitlines()
+ self.existing = f.read()
+ self.lines = self.existing.splitlines()
f.close()
except IOError:
# cron file does not exist
@@ -259,7 +258,7 @@ 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'))
+ self.existing = out
lines = out.splitlines()
count = 0
@@ -268,6 +267,9 @@ class CronTab(object):
not re.match( r'# \(/tmp/.*installed on.*\)', l) and
not re.match( r'# \(.*version.*\)', l)):
self.lines.append(l)
+ else:
+ pattern = re.escape(l) + '[\r\n]?'
+ self.existing = re.sub(pattern, '', self.existing, 1)
count += 1
def is_empty(self):
@@ -460,7 +462,7 @@ class CronTab(object):
self.lines = newlines
- def render(self, diff=None):
+ def render(self):
"""
Render this crontab as it would be in the crontab.
"""
@@ -469,7 +471,7 @@ class CronTab(object):
crons.append(cron)
result = '\n'.join(crons)
- if result and not diff:
+ if result:
result = result.rstrip('\r\n') + '\n'
return result
@@ -586,7 +588,7 @@ def main():
if module._diff:
diff = dict()
- diff['before'] = crontab.render(diff=True)
+ diff['before'] = crontab.existing
if crontab.cron_file:
diff['before_header'] = crontab.cron_file
else:
@@ -666,7 +668,7 @@ def main():
changed = True
# no changes to env/job, but existing crontab needs a terminating newline
- if not changed and not crontab.terminated:
+ if not changed and not crontab.existing.endswith(('\r', '\n')):
changed = True
res_args = dict(