summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/system/cron.py
diff options
context:
space:
mode:
authorEvan Kaufman <evan@digitalflophouse.com>2016-08-20 14:56:41 -0700
committerMatt Clay <matt@mystile.com>2016-12-08 11:25:25 -0500
commit5d3e64b0d256386a98f00d0f89b304f2db4e7748 (patch)
treeb49daa84a60b31fa9798344b9f5776bb175eed73 /lib/ansible/modules/system/cron.py
parent47ffb6d542126f553a056045265c6a3e7b0634cc (diff)
downloadansible-5d3e64b0d256386a98f00d0f89b304f2db4e7748.tar.gz
Identify existing unmanaged jobs by exact match, when no header comment is found
* updated `find_job` method to find by exact match of job, when no matching header comment is found * note this fallback injects a header comment for later calls to `update_job` or `remove_job` * abstracted header comment building to `do_comment` method Fixes #3256
Diffstat (limited to 'lib/ansible/modules/system/cron.py')
-rw-r--r--lib/ansible/modules/system/cron.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/lib/ansible/modules/system/cron.py b/lib/ansible/modules/system/cron.py
index 45c0e6958b..99fe48366a 100644
--- a/lib/ansible/modules/system/cron.py
+++ b/lib/ansible/modules/system/cron.py
@@ -307,9 +307,12 @@ class CronTab(object):
if rc != 0:
self.module.fail_json(msg=err)
+ def do_comment(self, name):
+ return "%s%s" % (self.ansible, name)
+
def add_job(self, name, job):
# Add the comment
- self.lines.append("%s%s" % (self.ansible, name))
+ self.lines.append(self.do_comment(name))
# Add the job
self.lines.append("%s" % (job))
@@ -370,7 +373,8 @@ class CronTab(object):
except:
raise CronTabError("Unexpected error:", sys.exc_info()[0])
- def find_job(self, name):
+ def find_job(self, name, job=None):
+ # attempt to find job by 'Ansible:' header comment
comment = None
for l in self.lines:
if comment is not None:
@@ -381,6 +385,19 @@ class CronTab(object):
elif re.match( r'%s' % self.ansible, l):
comment = re.sub( r'%s' % self.ansible, '', l)
+ # failing that, attempt to find job by exact match
+ if job:
+ for i, l in enumerate(self.lines):
+ if l == job:
+ # if no leading ansible header, insert one
+ if not re.match( r'%s' % self.ansible, self.lines[i-1]):
+ self.lines.insert(i, self.do_comment(name))
+ return [self.lines[i], l, True]
+ # if a leading blank ansible header AND job has a name, update header
+ elif name and self.lines[i-1] == self.do_comment(None):
+ self.lines[i-1] = self.do_comment(name)
+ return [self.lines[i-1], l, True]
+
return []
def find_env(self, name):
@@ -431,7 +448,7 @@ class CronTab(object):
return envnames
def _update_job(self, name, job, addlinesfunction):
- ansiblename = "%s%s" % (self.ansible, name)
+ ansiblename = self.do_comment(name)
newlines = []
comment = None
@@ -652,17 +669,22 @@ def main():
crontab.remove_env(name)
changed = True
else:
- old_job = crontab.find_job(name)
-
if do_install:
job = crontab.get_cron_job(minute, hour, day, month, weekday, job, special_time, disabled)
+ old_job = crontab.find_job(name, job)
+
if len(old_job) == 0:
crontab.add_job(name, job)
changed = True
if len(old_job) > 0 and old_job[1] != job:
crontab.update_job(name, job)
changed = True
+ if len(old_job) > 2:
+ crontab.update_job(name, job)
+ changed = True
else:
+ old_job = crontab.find_job(name)
+
if len(old_job) > 0:
crontab.remove_job(name)
changed = True