From e8b14e02feb7dfab214e40f221d19765d73b93a9 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Fri, 20 Sep 2019 19:27:10 -0400 Subject: [PATCH] [stable-2.8] cron - Use get_bin_path() to find executable (#62462) (#62546) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [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 951a80c8b019d74c5d6bad3e19bfac853d916231) Co-authored-by: Jean-Frédéric * [stable-2.8] cron - Only run get_bin_path() once (#62554) (cherry picked from commit b7897e3a8de5e7b8d4624e933bb28900a46ab773) --- .../59765-cron-cronvar-use-get-bin-path.yaml | 2 ++ .../fragments/cron-only-get-bin-path-once.yaml | 2 ++ lib/ansible/modules/system/cron.py | 17 ++++++++--------- lib/ansible/modules/system/cronvar.py | 16 ++++++++-------- 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 changelogs/fragments/59765-cron-cronvar-use-get-bin-path.yaml create mode 100644 changelogs/fragments/cron-only-get-bin-path-once.yaml 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)) # ==================================================