2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-09-25 23:48:22 +00:00
|
|
|
|
|
|
|
# Copyright: (c) 2012, Dane Summers <dsummers@pinedesk.biz>
|
|
|
|
# Copyright: (c) 2013, Mike Grozak <mike.grozak@gmail.com>
|
|
|
|
# Copyright: (c) 2013, Patrick Callahan <pmc@patrickcallahan.com>
|
|
|
|
# Copyright: (c) 2015, Evan Kaufman <evan@digitalflophouse.com>
|
|
|
|
# Copyright: (c) 2015, Luca Berruti <nadirio@gmail.com>
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
2017-05-18 15:28:41 +00:00
|
|
|
'supported_by': 'community'}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2019-02-19 14:42:14 +00:00
|
|
|
DOCUMENTATION = r'''
|
2014-09-26 01:01:01 +00:00
|
|
|
---
|
|
|
|
module: cron
|
2017-09-25 23:48:22 +00:00
|
|
|
short_description: Manage cron.d and crontab entries
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2014-09-30 15:46:32 +00: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).'
|
2019-02-19 14:42:14 +00:00
|
|
|
- When environment variables are managed, no comment line is added, but, when the module
|
2014-09-30 15:46:32 +00:00
|
|
|
needs to find/check the state, it uses the "name" parameter to find the environment
|
2019-02-19 14:42:14 +00:00
|
|
|
variable definition line.
|
|
|
|
- When using symbols such as %, they must be properly escaped.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "0.9"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2014-09-30 15:46:32 +00:00
|
|
|
- Description of a crontab entry or, if env is set, the name of environment variable.
|
2019-02-19 14:42:14 +00:00
|
|
|
- Required if C(state=absent).
|
|
|
|
- Note that if name is not set and C(state=present), then a
|
2016-06-10 17:03:01 +00:00
|
|
|
new crontab entry will always be created, regardless of existing ones.
|
2019-02-19 14:42:14 +00:00
|
|
|
- This parameter will always be required in future releases.
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- The specific user whose crontab should be modified.
|
2019-02-19 14:42:14 +00:00
|
|
|
- When unset, this parameter defaults to using C(root).
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
job:
|
|
|
|
description:
|
2014-09-30 15:46:32 +00:00
|
|
|
- The command to execute or, if env is set, the value of environment variable.
|
2019-02-19 14:42:14 +00:00
|
|
|
- The command should not contain line breaks.
|
|
|
|
- Required if C(state=present).
|
|
|
|
type: str
|
2017-09-25 23:48:22 +00:00
|
|
|
aliases: [ value ]
|
2014-09-26 01:01:01 +00:00
|
|
|
state:
|
|
|
|
description:
|
2014-09-30 15:46:32 +00:00
|
|
|
- Whether to ensure the job or environment variable is present or absent.
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
2017-09-25 23:48:22 +00:00
|
|
|
choices: [ absent, present ]
|
2014-09-26 01:01:01 +00:00
|
|
|
default: present
|
|
|
|
cron_file:
|
|
|
|
description:
|
2016-01-06 14:08:53 +00:00
|
|
|
- If specified, uses this file instead of an individual user's crontab.
|
2019-02-19 14:42:14 +00:00
|
|
|
- If this is a relative path, it is interpreted with respect to I(/etc/cron.d).
|
|
|
|
- If it is absolute, it will typically be I(/etc/crontab).
|
|
|
|
- Many linux distros expect (and some require) the filename portion to consist solely
|
2017-07-21 21:51:34 +00:00
|
|
|
of upper- and lower-case letters, digits, underscores, and hyphens.
|
2019-02-19 14:42:14 +00:00
|
|
|
- To use the C(cron_file) parameter you must specify the C(user) as well.
|
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
backup:
|
|
|
|
description:
|
|
|
|
- If set, create a backup of the crontab before it is modified.
|
2015-05-19 21:14:40 +00:00
|
|
|
The location of the backup is returned in the C(backup_file) variable by this module.
|
2017-09-25 23:48:22 +00:00
|
|
|
type: bool
|
2019-02-19 14:42:14 +00:00
|
|
|
default: no
|
2014-09-26 01:01:01 +00:00
|
|
|
minute:
|
|
|
|
description:
|
|
|
|
- Minute when the job should run ( 0-59, *, */2, etc )
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "*"
|
|
|
|
hour:
|
|
|
|
description:
|
|
|
|
- Hour when the job should run ( 0-23, *, */2, etc )
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "*"
|
|
|
|
day:
|
|
|
|
description:
|
|
|
|
- Day of the month the job should run ( 1-31, *, */2, etc )
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "*"
|
2017-09-25 23:48:22 +00:00
|
|
|
aliases: [ dom ]
|
2014-09-26 01:01:01 +00:00
|
|
|
month:
|
|
|
|
description:
|
|
|
|
- Month of the year the job should run ( 1-12, *, */2, etc )
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "*"
|
|
|
|
weekday:
|
|
|
|
description:
|
|
|
|
- Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "*"
|
2017-09-25 23:48:22 +00:00
|
|
|
aliases: [ dow ]
|
2014-09-26 01:01:01 +00:00
|
|
|
reboot:
|
|
|
|
description:
|
|
|
|
- If the job should be run at reboot. This option is deprecated. Users should use special_time.
|
|
|
|
version_added: "1.0"
|
2017-09-25 23:48:22 +00:00
|
|
|
type: bool
|
2019-02-19 14:42:14 +00:00
|
|
|
default: no
|
2014-09-26 01:01:01 +00:00
|
|
|
special_time:
|
|
|
|
description:
|
|
|
|
- Special time specification nickname.
|
2019-02-19 14:42:14 +00:00
|
|
|
type: str
|
Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option
This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.
- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.
As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).
This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)
```python
module = AnsibleModule(
argument_spec=dict(
path=dict(type='path', aliases=['dest', 'file']),
xmlstring=dict(type='str'),
xpath=dict(type='str'),
namespaces=dict(type='dict', default={}),
state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
value=dict(type='raw'),
attribute=dict(type='raw'),
add_children=dict(type='list'),
set_children=dict(type='list'),
count=dict(type='bool', default=False),
print_match=dict(type='bool', default=False),
pretty_print=dict(type='bool', default=False),
content=dict(type='str', choices=['attribute', 'text']),
input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
backup=dict(type='bool', default=False),
),
supports_check_mode=True,
required_by=dict(
add_children=['xpath'],
attribute=['value', 'xpath'],
content=['xpath'],
set_children=['xpath'],
value=['xpath'],
),
required_if=[
['count', True, ['xpath']],
['print_match', True, ['xpath']],
],
required_one_of=[
['path', 'xmlstring'],
['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
],
mutually_exclusive=[
['add_children', 'content', 'count', 'print_match','set_children', 'value'],
['path', 'xmlstring'],
],
)
```
* Rebase and fix conflict
* Add modules that use required_by functionality
* Update required_by schema
* Fix rebase issue
2019-02-15 00:57:45 +00:00
|
|
|
choices: [ annually, daily, hourly, monthly, reboot, weekly, yearly ]
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "1.3"
|
2015-06-28 16:29:31 +00:00
|
|
|
disabled:
|
|
|
|
description:
|
2017-09-25 23:48:22 +00:00
|
|
|
- If the job should be disabled (commented out) in the crontab.
|
|
|
|
- Only has effect if C(state=present).
|
|
|
|
type: bool
|
2019-02-19 14:42:14 +00:00
|
|
|
default: no
|
2016-12-08 02:33:38 +00:00
|
|
|
version_added: "2.0"
|
2014-09-30 15:46:32 +00:00
|
|
|
env:
|
|
|
|
description:
|
2019-02-19 14:42:14 +00:00
|
|
|
- If set, manages a crontab's environment variable.
|
|
|
|
- New variables are added on top of crontab.
|
|
|
|
- C(name) and C(value) parameters are the name and the value of environment variable.
|
2017-09-25 23:48:22 +00:00
|
|
|
type: bool
|
2019-02-19 14:42:14 +00:00
|
|
|
default: no
|
2017-09-25 23:48:22 +00:00
|
|
|
version_added: "2.1"
|
2014-09-30 15:46:32 +00:00
|
|
|
insertafter:
|
|
|
|
description:
|
2019-02-19 14:42:14 +00:00
|
|
|
- Used with C(state=present) and C(env).
|
|
|
|
- If specified, the environment variable will be inserted after the declaration of specified environment variable.
|
|
|
|
type: str
|
2016-12-08 02:33:38 +00:00
|
|
|
version_added: "2.1"
|
2014-09-30 15:46:32 +00:00
|
|
|
insertbefore:
|
|
|
|
description:
|
2019-02-19 14:42:14 +00:00
|
|
|
- Used with C(state=present) and C(env).
|
|
|
|
- If specified, the environment variable will be inserted before the declaration of specified environment variable.
|
|
|
|
type: str
|
2016-12-08 02:33:38 +00:00
|
|
|
version_added: "2.1"
|
2014-09-26 01:01:01 +00:00
|
|
|
requirements:
|
|
|
|
- cron
|
2014-09-30 15:46:32 +00:00
|
|
|
author:
|
2017-09-25 23:48:22 +00:00
|
|
|
- Dane Summers (@dsummersl)
|
2018-11-21 17:29:29 +00:00
|
|
|
- Mike Grozak (@rhaido)
|
2018-11-21 15:10:41 +00:00
|
|
|
- Patrick Callahan (@dirtyharrycallahan)
|
2017-09-25 23:48:22 +00:00
|
|
|
- Evan Kaufman (@EvanK)
|
|
|
|
- Luca Berruti (@lberruti)
|
2019-02-19 14:42:14 +00:00
|
|
|
'''
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2019-02-19 14:42:14 +00:00
|
|
|
EXAMPLES = r'''
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: "check dirs"
|
|
|
|
minute: "0"
|
|
|
|
hour: "5,2"
|
|
|
|
job: "ls -alh > /dev/null"
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: 'Ensure an old job is no longer present. Removes any job that is prefixed by "#Ansible: an old job" from the crontab'
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: "an old job"
|
|
|
|
state: absent
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Creates an entry like "@reboot /some/job.sh"
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: "a job for reboot"
|
|
|
|
special_time: reboot
|
|
|
|
job: "/some/job.sh"
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Creates an entry like "PATH=/opt/bin" on top of crontab
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: PATH
|
|
|
|
env: yes
|
2018-09-24 15:06:33 +00:00
|
|
|
job: /opt/bin
|
2014-09-30 15:46:32 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Creates an entry like "APP_HOME=/srv/app" and insert it after PATH declaration
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: APP_HOME
|
|
|
|
env: yes
|
2018-09-24 15:06:33 +00:00
|
|
|
job: /srv/app
|
2016-11-15 20:21:47 +00:00
|
|
|
insertafter: PATH
|
2014-09-30 15:46:32 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Creates a cron file under /etc/cron.d
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: yum autoupdate
|
|
|
|
weekday: 2
|
|
|
|
minute: 0
|
|
|
|
hour: 12
|
|
|
|
user: root
|
2018-05-15 22:22:07 +00:00
|
|
|
job: "YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
|
2016-11-15 20:21:47 +00:00
|
|
|
cron_file: ansible_yum-autoupdate
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Removes a cron file from under /etc/cron.d
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: "yum autoupdate"
|
|
|
|
cron_file: ansible_yum-autoupdate
|
|
|
|
state: absent
|
2014-09-30 15:46:32 +00:00
|
|
|
|
2018-04-16 23:02:06 +00:00
|
|
|
- name: Removes "APP_HOME" environment variable from crontab
|
|
|
|
cron:
|
2016-11-15 20:21:47 +00:00
|
|
|
name: APP_HOME
|
|
|
|
env: yes
|
|
|
|
state: absent
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
import platform
|
|
|
|
import pipes
|
2016-09-12 05:39:12 +00:00
|
|
|
import pwd
|
2014-09-26 01:01:01 +00:00
|
|
|
import re
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
import sys
|
2014-09-26 01:01:01 +00:00
|
|
|
import tempfile
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, get_platform
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
CRONCMD = "/usr/bin/crontab"
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
class CronTabError(Exception):
|
|
|
|
pass
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
class CronTab(object):
|
|
|
|
"""
|
|
|
|
CronTab object to write time based crontab file
|
|
|
|
|
|
|
|
user - the user of the crontab (defaults to root)
|
2016-01-06 14:08:53 +00:00
|
|
|
cron_file - a cron file under /etc/cron.d, or an absolute path
|
2014-09-26 01:01:01 +00:00
|
|
|
"""
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def __init__(self, module, user=None, cron_file=None):
|
2017-09-25 23:48:22 +00:00
|
|
|
self.module = module
|
|
|
|
self.user = user
|
|
|
|
self.root = (os.getuid() == 0)
|
|
|
|
self.lines = None
|
|
|
|
self.ansible = "#Ansible: "
|
|
|
|
self.existing = ''
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if cron_file:
|
2016-01-06 14:08:53 +00:00
|
|
|
if os.path.isabs(cron_file):
|
|
|
|
self.cron_file = cron_file
|
|
|
|
else:
|
|
|
|
self.cron_file = os.path.join('/etc/cron.d', cron_file)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
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')
|
2016-08-15 06:51:06 +00:00
|
|
|
self.existing = f.read()
|
|
|
|
self.lines = self.existing.splitlines()
|
2014-09-26 01:01:01 +00:00
|
|
|
f.close()
|
2016-05-17 17:19:19 +00:00
|
|
|
except IOError:
|
2014-09-26 01:01:01 +00:00
|
|
|
# cron file does not exist
|
|
|
|
return
|
2018-09-08 00:59:46 +00:00
|
|
|
except Exception:
|
2014-09-26 01:01:01 +00:00
|
|
|
raise CronTabError("Unexpected error:", sys.exc_info()[0])
|
|
|
|
else:
|
|
|
|
# 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)
|
|
|
|
|
2017-09-25 23:48:22 +00:00
|
|
|
if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
|
2014-09-26 01:01:01 +00:00
|
|
|
raise CronTabError("Unable to read crontab")
|
|
|
|
|
2016-08-15 06:51:06 +00:00
|
|
|
self.existing = out
|
2016-08-03 23:29:47 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
lines = out.splitlines()
|
|
|
|
count = 0
|
|
|
|
for l in lines:
|
2017-09-25 23:48:22 +00:00
|
|
|
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)):
|
2014-09-26 01:01:01 +00:00
|
|
|
self.lines.append(l)
|
2016-08-15 06:51:06 +00:00
|
|
|
else:
|
|
|
|
pattern = re.escape(l) + '[\r\n]?'
|
|
|
|
self.existing = re.sub(pattern, '', self.existing, 1)
|
2014-09-26 01:01:01 +00:00
|
|
|
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 17:19:19 +00:00
|
|
|
os.chmod(path, int('0644', 8))
|
2014-09-26 01:01:01 +00: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:
|
|
|
|
# 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)
|
|
|
|
os.unlink(path)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
2016-11-29 13:54:05 +00:00
|
|
|
# set SELinux permissions
|
2016-12-25 02:57:13 +00:00
|
|
|
if self.module.selinux_enabled() and self.cron_file:
|
|
|
|
self.module.set_default_selinux_context(self.cron_file, False)
|
2016-11-29 13:54:05 +00:00
|
|
|
|
2016-08-20 21:56:41 +00:00
|
|
|
def do_comment(self, name):
|
|
|
|
return "%s%s" % (self.ansible, name)
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def add_job(self, name, job):
|
|
|
|
# Add the comment
|
2016-08-20 21:56:41 +00:00
|
|
|
self.lines.append(self.do_comment(name))
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# 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 15:46:32 +00: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:
|
2017-09-25 23:48:22 +00:00
|
|
|
index = other_decl[0] + 1
|
2014-09-30 15:46:32 +00:00
|
|
|
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
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def remove_job_file(self):
|
|
|
|
try:
|
|
|
|
os.unlink(self.cron_file)
|
|
|
|
return True
|
2016-05-17 17:19:19 +00:00
|
|
|
except OSError:
|
2014-09-26 01:01:01 +00:00
|
|
|
# cron file does not exist
|
|
|
|
return False
|
2018-09-08 00:59:46 +00:00
|
|
|
except Exception:
|
2014-09-26 01:01:01 +00:00
|
|
|
raise CronTabError("Unexpected error:", sys.exc_info()[0])
|
|
|
|
|
2016-08-20 21:56:41 +00:00
|
|
|
def find_job(self, name, job=None):
|
|
|
|
# attempt to find job by 'Ansible:' header comment
|
2014-09-26 01:01:01 +00:00
|
|
|
comment = None
|
|
|
|
for l in self.lines:
|
|
|
|
if comment is not None:
|
|
|
|
if comment == name:
|
|
|
|
return [comment, l]
|
|
|
|
else:
|
|
|
|
comment = None
|
2017-09-25 23:48:22 +00:00
|
|
|
elif re.match(r'%s' % self.ansible, l):
|
|
|
|
comment = re.sub(r'%s' % self.ansible, '', l)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-08-20 21:56:41 +00:00
|
|
|
# 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
|
2017-09-25 23:48:22 +00:00
|
|
|
if not re.match(r'%s' % self.ansible, self.lines[i - 1]):
|
2016-08-20 21:56:41 +00:00
|
|
|
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
|
2017-09-25 23:48:22 +00:00
|
|
|
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]
|
2016-08-20 21:56:41 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
return []
|
|
|
|
|
2014-09-30 15:46:32 +00:00
|
|
|
def find_env(self, name):
|
|
|
|
for index, l in enumerate(self.lines):
|
2017-09-25 23:48:22 +00:00
|
|
|
if re.match(r'^%s=' % name, l):
|
2014-09-30 15:46:32 +00:00
|
|
|
return [index, l]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2017-09-25 23:48:22 +00:00
|
|
|
def get_cron_job(self, minute, hour, day, month, weekday, job, special, disabled):
|
2016-08-10 21:43:58 +00:00
|
|
|
# normalize any leading/trailing newlines (ansible/ansible-modules-core#3791)
|
|
|
|
job = job.strip('\r\n')
|
|
|
|
|
2015-06-28 16:29:31 +00:00
|
|
|
if disabled:
|
|
|
|
disable_prefix = '#'
|
|
|
|
else:
|
|
|
|
disable_prefix = ''
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
if special:
|
|
|
|
if self.cron_file:
|
2015-06-28 16:29:31 +00:00
|
|
|
return "%s@%s %s %s" % (disable_prefix, special, self.user, job)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2015-06-28 16:29:31 +00:00
|
|
|
return "%s@%s %s" % (disable_prefix, special, job)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
if self.cron_file:
|
2017-09-25 23:48:22 +00:00
|
|
|
return "%s%s %s %s %s %s %s %s" % (disable_prefix, minute, hour, day, month, weekday, self.user, job)
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2017-09-25 23:48:22 +00:00
|
|
|
return "%s%s %s %s %s %s %s" % (disable_prefix, minute, hour, day, month, weekday, job)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def get_jobnames(self):
|
|
|
|
jobnames = []
|
|
|
|
|
|
|
|
for l in self.lines:
|
2017-09-25 23:48:22 +00:00
|
|
|
if re.match(r'%s' % self.ansible, l):
|
|
|
|
jobnames.append(re.sub(r'%s' % self.ansible, '', l))
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
return jobnames
|
|
|
|
|
2014-09-30 15:46:32 +00:00
|
|
|
def get_envnames(self):
|
|
|
|
envnames = []
|
|
|
|
|
|
|
|
for l in self.lines:
|
2017-09-25 23:48:22 +00:00
|
|
|
if re.match(r'^\S+=', l):
|
2014-09-30 15:46:32 +00:00
|
|
|
envnames.append(l.split('=')[0])
|
|
|
|
|
|
|
|
return envnames
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def _update_job(self, name, job, addlinesfunction):
|
2016-08-20 21:56:41 +00:00
|
|
|
ansiblename = self.do_comment(name)
|
2014-09-26 01:01:01 +00:00
|
|
|
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:
|
2017-09-25 23:48:22 +00:00
|
|
|
return False # TODO add some more error testing
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2014-09-30 15:46:32 +00:00
|
|
|
def _update_env(self, name, decl, addenvfunction):
|
|
|
|
newlines = []
|
|
|
|
|
|
|
|
for l in self.lines:
|
2017-09-25 23:48:22 +00:00
|
|
|
if re.match(r'^%s=' % name, l):
|
2014-09-30 15:46:32 +00:00
|
|
|
addenvfunction(newlines, decl)
|
|
|
|
else:
|
|
|
|
newlines.append(l)
|
|
|
|
|
|
|
|
self.lines = newlines
|
|
|
|
|
2016-08-15 06:51:06 +00:00
|
|
|
def render(self):
|
2014-09-26 01:01:01 +00:00
|
|
|
"""
|
|
|
|
Render this crontab as it would be in the crontab.
|
|
|
|
"""
|
|
|
|
crons = []
|
|
|
|
for cron in self.lines:
|
|
|
|
crons.append(cron)
|
|
|
|
|
|
|
|
result = '\n'.join(crons)
|
2016-08-15 06:51:06 +00:00
|
|
|
if result:
|
2016-08-03 23:29:47 +00:00
|
|
|
result = result.rstrip('\r\n') + '\n'
|
2014-09-26 01:01:01 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _read_user_execute(self):
|
|
|
|
"""
|
|
|
|
Returns the command line for reading a crontab
|
|
|
|
"""
|
|
|
|
user = ''
|
|
|
|
if self.user:
|
|
|
|
if platform.system() == 'SunOS':
|
|
|
|
return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(CRONCMD))
|
|
|
|
elif platform.system() == 'AIX':
|
|
|
|
return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user))
|
|
|
|
elif platform.system() == 'HP-UX':
|
2017-09-25 23:48:22 +00:00
|
|
|
return "%s %s %s" % (CRONCMD, '-l', pipes.quote(self.user))
|
2016-09-12 05:39:12 +00:00
|
|
|
elif pwd.getpwuid(os.getuid())[0] != self.user:
|
2014-09-26 01:01:01 +00:00
|
|
|
user = '-u %s' % pipes.quote(self.user)
|
2017-09-25 23:48:22 +00:00
|
|
|
return "%s %s %s" % (CRONCMD, user, '-l')
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def _write_execute(self, path):
|
|
|
|
"""
|
|
|
|
Return the command line for writing a crontab
|
|
|
|
"""
|
|
|
|
user = ''
|
|
|
|
if self.user:
|
|
|
|
if platform.system() in ['SunOS', 'HP-UX', 'AIX']:
|
|
|
|
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-09-12 05:39:12 +00:00
|
|
|
elif pwd.getpwuid(os.getuid())[0] != self.user:
|
2014-09-26 01:01:01 +00:00
|
|
|
user = '-u %s' % pipes.quote(self.user)
|
2017-09-25 23:48:22 +00:00
|
|
|
return "%s %s %s" % (CRONCMD, user, pipes.quote(path))
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# The following example playbooks:
|
|
|
|
#
|
|
|
|
# - cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null"
|
|
|
|
#
|
|
|
|
# - name: do the job
|
|
|
|
# cron: name="do the job" hour="5,2" job="/some/dir/job.sh"
|
|
|
|
#
|
|
|
|
# - name: no job
|
|
|
|
# cron: name="an old job" state=absent
|
|
|
|
#
|
2014-09-30 15:46:32 +00:00
|
|
|
# - name: sets env
|
|
|
|
# cron: name="PATH" env=yes value="/bin:/usr/bin"
|
|
|
|
#
|
2014-09-26 01:01:01 +00:00
|
|
|
# Would produce:
|
2014-09-30 15:46:32 +00:00
|
|
|
# PATH=/bin:/usr/bin
|
2014-09-26 01:01:01 +00:00
|
|
|
# # Ansible: check dirs
|
|
|
|
# * * 5,2 * * ls -alh > /dev/null
|
|
|
|
# # Ansible: do the job
|
|
|
|
# * * 5,2 * * /some/dir/job.sh
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
2017-09-25 23:48:22 +00:00
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(type='str'),
|
|
|
|
user=dict(type='str'),
|
|
|
|
job=dict(type='str', aliases=['value']),
|
|
|
|
cron_file=dict(type='str'),
|
|
|
|
state=dict(type='str', default='present', choices=['present', 'absent']),
|
|
|
|
backup=dict(type='bool', default=False),
|
|
|
|
minute=dict(type='str', default='*'),
|
|
|
|
hour=dict(type='str', default='*'),
|
|
|
|
day=dict(type='str', default='*', aliases=['dom']),
|
|
|
|
month=dict(type='str', default='*'),
|
|
|
|
weekday=dict(type='str', default='*', aliases=['dow']),
|
|
|
|
reboot=dict(type='bool', default=False),
|
|
|
|
special_time=dict(type='str', choices=["reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly"]),
|
|
|
|
disabled=dict(type='bool', default=False),
|
|
|
|
env=dict(type='bool'),
|
|
|
|
insertafter=dict(type='str'),
|
|
|
|
insertbefore=dict(type='str'),
|
2014-09-26 01:01:01 +00:00
|
|
|
),
|
2017-09-25 23:48:22 +00:00
|
|
|
supports_check_mode=True,
|
2014-09-30 15:46:32 +00:00
|
|
|
mutually_exclusive=[
|
2017-01-29 07:28:53 +00:00
|
|
|
['reboot', 'special_time'],
|
|
|
|
['insertafter', 'insertbefore'],
|
2017-09-25 23:48:22 +00:00
|
|
|
],
|
Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option
This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.
- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.
As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).
This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)
```python
module = AnsibleModule(
argument_spec=dict(
path=dict(type='path', aliases=['dest', 'file']),
xmlstring=dict(type='str'),
xpath=dict(type='str'),
namespaces=dict(type='dict', default={}),
state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
value=dict(type='raw'),
attribute=dict(type='raw'),
add_children=dict(type='list'),
set_children=dict(type='list'),
count=dict(type='bool', default=False),
print_match=dict(type='bool', default=False),
pretty_print=dict(type='bool', default=False),
content=dict(type='str', choices=['attribute', 'text']),
input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
backup=dict(type='bool', default=False),
),
supports_check_mode=True,
required_by=dict(
add_children=['xpath'],
attribute=['value', 'xpath'],
content=['xpath'],
set_children=['xpath'],
value=['xpath'],
),
required_if=[
['count', True, ['xpath']],
['print_match', True, ['xpath']],
],
required_one_of=[
['path', 'xmlstring'],
['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
],
mutually_exclusive=[
['add_children', 'content', 'count', 'print_match','set_children', 'value'],
['path', 'xmlstring'],
],
)
```
* Rebase and fix conflict
* Add modules that use required_by functionality
* Update required_by schema
* Fix rebase issue
2019-02-15 00:57:45 +00:00
|
|
|
required_by=dict(
|
2019-02-19 19:29:51 +00:00
|
|
|
cron_file=('user',),
|
Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option
This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.
- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.
As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).
This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)
```python
module = AnsibleModule(
argument_spec=dict(
path=dict(type='path', aliases=['dest', 'file']),
xmlstring=dict(type='str'),
xpath=dict(type='str'),
namespaces=dict(type='dict', default={}),
state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
value=dict(type='raw'),
attribute=dict(type='raw'),
add_children=dict(type='list'),
set_children=dict(type='list'),
count=dict(type='bool', default=False),
print_match=dict(type='bool', default=False),
pretty_print=dict(type='bool', default=False),
content=dict(type='str', choices=['attribute', 'text']),
input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
backup=dict(type='bool', default=False),
),
supports_check_mode=True,
required_by=dict(
add_children=['xpath'],
attribute=['value', 'xpath'],
content=['xpath'],
set_children=['xpath'],
value=['xpath'],
),
required_if=[
['count', True, ['xpath']],
['print_match', True, ['xpath']],
],
required_one_of=[
['path', 'xmlstring'],
['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
],
mutually_exclusive=[
['add_children', 'content', 'count', 'print_match','set_children', 'value'],
['path', 'xmlstring'],
],
)
```
* Rebase and fix conflict
* Add modules that use required_by functionality
* Update required_by schema
* Fix rebase issue
2019-02-15 00:57:45 +00:00
|
|
|
),
|
|
|
|
required_if=(
|
2019-02-19 19:29:51 +00:00
|
|
|
('state', 'present', ('job',)),
|
Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option
This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.
- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.
As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).
This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)
```python
module = AnsibleModule(
argument_spec=dict(
path=dict(type='path', aliases=['dest', 'file']),
xmlstring=dict(type='str'),
xpath=dict(type='str'),
namespaces=dict(type='dict', default={}),
state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
value=dict(type='raw'),
attribute=dict(type='raw'),
add_children=dict(type='list'),
set_children=dict(type='list'),
count=dict(type='bool', default=False),
print_match=dict(type='bool', default=False),
pretty_print=dict(type='bool', default=False),
content=dict(type='str', choices=['attribute', 'text']),
input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
backup=dict(type='bool', default=False),
),
supports_check_mode=True,
required_by=dict(
add_children=['xpath'],
attribute=['value', 'xpath'],
content=['xpath'],
set_children=['xpath'],
value=['xpath'],
),
required_if=[
['count', True, ['xpath']],
['print_match', True, ['xpath']],
],
required_one_of=[
['path', 'xmlstring'],
['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
],
mutually_exclusive=[
['add_children', 'content', 'count', 'print_match','set_children', 'value'],
['path', 'xmlstring'],
],
)
```
* Rebase and fix conflict
* Add modules that use required_by functionality
* Update required_by schema
* Fix rebase issue
2019-02-15 00:57:45 +00:00
|
|
|
),
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
|
2017-09-25 23:48:22 +00: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']
|
2014-09-26 01:01:01 +00:00
|
|
|
special_time = module.params['special_time']
|
2017-09-25 23:48:22 +00:00
|
|
|
disabled = module.params['disabled']
|
|
|
|
env = module.params['env']
|
|
|
|
insertafter = module.params['insertafter']
|
2014-09-30 15:46:32 +00:00
|
|
|
insertbefore = module.params['insertbefore']
|
2017-09-25 23:48:22 +00:00
|
|
|
do_install = state == 'present'
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-09-25 23:48:22 +00:00
|
|
|
changed = False
|
|
|
|
res_args = dict()
|
|
|
|
warnings = list()
|
2017-07-21 21:51:34 +00:00
|
|
|
|
|
|
|
if cron_file:
|
|
|
|
cron_file_basename = os.path.basename(cron_file)
|
|
|
|
if not re.search(r'^[A-Z0-9_-]+$', cron_file_basename, re.I):
|
2017-09-25 23:48:22 +00:00
|
|
|
warnings.append('Filename portion of cron_file ("%s") should consist' % cron_file_basename +
|
|
|
|
' solely of upper- and lower-case letters, digits, underscores, and hyphens')
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# Ensure all files generated are only writable by the owning user. Primarily relevant for the cron_file option.
|
2016-05-17 17:19:19 +00:00
|
|
|
os.umask(int('022', 8))
|
2014-09-26 01:01:01 +00:00
|
|
|
crontab = CronTab(module, user, cron_file)
|
2015-10-01 14:16:34 +00:00
|
|
|
|
2015-10-01 04:12:54 +00:00
|
|
|
module.debug('cron instantiated - name: "%s"' % name)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2019-02-15 19:09:03 +00:00
|
|
|
if not name:
|
|
|
|
module.deprecate(
|
|
|
|
msg="The 'name' parameter will be required in future releases.",
|
2019-02-19 06:26:34 +00:00
|
|
|
version='2.12'
|
2019-02-15 19:09:03 +00:00
|
|
|
)
|
|
|
|
if reboot:
|
|
|
|
module.deprecate(
|
|
|
|
msg="The 'reboot' parameter will be removed in future releases. Use 'special_time' option instead.",
|
2019-02-19 06:26:34 +00:00
|
|
|
version='2.12'
|
2019-02-15 19:09:03 +00:00
|
|
|
)
|
|
|
|
|
2016-03-14 14:42:32 +00:00
|
|
|
if module._diff:
|
|
|
|
diff = dict()
|
2016-08-15 06:51:06 +00:00
|
|
|
diff['before'] = crontab.existing
|
2016-03-14 14:42:32 +00:00
|
|
|
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'
|
|
|
|
|
2014-09-26 01:01:01 +00: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.")
|
|
|
|
|
2017-06-01 22:04:12 +00:00
|
|
|
# cannot support special_time on solaris
|
|
|
|
if (special_time or reboot) and get_platform() == 'SunOS':
|
|
|
|
module.fail_json(msg="Solaris does not support special_time=... or @reboot")
|
|
|
|
|
2016-02-03 15:57:06 +00:00
|
|
|
if (insertafter or insertbefore) and not env and do_install:
|
2014-09-30 15:46:32 +00:00
|
|
|
module.fail_json(msg="Insertafter and insertbefore parameters are valid only with env=yes")
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if reboot:
|
2014-09-30 15:46:32 +00:00
|
|
|
special_time = "reboot"
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# if requested make a backup before making a change
|
2016-03-14 14:42:32 +00:00
|
|
|
if backup and not module.check_mode:
|
2014-09-26 01:01:01 +00:00
|
|
|
(backuph, backup_file) = tempfile.mkstemp(prefix='crontab')
|
|
|
|
crontab.write(backup_file)
|
|
|
|
|
|
|
|
if crontab.cron_file and not name and not do_install:
|
2016-03-14 14:42:32 +00: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()
|
2017-09-25 23:48:22 +00:00
|
|
|
module.exit_json(changed=changed, cron_file=cron_file, state=state, diff=diff)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2014-09-30 15:46:32 +00: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
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2014-09-30 15:46:32 +00:00
|
|
|
if do_install:
|
2017-07-27 06:19:36 +00:00
|
|
|
for char in ['\r', '\n']:
|
|
|
|
if char in job.strip('\r\n'):
|
|
|
|
warnings.append('Job should not contain line breaks')
|
|
|
|
break
|
|
|
|
|
2016-10-17 15:53:07 +00:00
|
|
|
job = crontab.get_cron_job(minute, hour, day, month, weekday, job, special_time, disabled)
|
2016-08-20 21:56:41 +00:00
|
|
|
old_job = crontab.find_job(name, job)
|
|
|
|
|
2014-09-30 15:46:32 +00:00
|
|
|
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
|
2016-08-20 21:56:41 +00:00
|
|
|
if len(old_job) > 2:
|
|
|
|
crontab.update_job(name, job)
|
|
|
|
changed = True
|
2014-09-30 15:46:32 +00:00
|
|
|
else:
|
2016-08-20 21:56:41 +00:00
|
|
|
old_job = crontab.find_job(name)
|
|
|
|
|
2014-09-30 15:46:32 +00:00
|
|
|
if len(old_job) > 0:
|
|
|
|
crontab.remove_job(name)
|
|
|
|
changed = True
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-08-03 23:29:47 +00:00
|
|
|
# no changes to env/job, but existing crontab needs a terminating newline
|
2018-10-18 20:38:08 +00:00
|
|
|
if not changed and crontab.existing != '':
|
2016-12-02 08:49:48 +00:00
|
|
|
if not (crontab.existing.endswith('\r') or crontab.existing.endswith('\n')):
|
|
|
|
changed = True
|
2016-08-03 23:29:47 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
res_args = dict(
|
2017-09-25 23:48:22 +00:00
|
|
|
jobs=crontab.get_jobnames(),
|
|
|
|
envs=crontab.get_envnames(),
|
|
|
|
warnings=warnings,
|
|
|
|
changed=changed
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if changed:
|
2016-03-14 14:42:32 +00: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
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# retain the backup only if crontab or cron file have changed
|
2017-05-26 14:16:15 +00:00
|
|
|
if backup and not module.check_mode:
|
2014-09-26 01:01:01 +00:00
|
|
|
if changed:
|
|
|
|
res_args['backup_file'] = backup_file
|
|
|
|
else:
|
2017-05-26 14:16:15 +00:00
|
|
|
os.unlink(backup_file)
|
2014-09-26 01:01:01 +00: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.")
|
|
|
|
|
|
|
|
|
2016-12-05 17:04:51 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|