mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fix #2125 and clean up a few things along the way
This commit is contained in:
parent
18eca152e3
commit
d2bf205a5d
1 changed files with 13 additions and 12 deletions
17
library/cron
17
library/cron
|
@ -167,8 +167,9 @@ def find_job(name,tmpfile):
|
|||
return []
|
||||
|
||||
def add_job(module,name,job,tmpfile):
|
||||
cmd = "echo \"#Ansible: %s\n%s\" >> %s" % (name,job,tmpfile)
|
||||
return module.run_command(cmd)
|
||||
f = open(tmpfile, 'a')
|
||||
f.write("#Ansible: %s\n%s\n" % (name, job))
|
||||
f.close()
|
||||
|
||||
def update_job(name,job,tmpfile):
|
||||
return _update_job(name,job,tmpfile,do_add_job)
|
||||
|
@ -191,6 +192,7 @@ def _update_job(name,job,tmpfile,addlinesfunction):
|
|||
ansiblename = "#Ansible: %s" % (name)
|
||||
f = open(tmpfile)
|
||||
lines = f.read().splitlines()
|
||||
f.close()
|
||||
newlines = []
|
||||
comment = None
|
||||
for l in lines:
|
||||
|
@ -201,7 +203,6 @@ def _update_job(name,job,tmpfile,addlinesfunction):
|
|||
comment = l
|
||||
else:
|
||||
newlines.append(l)
|
||||
f.close()
|
||||
f = open(tmpfile, 'w')
|
||||
for l in newlines:
|
||||
f.write(l)
|
||||
|
@ -209,9 +210,9 @@ def _update_job(name,job,tmpfile,addlinesfunction):
|
|||
f.close()
|
||||
|
||||
if len(newlines) == 0:
|
||||
return (0,"","",True)
|
||||
return True
|
||||
else:
|
||||
return (0,"","",False) # TODO add some more error testing
|
||||
return False # TODO add some more error testing
|
||||
|
||||
def get_cron_job(minute,hour,day,month,weekday,job,user,cron_file,reboot):
|
||||
if reboot:
|
||||
|
@ -314,15 +315,15 @@ def main():
|
|||
old_job = find_job(name,backupfile)
|
||||
if do_install:
|
||||
if len(old_job) == 0:
|
||||
(rc, out, err) = add_job(module,name,job,tmpfile.name)
|
||||
add_job(module,name,job,tmpfile.name)
|
||||
changed = True
|
||||
if len(old_job) > 0 and old_job[1] != job:
|
||||
(rc, out, err) = update_job(name,job,tmpfile.name)
|
||||
update_job(name,job,tmpfile.name)
|
||||
changed = True
|
||||
else:
|
||||
if len(old_job) > 0:
|
||||
# if rm is true after the next line, file will be deleted afterwards
|
||||
(rc, out, err, rm) = remove_job(name,tmpfile.name)
|
||||
rm = remove_job(name,tmpfile.name)
|
||||
changed = True
|
||||
else:
|
||||
# there is no old_jobs for deletion - we should leave everything
|
||||
|
|
Loading…
Reference in a new issue