sudo -u works now. Needs testing and maybe a bit of cleanup

This commit is contained in:
Jim Richardson 2012-05-03 17:11:21 -07:00 committed by Michael DeHaan
parent 93a20a33e9
commit 8d39ae709c
2 changed files with 10 additions and 8 deletions

View file

@ -44,10 +44,10 @@ class Connection(object):
_LOCALHOSTRE = re.compile(r"^(127.0.0.1|localhost|%s)$" % os.uname()[1])
def __init__(self, runner, transport):
def __init__(self, runner, transport,sudo_user):
self.runner = runner
self.transport = transport
self.sudo_user = sudo_user
def connect(self, host, port=None):
conn = None
if self.transport == 'local' and self._LOCALHOSTRE.search(host):
@ -126,7 +126,8 @@ class ParamikoConnection(object):
self.ssh = self._get_conn()
return self
def exec_command(self, cmd, tmp_path, sudoable=False):
def exec_command(self, cmd, tmp_path,sudo_user,sudoable=False):
''' run a command on the remote host '''
bufsize = 4096
chan = self.ssh.get_transport().open_session()
@ -146,7 +147,8 @@ class ParamikoConnection(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 -p "%s" -- "$SHELL" -c %s' % (prompt, pipes.quote(cmd))
sudocmd = 'sudo -k -p "%s" -u %s -- "$SHELL" -c %s' % (prompt,
sudo_user, pipes.quote(cmd))
sudo_output = ''
try:
chan.exec_command(sudocmd)

View file

@ -116,8 +116,9 @@ class Runner(object):
self.generated_jid = str(random.randint(0, 999999999999))
self.sudo_user = sudo_user
self.transport = transport
self.connector = ansible.connection.Connection(self, self.transport)
self.connector = ansible.connection.Connection(self, self.transport, self.sudo_user)
if inventory is None:
self.inventory = ansible.inventory.Inventory(host_list)
@ -144,7 +145,6 @@ class Runner(object):
self.basedir = basedir
self.sudo = sudo
self.sudo_pass = sudo_pass
self.sudo_user = sudo_user
self.is_playbook = is_playbook
euid = pwd.getpwuid(os.geteuid())[0]
@ -625,8 +625,8 @@ class Runner(object):
def _exec_command(self, conn, cmd, tmp, sudoable=False):
''' execute a command string over SSH, return the output '''
stdin, stdout, stderr = conn.exec_command(cmd, tmp, sudoable=sudoable)
sudo_user = self.sudo_user
stdin, stdout, stderr = conn.exec_command(cmd, tmp, sudo_user,sudoable=sudoable)
err=None
out=None
if type(stderr) != str: