[stable-2.8] cron - Use get_bin_path() to find executable (#62462) (#62546)

* [stable-2.8] cron - Use the default crontab executable in cron and cronvar modules (#59765)

In some remote environments, the `crontab` executable is
overloaded with a custom executable, which typically does
some pre/post processing before forwarding to crontab.

Instead of using the hardcoded `/usr/bin/crontab`, this uses
the `get_bin_path` utility to locate the default crontab executable..
(cherry picked from commit 951a80c8b0)

Co-authored-by: Jean-Frédéric <JeanFred@users.noreply.github.com>

* [stable-2.8] cron - Only run get_bin_path() once (#62554)

(cherry picked from commit b7897e3a8d)
pull/62703/head
Sam Doran 6 years ago committed by Toshio Kuratomi
parent 912c493307
commit e8b14e02fe

@ -0,0 +1,2 @@
bugfixes:
- cron and cronvar - use get_bin_path utility to locate the default crontab executable instead of the hardcoded /usr/bin/crontab. (https://github.com/ansible/ansible/pull/59765)

@ -0,0 +1,2 @@
bugfixes:
- cron cronvar - only run ``get_bin_path()`` once

@ -216,9 +216,6 @@ import tempfile
from ansible.module_utils.basic import AnsibleModule, get_platform
CRONCMD = "/usr/bin/crontab"
class CronTabError(Exception):
pass
@ -238,6 +235,7 @@ class CronTab(object):
self.lines = None
self.ansible = "#Ansible: "
self.existing = ''
self.cron_cmd = self.module.get_bin_path('crontab', required=True)
if cron_file:
if os.path.isabs(cron_file):
@ -514,14 +512,14 @@ class CronTab(object):
user = ''
if self.user:
if platform.system() == 'SunOS':
return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(CRONCMD))
return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(self.cron_cmd))
elif platform.system() == 'AIX':
return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user))
return "%s -l %s" % (pipes.quote(self.cron_cmd), pipes.quote(self.user))
elif platform.system() == 'HP-UX':
return "%s %s %s" % (CRONCMD, '-l', pipes.quote(self.user))
return "%s %s %s" % (self.cron_cmd, '-l', pipes.quote(self.user))
elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % pipes.quote(self.user)
return "%s %s %s" % (CRONCMD, user, '-l')
return "%s %s %s" % (self.cron_cmd, user, '-l')
def _write_execute(self, path):
"""
@ -530,10 +528,11 @@ class CronTab(object):
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))
return "chown %s %s ; su '%s' -c '%s %s'" % (
pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), self.cron_cmd, pipes.quote(path))
elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % pipes.quote(self.user)
return "%s %s %s" % (CRONCMD, user, pipes.quote(path))
return "%s %s %s" % (self.cron_cmd, user, pipes.quote(path))
def main():

@ -108,8 +108,6 @@ import tempfile
from ansible.module_utils.basic import AnsibleModule
CRONCMD = "/usr/bin/crontab"
class CronVarError(Exception):
pass
@ -128,6 +126,7 @@ class CronVar(object):
self.user = user
self.lines = None
self.wordchars = ''.join(chr(x) for x in range(128) if chr(x) not in ('=', "'", '"',))
self.cron_cmd = self.module.get_bin_path('cronvar', required=True)
if cron_file:
self.cron_file = ""
@ -296,14 +295,14 @@ class CronVar(object):
if self.user:
if platform.system() == 'SunOS':
return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(CRONCMD))
return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(self.cron_cmd))
elif platform.system() == 'AIX':
return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user))
return "%s -l %s" % (pipes.quote(self.cron_cmd), pipes.quote(self.user))
elif platform.system() == 'HP-UX':
return "%s %s %s" % (CRONCMD, '-l', pipes.quote(self.user))
return "%s %s %s" % (self.cron_cmd, '-l', pipes.quote(self.user))
elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % pipes.quote(self.user)
return "%s %s %s" % (CRONCMD, user, '-l')
return "%s %s %s" % (self.cron_cmd, user, '-l')
def _write_execute(self, path):
"""
@ -312,10 +311,11 @@ class CronVar(object):
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))
return "chown %s %s ; su '%s' -c '%s %s'" % (
pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), self.cron_cmd, pipes.quote(path))
elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % pipes.quote(self.user)
return "%s %s %s" % (CRONCMD, user, pipes.quote(path))
return "%s %s %s" % (self.cron_cmd, user, pipes.quote(path))
# ==================================================

Loading…
Cancel
Save