From 8d39ae709c8bbd0dc5a139f73bf886231ebad360 Mon Sep 17 00:00:00 2001 From: Jim Richardson Date: Thu, 3 May 2012 17:11:21 -0700 Subject: [PATCH] sudo -u works now. Needs testing and maybe a bit of cleanup --- lib/ansible/connection.py | 10 ++++++---- lib/ansible/runner.py | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/ansible/connection.py b/lib/ansible/connection.py index e99f1b32e5..6d34938819 100644 --- a/lib/ansible/connection.py +++ b/lib/ansible/connection.py @@ -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) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index c09409d9a3..5c52c17f13 100644 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -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: