2012-10-06 03:35:37 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2012, Dane Summers <dsummers@pinedesk.biz>
|
2013-02-08 09:02:44 +01:00
|
|
|
# (c) 2013, Mike Grozak <mike.grozak@gmail.com>
|
2013-07-09 05:21:40 +02:00
|
|
|
# (c) 2013, Patrick Callahan <pmc@patrickcallahan.com>
|
2015-06-28 18:29:31 +02:00
|
|
|
# (c) 2015, Evan Kaufman <evan@digitalflophouse.com>
|
2014-09-30 17:46:32 +02:00
|
|
|
# (c) 2015, Luca Berruti <nadirio@gmail.com>
|
2012-10-06 03:35:37 +02:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-09 05:21:40 +02:00
|
|
|
#
|
2012-10-06 03:35:37 +02:00
|
|
|
# Cron Plugin: The goal of this plugin is to provide an indempotent method for
|
|
|
|
# setting up cron jobs on a host. The script will play well with other manually
|
|
|
|
# entered crons. Each cron job entered will be preceded with a comment
|
|
|
|
# describing the job so that it can be found later, which is required to be
|
|
|
|
# present in order for this plugin to find/modify the job.
|
2013-07-09 05:21:40 +02:00
|
|
|
#
|
|
|
|
# This module is based on python-crontab by Martin Owens.
|
|
|
|
#
|
2012-10-06 03:35:37 +02:00
|
|
|
|
2013-02-09 20:29:31 +01:00
|
|
|
DOCUMENTATION = """
|
2012-10-08 17:10:40 +02:00
|
|
|
---
|
|
|
|
module: cron
|
2013-07-09 05:21:40 +02:00
|
|
|
short_description: Manage cron.d and crontab entries.
|
2012-10-08 17:10:40 +02:00
|
|
|
description:
|
2014-09-30 17:46:32 +02:00
|
|
|
- Use this module to manage crontab and environment variables entries. This module allows
|
|
|
|
you to create environment variables and named crontab entries, update, or delete them.
|
|
|
|
- 'When crontab jobs are managed: the module includes one line with the description of the
|
|
|
|
crontab entry C("#Ansible: <name>") corresponding to the "name" passed to the module,
|
|
|
|
which is used by future ansible/module calls to find/check the state. The "name"
|
|
|
|
parameter should be unique, and changing the "name" value will result in a new cron
|
|
|
|
task being created (or a different one being removed).'
|
|
|
|
- 'When environment variables are managed: no comment line is added, but, when the module
|
|
|
|
needs to find/check the state, it uses the "name" parameter to find the environment
|
|
|
|
variable definition line.'
|
2016-05-27 20:46:43 +02:00
|
|
|
- 'When using symbols such as %, they must be properly escaped.'
|
2012-10-08 17:10:40 +02:00
|
|
|
version_added: "0.9"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2014-09-30 17:46:32 +02:00
|
|
|
- Description of a crontab entry or, if env is set, the name of environment variable.
|
2016-06-10 19:03:01 +02:00
|
|
|
Required if state=absent. Note that if name is not set and state=present, then a
|
|
|
|
new crontab entry will always be created, regardless of existing ones.
|
2013-07-09 05:21:40 +02:00
|
|
|
default: null
|
2015-02-02 14:51:04 +01:00
|
|
|
required: false
|
2012-10-08 17:10:40 +02:00
|
|
|
user:
|
|
|
|
description:
|
2014-05-09 16:19:42 +02:00
|
|
|
- The specific user whose crontab should be modified.
|
2012-10-08 17:10:40 +02:00
|
|
|
required: false
|
|
|
|
default: root
|
|
|
|
job:
|
|
|
|
description:
|
2014-09-30 17:46:32 +02:00
|
|
|
- The command to execute or, if env is set, the value of environment variable.
|
|
|
|
Required if state=present.
|
2012-10-08 17:10:40 +02:00
|
|
|
required: false
|
2014-09-30 17:46:32 +02:00
|
|
|
aliases: ['value']
|
2013-07-09 05:21:40 +02:00
|
|
|
default: null
|
2012-10-08 17:10:40 +02:00
|
|
|
state:
|
|
|
|
description:
|
2014-09-30 17:46:32 +02:00
|
|
|
- Whether to ensure the job or environment variable is present or absent.
|
2012-10-08 17:10:40 +02:00
|
|
|
required: false
|
|
|
|
default: present
|
2013-07-09 05:21:40 +02:00
|
|
|
choices: [ "present", "absent" ]
|
2013-02-08 09:02:44 +01:00
|
|
|
cron_file:
|
2013-02-09 20:29:31 +01:00
|
|
|
description:
|
2016-01-06 15:08:53 +01:00
|
|
|
- If specified, uses this file instead of an individual user's crontab.
|
|
|
|
If this is a relative path, it is interpreted with respect to
|
|
|
|
/etc/cron.d. (If it is absolute, it will typically be /etc/crontab).
|
2015-10-08 18:20:08 +02:00
|
|
|
To use the C(cron_file) parameter you must specify the C(user) as well.
|
2013-02-09 20:29:31 +01:00
|
|
|
required: false
|
2013-07-09 05:21:40 +02:00
|
|
|
default: null
|
2012-10-08 17:10:40 +02:00
|
|
|
backup:
|
|
|
|
description:
|
2013-07-09 05:21:40 +02:00
|
|
|
- If set, create a backup of the crontab before it is modified.
|
2015-05-19 23:14:40 +02:00
|
|
|
The location of the backup is returned in the C(backup_file) variable by this module.
|
2012-10-08 17:10:40 +02:00
|
|
|
required: false
|
2015-05-19 23:23:23 +02:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: no
|
2012-10-08 17:10:40 +02:00
|
|
|
minute:
|
|
|
|
description:
|
|
|
|
- Minute when the job should run ( 0-59, *, */2, etc )
|
|
|
|
required: false
|
2012-10-09 12:42:10 +02:00
|
|
|
default: "*"
|
2012-10-08 17:10:40 +02:00
|
|
|
hour:
|
|
|
|
description:
|
|
|
|
- Hour when the job should run ( 0-23, *, */2, etc )
|
|
|
|
required: false
|
2012-10-09 12:42:10 +02:00
|
|
|
default: "*"
|
2012-10-08 17:10:40 +02:00
|
|
|
day:
|
|
|
|
description:
|
|
|
|
- Day of the month the job should run ( 1-31, *, */2, etc )
|
|
|
|
required: false
|
2012-10-09 12:42:10 +02:00
|
|
|
default: "*"
|
2013-07-09 05:21:40 +02:00
|
|
|
aliases: [ "dom" ]
|
2012-10-08 17:10:40 +02:00
|
|
|
month:
|
|
|
|
description:
|
|
|
|
- Month of the year the job should run ( 1-12, *, */2, etc )
|
|
|
|
required: false
|
2012-10-09 12:42:10 +02:00
|
|
|
default: "*"
|
2012-10-08 17:10:40 +02:00
|
|
|
weekday:
|
|
|
|
description:
|
2014-05-09 16:19:42 +02:00
|
|
|
- Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
|
2012-10-08 17:10:40 +02:00
|
|
|
required: false
|
2012-10-09 12:42:10 +02:00
|
|
|
default: "*"
|
2013-07-09 05:21:40 +02:00
|
|
|
aliases: [ "dow" ]
|
2012-12-18 23:32:51 +01:00
|
|
|
reboot:
|
|
|
|
description:
|
2013-07-09 05:21:40 +02:00
|
|
|
- If the job should be run at reboot. This option is deprecated. Users should use special_time.
|
2012-12-19 18:45:33 +01:00
|
|
|
version_added: "1.0"
|
2012-12-18 23:32:51 +01:00
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-07-09 05:21:40 +02:00
|
|
|
special_time:
|
|
|
|
description:
|
|
|
|
- Special time specification nickname.
|
|
|
|
version_added: "1.3"
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
choices: [ "reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly" ]
|
2015-06-28 18:29:31 +02:00
|
|
|
disabled:
|
|
|
|
description:
|
|
|
|
- If the job should be disabled (commented out) in the crontab. Only has effect if state=present
|
2015-08-13 05:55:01 +02:00
|
|
|
version_added: "2.0"
|
2015-06-28 18:29:31 +02:00
|
|
|
required: false
|
|
|
|
default: false
|
2014-09-30 17:46:32 +02:00
|
|
|
env:
|
|
|
|
description:
|
|
|
|
- If set, manages a crontab's environment variable. New variables are added on top of crontab.
|
|
|
|
"name" and "value" paramenters are the name and the value of environment variable.
|
2016-03-01 05:47:37 +01:00
|
|
|
version_added: "2.1"
|
2014-09-30 17:46:32 +02:00
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
insertafter:
|
|
|
|
description:
|
|
|
|
- Used with C(state=present) and C(env). If specified, the environment variable will be
|
|
|
|
inserted after the declaration of specified environment variable.
|
2016-03-01 05:47:37 +01:00
|
|
|
version_added: "2.1"
|
2014-09-30 17:46:32 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
insertbefore:
|
|
|
|
description:
|
|
|
|
- Used with C(state=present) and C(env). If specified, the environment variable will be
|
|
|
|
inserted before the declaration of specified environment variable.
|
2016-03-01 05:47:37 +01:00
|
|
|
version_added: "2.1"
|
2014-09-30 17:46:32 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2013-03-30 20:53:28 +01:00
|
|
|
requirements:
|
|
|
|
- cron
|
2015-08-13 05:55:01 +02:00
|
|
|
author:
|
|
|
|
- "Dane Summers (@dsummersl)"
|
|
|
|
- 'Mike Grozak'
|
|
|
|
- 'Patrick Callahan'
|
|
|
|
- 'Evan Kaufman (@EvanK)'
|
2014-09-30 17:46:32 +02:00
|
|
|
- 'Luca Berruti (@lberruti)'
|
2013-02-09 20:29:31 +01:00
|
|
|
"""
|
2012-10-08 17:10:40 +02:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Ensure a job that runs at 2 and 5 exists.
|
2014-12-02 23:38:32 +01:00
|
|
|
# Creates an entry like "0 5,2 * * ls -alh > /dev/null"
|
|
|
|
- cron: name="check dirs" minute="0" hour="5,2" job="ls -alh > /dev/null"
|
2013-06-14 11:53:43 +02:00
|
|
|
|
|
|
|
# Ensure an old job is no longer present. Removes any job that is prefixed
|
|
|
|
# by "#Ansible: an old job" from the crontab
|
2013-07-09 05:21:40 +02:00
|
|
|
- cron: name="an old job" state=absent
|
2013-06-14 11:53:43 +02:00
|
|
|
|
|
|
|
# Creates an entry like "@reboot /some/job.sh"
|
2013-07-09 05:21:40 +02:00
|
|
|
- cron: name="a job for reboot" special_time=reboot job="/some/job.sh"
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2014-09-30 17:46:32 +02:00
|
|
|
# Creates an entry like "PATH=/opt/bin" on top of crontab
|
|
|
|
- cron: name=PATH env=yes value=/opt/bin
|
|
|
|
|
|
|
|
# Creates an entry like "APP_HOME=/srv/app" and insert it after PATH
|
|
|
|
# declaration
|
|
|
|
- cron: name=APP_HOME env=yes value=/srv/app insertafter=PATH
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
# Creates a cron file under /etc/cron.d
|
2013-06-14 11:53:43 +02:00
|
|
|
- cron: name="yum autoupdate" weekday="2" minute=0 hour=12
|
|
|
|
user="root" job="YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
|
|
|
|
cron_file=ansible_yum-autoupdate
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
# Removes a cron file from under /etc/cron.d
|
2015-04-19 18:39:36 +02:00
|
|
|
- cron: name="yum autoupdate" cron_file=ansible_yum-autoupdate state=absent
|
2014-09-30 17:46:32 +02:00
|
|
|
|
|
|
|
# Removes "APP_HOME" environment variable from crontab
|
|
|
|
- cron: name=APP_HOME env=yes state=absent
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
import os
|
2012-10-06 03:35:37 +02:00
|
|
|
import re
|
|
|
|
import tempfile
|
2013-10-31 18:23:04 +01:00
|
|
|
import platform
|
2014-03-12 22:22:59 +01:00
|
|
|
import pipes
|
2012-10-06 03:35:37 +02:00
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
CRONCMD = "/usr/bin/crontab"
|
2012-10-06 03:35:37 +02:00
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
class CronTabError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class CronTab(object):
|
|
|
|
"""
|
|
|
|
CronTab object to write time based crontab file
|
|
|
|
|
|
|
|
user - the user of the crontab (defaults to root)
|
2016-01-06 15:08:53 +01:00
|
|
|
cron_file - a cron file under /etc/cron.d, or an absolute path
|
2013-07-09 05:21:40 +02:00
|
|
|
"""
|
|
|
|
def __init__(self, module, user=None, cron_file=None):
|
|
|
|
self.module = module
|
|
|
|
self.user = user
|
|
|
|
self.root = (os.getuid() == 0)
|
|
|
|
self.lines = None
|
|
|
|
self.ansible = "#Ansible: "
|
|
|
|
|
2013-02-08 09:02:44 +01:00
|
|
|
if cron_file:
|
2016-01-06 15:08:53 +01:00
|
|
|
if os.path.isabs(cron_file):
|
|
|
|
self.cron_file = cron_file
|
|
|
|
else:
|
|
|
|
self.cron_file = os.path.join('/etc/cron.d', cron_file)
|
2013-02-08 09:02:44 +01:00
|
|
|
else:
|
2013-07-09 05:21:40 +02:00
|
|
|
self.cron_file = None
|
|
|
|
|
|
|
|
self.read()
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
# Read in the crontab from the system
|
|
|
|
self.lines = []
|
|
|
|
if self.cron_file:
|
|
|
|
# read the cronfile
|
|
|
|
try:
|
|
|
|
f = open(self.cron_file, 'r')
|
|
|
|
self.lines = f.read().splitlines()
|
|
|
|
f.close()
|
2016-05-17 19:19:19 +02:00
|
|
|
except IOError:
|
2013-07-09 05:21:40 +02:00
|
|
|
# cron file does not exist
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
raise CronTabError("Unexpected error:", sys.exc_info()[0])
|
|
|
|
else:
|
2014-03-12 22:22:59 +01:00
|
|
|
# using safely quoted shell for now, but this really should be two non-shell calls instead. FIXME
|
|
|
|
(rc, out, err) = self.module.run_command(self._read_user_execute(), use_unsafe_shell=True)
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
|
|
|
|
raise CronTabError("Unable to read crontab")
|
|
|
|
|
|
|
|
lines = out.splitlines()
|
|
|
|
count = 0
|
|
|
|
for l in lines:
|
|
|
|
if count > 2 or (not re.match( r'# DO NOT EDIT THIS FILE - edit the master and reinstall.', l) and
|
|
|
|
not re.match( r'# \(/tmp/.*installed on.*\)', l) and
|
|
|
|
not re.match( r'# \(.*version.*\)', l)):
|
|
|
|
self.lines.append(l)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
def is_empty(self):
|
|
|
|
if len(self.lines) == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def write(self, backup_file=None):
|
|
|
|
"""
|
|
|
|
Write the crontab to the system. Saves all information.
|
|
|
|
"""
|
|
|
|
if backup_file:
|
|
|
|
fileh = open(backup_file, 'w')
|
|
|
|
elif self.cron_file:
|
|
|
|
fileh = open(self.cron_file, 'w')
|
|
|
|
else:
|
|
|
|
filed, path = tempfile.mkstemp(prefix='crontab')
|
2016-05-17 19:19:19 +02:00
|
|
|
os.chmod(path, int('0644', 8))
|
2013-07-09 05:21:40 +02:00
|
|
|
fileh = os.fdopen(filed, 'w')
|
|
|
|
|
|
|
|
fileh.write(self.render())
|
|
|
|
fileh.close()
|
|
|
|
|
|
|
|
# return if making a backup
|
|
|
|
if backup_file:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Add the entire crontab back to the user crontab
|
|
|
|
if not self.cron_file:
|
2014-03-12 22:22:59 +01:00
|
|
|
# quoting shell args for now but really this should be two non-shell calls. FIXME
|
|
|
|
(rc, out, err) = self.module.run_command(self._write_execute(path), use_unsafe_shell=True)
|
2013-07-09 05:21:40 +02:00
|
|
|
os.unlink(path)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
|
|
|
def add_job(self, name, job):
|
|
|
|
# Add the comment
|
|
|
|
self.lines.append("%s%s" % (self.ansible, name))
|
|
|
|
|
|
|
|
# Add the job
|
|
|
|
self.lines.append("%s" % (job))
|
|
|
|
|
|
|
|
def update_job(self, name, job):
|
|
|
|
return self._update_job(name, job, self.do_add_job)
|
|
|
|
|
|
|
|
def do_add_job(self, lines, comment, job):
|
|
|
|
lines.append(comment)
|
|
|
|
|
|
|
|
lines.append("%s" % (job))
|
|
|
|
|
|
|
|
def remove_job(self, name):
|
|
|
|
return self._update_job(name, "", self.do_remove_job)
|
|
|
|
|
|
|
|
def do_remove_job(self, lines, comment, job):
|
|
|
|
return None
|
|
|
|
|
2014-09-30 17:46:32 +02:00
|
|
|
def add_env(self, decl, insertafter=None, insertbefore=None):
|
|
|
|
if not (insertafter or insertbefore):
|
|
|
|
self.lines.insert(0, decl)
|
|
|
|
return
|
|
|
|
|
|
|
|
if insertafter:
|
|
|
|
other_name = insertafter
|
|
|
|
elif insertbefore:
|
|
|
|
other_name = insertbefore
|
|
|
|
other_decl = self.find_env(other_name)
|
|
|
|
if len(other_decl) > 0:
|
|
|
|
if insertafter:
|
|
|
|
index = other_decl[0]+1
|
|
|
|
elif insertbefore:
|
|
|
|
index = other_decl[0]
|
|
|
|
self.lines.insert(index, decl)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.module.fail_json(msg="Variable named '%s' not found." % other_name)
|
|
|
|
|
|
|
|
def update_env(self, name, decl):
|
|
|
|
return self._update_env(name, decl, self.do_add_env)
|
|
|
|
|
|
|
|
def do_add_env(self, lines, decl):
|
|
|
|
lines.append(decl)
|
|
|
|
|
|
|
|
def remove_env(self, name):
|
|
|
|
return self._update_env(name, '', self.do_remove_env)
|
|
|
|
|
|
|
|
def do_remove_env(self, lines, decl):
|
|
|
|
return None
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
def remove_job_file(self):
|
|
|
|
try:
|
|
|
|
os.unlink(self.cron_file)
|
2013-11-04 23:14:53 +01:00
|
|
|
return True
|
2016-05-17 19:19:19 +02:00
|
|
|
except OSError:
|
2013-07-09 05:21:40 +02:00
|
|
|
# cron file does not exist
|
2013-11-04 23:14:53 +01:00
|
|
|
return False
|
2013-07-09 05:21:40 +02:00
|
|
|
except:
|
|
|
|
raise CronTabError("Unexpected error:", sys.exc_info()[0])
|
|
|
|
|
|
|
|
def find_job(self, name):
|
|
|
|
comment = None
|
|
|
|
for l in self.lines:
|
|
|
|
if comment is not None:
|
|
|
|
if comment == name:
|
|
|
|
return [comment, l]
|
|
|
|
else:
|
|
|
|
comment = None
|
|
|
|
elif re.match( r'%s' % self.ansible, l):
|
|
|
|
comment = re.sub( r'%s' % self.ansible, '', l)
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2014-09-30 17:46:32 +02:00
|
|
|
def find_env(self, name):
|
|
|
|
for index, l in enumerate(self.lines):
|
|
|
|
if re.match( r'^%s=' % name, l):
|
|
|
|
return [index, l]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2015-06-28 18:29:31 +02:00
|
|
|
def get_cron_job(self,minute,hour,day,month,weekday,job,special,disabled):
|
2016-08-10 23:43:58 +02:00
|
|
|
# normalize any leading/trailing newlines (ansible/ansible-modules-core#3791)
|
|
|
|
job = job.strip('\r\n')
|
|
|
|
|
2015-06-28 18:29:31 +02:00
|
|
|
if disabled:
|
|
|
|
disable_prefix = '#'
|
|
|
|
else:
|
|
|
|
disable_prefix = ''
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
if special:
|
|
|
|
if self.cron_file:
|
2015-06-28 18:29:31 +02:00
|
|
|
return "%s@%s %s %s" % (disable_prefix, special, self.user, job)
|
2013-07-09 05:21:40 +02:00
|
|
|
else:
|
2015-06-28 18:29:31 +02:00
|
|
|
return "%s@%s %s" % (disable_prefix, special, job)
|
2013-02-08 09:02:44 +01:00
|
|
|
else:
|
2013-07-09 05:21:40 +02:00
|
|
|
if self.cron_file:
|
2015-06-28 18:29:31 +02:00
|
|
|
return "%s%s %s %s %s %s %s %s" % (disable_prefix,minute,hour,day,month,weekday,self.user,job)
|
2013-07-09 05:21:40 +02:00
|
|
|
else:
|
2015-06-28 18:29:31 +02:00
|
|
|
return "%s%s %s %s %s %s %s" % (disable_prefix,minute,hour,day,month,weekday,job)
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_jobnames(self):
|
|
|
|
jobnames = []
|
|
|
|
|
|
|
|
for l in self.lines:
|
|
|
|
if re.match( r'%s' % self.ansible, l):
|
|
|
|
jobnames.append(re.sub( r'%s' % self.ansible, '', l))
|
2013-02-08 09:02:44 +01:00
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
return jobnames
|
|
|
|
|
2014-09-30 17:46:32 +02:00
|
|
|
def get_envnames(self):
|
|
|
|
envnames = []
|
|
|
|
|
|
|
|
for l in self.lines:
|
|
|
|
if re.match( r'^\S+=' , l):
|
|
|
|
envnames.append(l.split('=')[0])
|
|
|
|
|
|
|
|
return envnames
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
def _update_job(self, name, job, addlinesfunction):
|
|
|
|
ansiblename = "%s%s" % (self.ansible, name)
|
|
|
|
newlines = []
|
|
|
|
comment = None
|
|
|
|
|
|
|
|
for l in self.lines:
|
|
|
|
if comment is not None:
|
|
|
|
addlinesfunction(newlines, comment, job)
|
|
|
|
comment = None
|
|
|
|
elif l == ansiblename:
|
|
|
|
comment = l
|
|
|
|
else:
|
|
|
|
newlines.append(l)
|
|
|
|
|
|
|
|
self.lines = newlines
|
|
|
|
|
|
|
|
if len(newlines) == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False # TODO add some more error testing
|
|
|
|
|
2014-09-30 17:46:32 +02:00
|
|
|
def _update_env(self, name, decl, addenvfunction):
|
|
|
|
newlines = []
|
|
|
|
|
|
|
|
for l in self.lines:
|
|
|
|
if re.match( r'^%s=' % name, l):
|
|
|
|
addenvfunction(newlines, decl)
|
|
|
|
else:
|
|
|
|
newlines.append(l)
|
|
|
|
|
|
|
|
self.lines = newlines
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
def render(self):
|
|
|
|
"""
|
|
|
|
Render this crontab as it would be in the crontab.
|
|
|
|
"""
|
|
|
|
crons = []
|
|
|
|
for cron in self.lines:
|
|
|
|
crons.append(cron)
|
|
|
|
|
|
|
|
result = '\n'.join(crons)
|
|
|
|
if result and result[-1] not in ['\n', '\r']:
|
|
|
|
result += '\n'
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _read_user_execute(self):
|
|
|
|
"""
|
|
|
|
Returns the command line for reading a crontab
|
|
|
|
"""
|
2013-10-31 18:23:04 +01:00
|
|
|
user = ''
|
|
|
|
if self.user:
|
|
|
|
if platform.system() == 'SunOS':
|
2014-03-12 22:22:59 +01:00
|
|
|
return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(CRONCMD))
|
2014-04-06 03:10:45 +02:00
|
|
|
elif platform.system() == 'AIX':
|
2014-04-08 18:53:56 +02:00
|
|
|
return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user))
|
2014-04-01 13:09:08 +02:00
|
|
|
elif platform.system() == 'HP-UX':
|
|
|
|
return "%s %s %s" % (CRONCMD , '-l', pipes.quote(self.user))
|
2016-08-10 23:30:09 +02:00
|
|
|
elif os.getlogin() != self.user:
|
2014-03-12 22:22:59 +01:00
|
|
|
user = '-u %s' % pipes.quote(self.user)
|
2013-10-31 18:23:04 +01:00
|
|
|
return "%s %s %s" % (CRONCMD , user, '-l')
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
def _write_execute(self, path):
|
|
|
|
"""
|
|
|
|
Return the command line for writing a crontab
|
|
|
|
"""
|
2013-10-31 18:23:04 +01:00
|
|
|
user = ''
|
2013-07-09 05:21:40 +02:00
|
|
|
if self.user:
|
2014-06-20 05:41:58 +02:00
|
|
|
if platform.system() in ['SunOS', 'HP-UX', 'AIX']:
|
2014-03-12 22:22:59 +01:00
|
|
|
return "chown %s %s ; su '%s' -c '%s %s'" % (pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), CRONCMD, pipes.quote(path))
|
2016-08-10 23:30:09 +02:00
|
|
|
elif os.getlogin() != self.user:
|
2014-03-12 22:22:59 +01:00
|
|
|
user = '-u %s' % pipes.quote(self.user)
|
2016-05-27 06:54:03 +02:00
|
|
|
return "%s %s %s" % (CRONCMD , user, pipes.quote(path))
|
2013-10-31 18:23:04 +01:00
|
|
|
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
#==================================================
|
2012-10-06 03:35:37 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# The following example playbooks:
|
2013-07-09 05:21:40 +02:00
|
|
|
#
|
|
|
|
# - cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null"
|
|
|
|
#
|
2012-10-06 03:35:37 +02:00
|
|
|
# - name: do the job
|
2013-07-09 05:21:40 +02:00
|
|
|
# cron: name="do the job" hour="5,2" job="/some/dir/job.sh"
|
|
|
|
#
|
2012-10-06 03:35:37 +02:00
|
|
|
# - name: no job
|
2013-07-09 05:21:40 +02:00
|
|
|
# cron: name="an old job" state=absent
|
2012-10-06 03:35:37 +02:00
|
|
|
#
|
2014-09-30 17:46:32 +02:00
|
|
|
# - name: sets env
|
|
|
|
# cron: name="PATH" env=yes value="/bin:/usr/bin"
|
|
|
|
#
|
2012-10-06 03:35:37 +02:00
|
|
|
# Would produce:
|
2014-09-30 17:46:32 +02:00
|
|
|
# PATH=/bin:/usr/bin
|
2012-10-06 03:35:37 +02:00
|
|
|
# # Ansible: check dirs
|
|
|
|
# * * 5,2 * * ls -alh > /dev/null
|
|
|
|
# # Ansible: do the job
|
|
|
|
# * * 5,2 * * /some/dir/job.sh
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2015-02-02 14:51:04 +01:00
|
|
|
name=dict(required=False),
|
2012-10-06 03:35:37 +02:00
|
|
|
user=dict(required=False),
|
2014-09-30 17:46:32 +02:00
|
|
|
job=dict(required=False, aliases=['value']),
|
2013-02-08 09:02:44 +01:00
|
|
|
cron_file=dict(required=False),
|
2012-10-06 03:35:37 +02:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
2013-02-23 22:56:45 +01:00
|
|
|
backup=dict(default=False, type='bool'),
|
2012-10-06 03:35:37 +02:00
|
|
|
minute=dict(default='*'),
|
|
|
|
hour=dict(default='*'),
|
2013-07-09 05:21:40 +02:00
|
|
|
day=dict(aliases=['dom'], default='*'),
|
2012-10-06 03:35:37 +02:00
|
|
|
month=dict(default='*'),
|
2013-07-09 05:21:40 +02:00
|
|
|
weekday=dict(aliases=['dow'], default='*'),
|
|
|
|
reboot=dict(required=False, default=False, type='bool'),
|
|
|
|
special_time=dict(required=False,
|
|
|
|
default=None,
|
|
|
|
choices=["reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly"],
|
2015-06-28 18:29:31 +02:00
|
|
|
type='str'),
|
2014-09-30 17:46:32 +02:00
|
|
|
disabled=dict(default=False, type='bool'),
|
|
|
|
env=dict(required=False, type='bool'),
|
|
|
|
insertafter=dict(required=False),
|
|
|
|
insertbefore=dict(required=False),
|
2013-07-09 05:21:40 +02:00
|
|
|
),
|
2016-03-14 15:42:32 +01:00
|
|
|
supports_check_mode = True,
|
2014-09-30 17:46:32 +02:00
|
|
|
mutually_exclusive=[
|
|
|
|
['reboot', 'special_time'],
|
|
|
|
['insertafter', 'insertbefore'],
|
|
|
|
]
|
2012-10-06 03:35:37 +02:00
|
|
|
)
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
name = module.params['name']
|
|
|
|
user = module.params['user']
|
|
|
|
job = module.params['job']
|
|
|
|
cron_file = module.params['cron_file']
|
|
|
|
state = module.params['state']
|
|
|
|
backup = module.params['backup']
|
|
|
|
minute = module.params['minute']
|
|
|
|
hour = module.params['hour']
|
|
|
|
day = module.params['day']
|
|
|
|
month = module.params['month']
|
|
|
|
weekday = module.params['weekday']
|
|
|
|
reboot = module.params['reboot']
|
|
|
|
special_time = module.params['special_time']
|
2015-06-28 18:29:31 +02:00
|
|
|
disabled = module.params['disabled']
|
2014-09-30 17:46:32 +02:00
|
|
|
env = module.params['env']
|
|
|
|
insertafter = module.params['insertafter']
|
|
|
|
insertbefore = module.params['insertbefore']
|
2013-07-09 05:21:40 +02:00
|
|
|
do_install = state == 'present'
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
res_args = dict()
|
|
|
|
|
2013-09-30 19:20:06 +02:00
|
|
|
# Ensure all files generated are only writable by the owning user. Primarily relevant for the cron_file option.
|
2016-05-17 19:19:19 +02:00
|
|
|
os.umask(int('022', 8))
|
2013-07-09 05:21:40 +02:00
|
|
|
crontab = CronTab(module, user, cron_file)
|
2015-10-01 16:16:34 +02:00
|
|
|
|
2015-10-01 06:12:54 +02:00
|
|
|
module.debug('cron instantiated - name: "%s"' % name)
|
2013-07-09 05:21:40 +02:00
|
|
|
|
2016-03-14 15:42:32 +01:00
|
|
|
if module._diff:
|
|
|
|
diff = dict()
|
|
|
|
diff['before'] = crontab.render()
|
|
|
|
if crontab.cron_file:
|
|
|
|
diff['before_header'] = crontab.cron_file
|
|
|
|
else:
|
|
|
|
if crontab.user:
|
|
|
|
diff['before_header'] = 'crontab for user "%s"' % crontab.user
|
|
|
|
else:
|
|
|
|
diff['before_header'] = 'crontab'
|
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
# --- user input validation ---
|
|
|
|
|
|
|
|
if (special_time or reboot) and \
|
|
|
|
(True in [(x != '*') for x in [minute, hour, day, month, weekday]]):
|
|
|
|
module.fail_json(msg="You must specify time and date fields or special time.")
|
|
|
|
|
|
|
|
if cron_file and do_install:
|
2013-02-08 09:02:44 +01:00
|
|
|
if not user:
|
2014-06-27 14:53:54 +02:00
|
|
|
module.fail_json(msg="To use cron_file=... parameter you must specify user=... as well")
|
2012-10-06 03:35:37 +02:00
|
|
|
|
|
|
|
if job is None and do_install:
|
2014-09-30 17:46:32 +02:00
|
|
|
module.fail_json(msg="You must specify 'job' to install a new cron job or variable")
|
2013-02-08 09:02:44 +01:00
|
|
|
|
2016-02-03 16:57:06 +01:00
|
|
|
if (insertafter or insertbefore) and not env and do_install:
|
2014-09-30 17:46:32 +02:00
|
|
|
module.fail_json(msg="Insertafter and insertbefore parameters are valid only with env=yes")
|
2013-11-14 10:22:56 +01:00
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
if reboot:
|
2014-09-30 17:46:32 +02:00
|
|
|
special_time = "reboot"
|
2013-02-08 09:02:44 +01:00
|
|
|
|
2013-07-09 05:21:40 +02:00
|
|
|
# if requested make a backup before making a change
|
2016-03-14 15:42:32 +01:00
|
|
|
if backup and not module.check_mode:
|
2013-07-09 05:21:40 +02:00
|
|
|
(backuph, backup_file) = tempfile.mkstemp(prefix='crontab')
|
|
|
|
crontab.write(backup_file)
|
2013-02-08 09:02:44 +01:00
|
|
|
|
2015-10-01 06:12:54 +02:00
|
|
|
|
2013-11-13 20:58:37 +01:00
|
|
|
if crontab.cron_file and not name and not do_install:
|
2016-03-14 15:42:32 +01:00
|
|
|
if module._diff:
|
|
|
|
diff['after'] = ''
|
|
|
|
diff['after_header'] = '/dev/null'
|
|
|
|
else:
|
|
|
|
diff = dict()
|
|
|
|
if module.check_mode:
|
|
|
|
changed = os.path.isfile(crontab.cron_file)
|
|
|
|
else:
|
|
|
|
changed = crontab.remove_job_file()
|
|
|
|
module.exit_json(changed=changed,cron_file=cron_file,state=state,diff=diff)
|
2013-07-09 05:21:40 +02:00
|
|
|
|
2014-09-30 17:46:32 +02:00
|
|
|
if env:
|
|
|
|
if ' ' in name:
|
|
|
|
module.fail_json(msg="Invalid name for environment variable")
|
|
|
|
decl = '%s="%s"' % (name, job)
|
|
|
|
old_decl = crontab.find_env(name)
|
|
|
|
|
|
|
|
if do_install:
|
|
|
|
if len(old_decl) == 0:
|
|
|
|
crontab.add_env(decl, insertafter, insertbefore)
|
|
|
|
changed = True
|
|
|
|
if len(old_decl) > 0 and old_decl[1] != decl:
|
|
|
|
crontab.update_env(name, decl)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if len(old_decl) > 0:
|
|
|
|
crontab.remove_env(name)
|
|
|
|
changed = True
|
2012-10-06 03:35:37 +02:00
|
|
|
else:
|
2014-09-30 17:46:32 +02:00
|
|
|
job = crontab.get_cron_job(minute, hour, day, month, weekday, job, special_time, disabled)
|
|
|
|
old_job = crontab.find_job(name)
|
|
|
|
|
|
|
|
if do_install:
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
if len(old_job) > 0:
|
|
|
|
crontab.remove_job(name)
|
|
|
|
changed = True
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
res_args = dict(
|
2014-09-30 17:46:32 +02:00
|
|
|
jobs = crontab.get_jobnames(),
|
|
|
|
envs = crontab.get_envnames(),
|
|
|
|
changed = changed
|
2013-07-09 05:21:40 +02:00
|
|
|
)
|
2013-02-08 09:02:44 +01:00
|
|
|
|
2012-10-06 03:35:37 +02:00
|
|
|
if changed:
|
2016-03-14 15:42:32 +01:00
|
|
|
if not module.check_mode:
|
|
|
|
crontab.write()
|
|
|
|
if module._diff:
|
|
|
|
diff['after'] = crontab.render()
|
|
|
|
if crontab.cron_file:
|
|
|
|
diff['after_header'] = crontab.cron_file
|
|
|
|
else:
|
|
|
|
if crontab.user:
|
|
|
|
diff['after_header'] = 'crontab for user "%s"' % crontab.user
|
|
|
|
else:
|
|
|
|
diff['after_header'] = 'crontab'
|
|
|
|
|
|
|
|
res_args['diff'] = diff
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
# retain the backup only if crontab or cron file have changed
|
|
|
|
if backup:
|
|
|
|
if changed:
|
|
|
|
res_args['backup_file'] = backup_file
|
2013-02-08 09:02:44 +01:00
|
|
|
else:
|
2016-03-14 15:42:32 +01:00
|
|
|
if not module.check_mode:
|
|
|
|
os.unlink(backup_file)
|
2013-07-09 05:21:40 +02:00
|
|
|
|
|
|
|
if cron_file:
|
|
|
|
res_args['cron_file'] = cron_file
|
|
|
|
|
|
|
|
module.exit_json(**res_args)
|
|
|
|
|
|
|
|
# --- should never get here
|
|
|
|
module.exit_json(msg="Unable to execute cron task.")
|
2012-10-06 03:35:37 +02:00
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-02-08 09:02:44 +01:00
|
|
|
|
2012-10-06 03:35:37 +02:00
|
|
|
main()
|
2012-12-18 23:32:51 +01:00
|
|
|
|