From eb33135fd15750d01b66dd7e84c03d1a23f96a54 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Mon, 8 Apr 2019 15:41:27 +0100 Subject: [PATCH] add feature to search for and return a single cron line --- salt/modules/cron.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/salt/modules/cron.py b/salt/modules/cron.py index c833c807a174..85bd3a19b5b9 100644 --- a/salt/modules/cron.py +++ b/salt/modules/cron.py @@ -12,6 +12,7 @@ # Import python libs import os import random +import logging # Import salt libs import salt.utils.data @@ -27,6 +28,8 @@ SALT_CRON_IDENTIFIER = 'SALT_CRON_IDENTIFIER' SALT_CRON_NO_IDENTIFIER = 'NO ID SET' +log = logging.getLogger(__name__) + def __virtual__(): if salt.utils.path.which('crontab'): @@ -405,6 +408,36 @@ def list_tab(user): ls = salt.utils.functools.alias_function(list_tab, 'ls') +def get_entry(user, identifier=None, cmd=None): + ''' + Return the specified entry from user's crontab. + identifier will be used if specified, otherwise will lookup cmd + Either identifier or cmd should be specified. + + user: + User's crontab to query + + identifier: + Search for line with identifier + + cmd: + Search for cron line with cmd + + CLI Example: + + .. code-block:: bash + + salt '*' cron.identifier_exists root identifier=task1 + ''' + cron_entries = list_tab(user).get('crons', False) + for cron_entry in cron_entries: + if identifier and cron_entry.get('identifier') == identifier: + return cron_entry + elif cmd and cron_entry.get('cmd') == cmd: + return cron_entry + return False + + def set_special(user, special, cmd,