diff --git a/changelogs/fragments/59765-cron-cronvar-use-get-bin-path.yaml b/changelogs/fragments/59765-cron-cronvar-use-get-bin-path.yaml new file mode 100644 index 00000000000..c93159594aa --- /dev/null +++ b/changelogs/fragments/59765-cron-cronvar-use-get-bin-path.yaml @@ -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) diff --git a/changelogs/fragments/cron-only-get-bin-path-once.yaml b/changelogs/fragments/cron-only-get-bin-path-once.yaml new file mode 100644 index 00000000000..7a03ff98153 --- /dev/null +++ b/changelogs/fragments/cron-only-get-bin-path-once.yaml @@ -0,0 +1,2 @@ +bugfixes: + - cron cronvar - only run ``get_bin_path()`` once diff --git a/lib/ansible/modules/system/cron.py b/lib/ansible/modules/system/cron.py index 1d290a3d82f..29c670e2c2b 100644 --- a/lib/ansible/modules/system/cron.py +++ b/lib/ansible/modules/system/cron.py @@ -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(): diff --git a/lib/ansible/modules/system/cronvar.py b/lib/ansible/modules/system/cronvar.py index 218a79fa76a..93bbfb37308 100644 --- a/lib/ansible/modules/system/cronvar.py +++ b/lib/ansible/modules/system/cronvar.py @@ -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)) # ==================================================