mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Allow to change executable (shell/interpreter) when using raw
This patch adds an optional 'executable=' option to the raw command line to override the default shell (/bin/sh), much like the shell module does.
This commit is contained in:
@@ -67,7 +67,7 @@ class Connection(object):
|
||||
|
||||
return self
|
||||
|
||||
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False):
|
||||
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False, executable='/bin/sh'):
|
||||
''' run a command on the remote host '''
|
||||
|
||||
vvv("EXEC COMMAND %s" % cmd)
|
||||
@@ -79,6 +79,7 @@ class Connection(object):
|
||||
mode='command',
|
||||
cmd=cmd,
|
||||
tmp_path=tmp_path,
|
||||
executable=executable,
|
||||
)
|
||||
data = utils.jsonify(data)
|
||||
data = utils.encrypt(self.key, data)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
import traceback
|
||||
import os
|
||||
import pipes
|
||||
import shutil
|
||||
import subprocess
|
||||
from ansible import errors
|
||||
@@ -36,20 +37,22 @@ class Connection(object):
|
||||
|
||||
return self
|
||||
|
||||
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False):
|
||||
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False, executable='/bin/sh'):
|
||||
''' run a command on the local host '''
|
||||
|
||||
if self.runner.sudo and sudoable:
|
||||
if not self.runner.sudo or not sudoable:
|
||||
local_cmd = [ executable, '-c', cmd]
|
||||
else:
|
||||
if self.runner.sudo_pass:
|
||||
# NOTE: if someone wants to add sudo w/ password to the local connection type, they are welcome
|
||||
# to do so. The primary usage of the local connection is for crontab and kickstart usage however
|
||||
# so this doesn't seem to be a huge priority
|
||||
raise errors.AnsibleError("sudo with password is presently only supported on the 'paramiko' (SSH) and native 'ssh' connection types")
|
||||
cmd = "sudo -u {0} -s {1}".format(sudo_user, cmd)
|
||||
sudocmd = "sudo -u %s -s %s -c %s" % (sudo_user, executable, cmd)
|
||||
local_cmd = ['/bin/sh', '-c', sudocmd]
|
||||
|
||||
vvv("EXEC %s" % cmd, host=self.host)
|
||||
basedir = self.runner.basedir
|
||||
p = subprocess.Popen(cmd, cwd=basedir, shell=True, stdin=None,
|
||||
vvv("EXEC %s" % local_cmd, host=self.host)
|
||||
p = subprocess.Popen(local_cmd, cwd=self.runner.basedir, executable=executable,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
return (p.returncode, '', stdout, stderr)
|
||||
|
||||
@@ -96,7 +96,7 @@ class Connection(object):
|
||||
|
||||
return ssh
|
||||
|
||||
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False):
|
||||
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False, executable='/bin/sh'):
|
||||
''' run a command on the remote host '''
|
||||
|
||||
bufsize = 4096
|
||||
@@ -110,7 +110,7 @@ class Connection(object):
|
||||
chan.get_pty()
|
||||
|
||||
if not self.runner.sudo or not sudoable:
|
||||
quoted_command = '/bin/sh -c ' + pipes.quote(cmd)
|
||||
quoted_command = executable + ' -c ' + pipes.quote(cmd)
|
||||
vvv("EXEC %s" % quoted_command, host=self.host)
|
||||
chan.exec_command(quoted_command)
|
||||
else:
|
||||
@@ -123,8 +123,8 @@ class Connection(object):
|
||||
# the -p option.
|
||||
randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
|
||||
prompt = '[sudo via ansible, key=%s] password: ' % randbits
|
||||
sudocmd = 'sudo -k && sudo -p "%s" -u %s /bin/sh -c %s' % (
|
||||
prompt, sudo_user, pipes.quote(cmd))
|
||||
sudocmd = 'sudo -k && sudo -p "%s" -u %s %s -c %s' % (
|
||||
prompt, sudo_user, executable, pipes.quote(cmd))
|
||||
shcmd = '/bin/sh -c ' + pipes.quote(sudocmd)
|
||||
vvv("EXEC %s" % shcmd, host=self.host)
|
||||
sudo_output = ''
|
||||
|
||||
@@ -81,13 +81,15 @@ class Connection(object):
|
||||
os.write(self.wfd, "%s\n" % self.runner.remote_pass)
|
||||
os.close(self.wfd)
|
||||
|
||||
def exec_command(self, cmd, tmp_path, sudo_user,sudoable=False):
|
||||
def exec_command(self, cmd, tmp_path, sudo_user,sudoable=False, executable='/bin/sh'):
|
||||
''' run a command on the remote host '''
|
||||
|
||||
ssh_cmd = self._password_cmd()
|
||||
ssh_cmd += ["ssh", "-tt", "-q"] + self.common_args + [self.host]
|
||||
|
||||
if self.runner.sudo and sudoable:
|
||||
if not self.runner.sudo or not sudoable:
|
||||
ssh_cmd.append(executable + ' -c ' + pipes.quote(cmd))
|
||||
else:
|
||||
# Rather than detect if sudo wants a password this time, -k makes
|
||||
# sudo always ask for a password if one is required.
|
||||
# Passing a quoted compound command to sudo (or sudo -s)
|
||||
@@ -97,10 +99,9 @@ class Connection(object):
|
||||
# the -p option.
|
||||
randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
|
||||
prompt = '[sudo via ansible, key=%s] password: ' % randbits
|
||||
sudocmd = 'sudo -k && sudo -p "%s" -u %s /bin/sh -c %s' % (
|
||||
prompt, sudo_user, pipes.quote(cmd))
|
||||
cmd = sudocmd
|
||||
ssh_cmd.append('/bin/sh -c ' + pipes.quote(cmd))
|
||||
sudocmd = 'sudo -k && sudo -p "%s" -u %s %s -c %s' % (
|
||||
prompt, sudo_user, executable, pipes.quote(cmd))
|
||||
ssh_cmd.append('/bin/sh -c ' + pipes.quote(sudocmd))
|
||||
|
||||
vvv("EXEC %s" % ssh_cmd, host=self.host)
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user