Skip to content

Commit

Permalink
Add disabled option to cron module
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanK authored and mattclay committed Dec 8, 2016
1 parent a886690 commit b3495e2
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/ansible/modules/system/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# (c) 2012, Dane Summers <[email protected]>
# (c) 2013, Mike Grozak <[email protected]>
# (c) 2013, Patrick Callahan <[email protected]>
# (c) 2015, Evan Kaufman <[email protected]>
#
# This file is part of Ansible
#
Expand Down Expand Up @@ -116,10 +117,16 @@
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 = '''
Expand Down Expand Up @@ -291,17 +298,22 @@ def find_job(self, name):

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

Expand Down Expand Up @@ -414,7 +426,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,
)
Expand All @@ -432,6 +445,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
Expand Down Expand Up @@ -482,7 +496,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:
Expand Down

0 comments on commit b3495e2

Please sign in to comment.