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]) _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.runner = runner
self.transport = transport self.transport = transport
self.sudo_user = sudo_user
def connect(self, host, port=None): def connect(self, host, port=None):
conn = None conn = None
if self.transport == 'local' and self._LOCALHOSTRE.search(host): if self.transport == 'local' and self._LOCALHOSTRE.search(host):
@ -126,7 +126,8 @@ class ParamikoConnection(object):
self.ssh = self._get_conn() self.ssh = self._get_conn()
return self 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 ''' ''' run a command on the remote host '''
bufsize = 4096 bufsize = 4096
chan = self.ssh.get_transport().open_session() chan = self.ssh.get_transport().open_session()
@ -146,7 +147,8 @@ class ParamikoConnection(object):
# the -p option. # the -p option.
randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32)) randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
prompt = '[sudo via ansible, key=%s] password: ' % randbits 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 = '' sudo_output = ''
try: try:
chan.exec_command(sudocmd) chan.exec_command(sudocmd)

View file

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