From 1187399ffa7a5b4d99b5723fe6aeb44a2a1edbf3 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Sun, 28 Jun 2015 11:29:31 -0500 Subject: [PATCH] Add disabled option to cron module --- system/cron.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/system/cron.py b/system/cron.py index 88985e23071..57455d6e19d 100644 --- a/system/cron.py +++ b/system/cron.py @@ -4,6 +4,7 @@ # (c) 2012, Dane Summers # (c) 2013, Mike Grozak # (c) 2013, Patrick Callahan +# (c) 2015, Evan Kaufman # # This file is part of Ansible # @@ -116,10 +117,16 @@ options: required: false default: null choices: [ "reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly" ] + disabled: + description: + - If the job should be disabled (commented out) in the crontab. Only has effect if state=present + version_added: "1.9" + required: false + default: false requirements: - cron author: "Dane Summers (@dsummersl)" -updates: [ 'Mike Grozak', 'Patrick Callahan' ] +updates: [ 'Mike Grozak', 'Patrick Callahan', 'Evan Kaufman' ] """ EXAMPLES = ''' @@ -290,17 +297,22 @@ class CronTab(object): return [] - def get_cron_job(self,minute,hour,day,month,weekday,job,special): + def get_cron_job(self,minute,hour,day,month,weekday,job,special,disabled): + if disabled: + disable_prefix = '#' + else: + disable_prefix = '' + if special: if self.cron_file: - return "@%s %s %s" % (special, self.user, job) + return "%s@%s %s %s" % (disable_prefix, special, self.user, job) else: - return "@%s %s" % (special, job) + return "%s@%s %s" % (disable_prefix, special, job) else: if self.cron_file: - return "%s %s %s %s %s %s %s" % (minute,hour,day,month,weekday,self.user,job) + return "%s%s %s %s %s %s %s %s" % (disable_prefix,minute,hour,day,month,weekday,self.user,job) else: - return "%s %s %s %s %s %s" % (minute,hour,day,month,weekday,job) + return "%s%s %s %s %s %s %s" % (disable_prefix,minute,hour,day,month,weekday,job) return None @@ -413,7 +425,8 @@ def main(): special_time=dict(required=False, default=None, choices=["reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly"], - type='str') + type='str'), + disabled=dict(default=False, type='bool') ), supports_check_mode = False, ) @@ -431,6 +444,7 @@ def main(): weekday = module.params['weekday'] reboot = module.params['reboot'] special_time = module.params['special_time'] + disabled = module.params['disabled'] do_install = state == 'present' changed = False @@ -481,7 +495,7 @@ def main(): changed = crontab.remove_job_file() module.exit_json(changed=changed,cron_file=cron_file,state=state) - job = crontab.get_cron_job(minute, hour, day, month, weekday, job, special_time) + job = crontab.get_cron_job(minute, hour, day, month, weekday, job, special_time, disabled) old_job = crontab.find_job(name) if do_install: