Added preliminary support for --sudo to ansible, playbook support and further testing pending.

This commit is contained in:
Michael DeHaan 2012-03-29 01:36:29 -04:00
parent 011a0ecbdf
commit 81e3496037
4 changed files with 14 additions and 22 deletions

View file

@ -67,6 +67,8 @@ class Cli(object):
help='condense output') help='condense output')
parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int', parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int',
dest='poll_interval', help='set the poll interval if using -B') dest='poll_interval', help='set the poll interval if using -B')
parser.add_option("-s", "--sudo", default=False, action="store_true",
dest='sudo', help="run operations with sudo (nopasswd)")
parser.add_option('-t', '--tree', dest='tree', default=None, parser.add_option('-t', '--tree', dest='tree', default=None,
help='log output to this directory') help='log output to this directory')
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int', parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
@ -107,7 +109,7 @@ class Cli(object):
host_list=options.inventory, timeout=options.timeout, host_list=options.inventory, timeout=options.timeout,
remote_port=options.remote_port, forks=options.forks, remote_port=options.remote_port, forks=options.forks,
background=options.seconds, pattern=pattern, background=options.seconds, pattern=pattern,
callbacks=self.callbacks, verbose=True, callbacks=self.callbacks, sudo=options.sudo, verbose=True,
) )
return (runner, runner.run()) return (runner, runner.run())

View file

@ -45,7 +45,6 @@ def main(args):
help="run playbook against these hosts regardless of inventory settings") help="run playbook against these hosts regardless of inventory settings")
parser.add_option('-p', '--port', default=C.DEFAULT_REMOTE_PORT, type='int', parser.add_option('-p', '--port', default=C.DEFAULT_REMOTE_PORT, type='int',
dest='remote_port', help='set the remote ssh port') dest='remote_port', help='set the remote ssh port')
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int', parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
dest='timeout', help="set the SSH timeout in seconds") dest='timeout', help="set the SSH timeout in seconds")

View file

@ -87,17 +87,19 @@ class ParamikoConnection(object):
stdin, stdout, stderr = self.ssh.exec_command(cmd) stdin, stdout, stderr = self.ssh.exec_command(cmd)
return (stdin, stdout, stderr) return (stdin, stdout, stderr)
else: else:
# this code is a work in progress, so it's disabled...
self.ssh.close() self.ssh.close()
ssh_sudo = self._get_conn() ssh_sudo = self._get_conn()
sudo_chan = ssh_sudo.invoke_shell() sudo_chan = ssh_sudo.invoke_shell()
sudo_chan.send("sudo -s\n") sudo_chan.send("sudo -s\n")
sudo_chan.recv(1024) sudo_chan.send("echo 'START==>';%s;echo '<==STOP'\n" % cmd)
sudo_chan.send("echo && %s && echo\n" % cmd) timeout = 60 # make configurable?
while not sudo_chan.recv_ready():
# TODO: timeout support
time.sleep(1) time.sleep(1)
out = sudo_chan.recv(1024) while not sudo_chan.recv_ready():
time.sleep(1)
timeout -= 1
if timeout < 0:
return (None, json.dumps(dict(failed=True, msg="sudo timeout")), '')
out = sudo_chan.recv(2058)
sudo_chan.close() sudo_chan.close()
self.ssh = self._get_conn() self.ssh = self._get_conn()
out = self._expect_like(out) out = self._expect_like(out)
@ -105,18 +107,9 @@ class ParamikoConnection(object):
def _expect_like(self, msg): def _expect_like(self, msg):
''' hack to make invoke_shell more or less work for sudo usage ''' ''' hack to make invoke_shell more or less work for sudo usage '''
lines = msg.split("\n") left = msg.rindex("START==>")
index = 0 right = msg.rindex("<==STOP")
for x in lines: return msg[left+8:right].lstrip().rstrip()
if x == '\r':
break
index += 1
index2 = index+1
for x in lines[index:]:
if x == '\r':
break
index2 += 1
return "\n".join(lines[index+1:index2+1]).strip()
def put_file(self, in_path, out_path): def put_file(self, in_path, out_path):
''' transfer a file from local to remote ''' ''' transfer a file from local to remote '''

View file

@ -79,9 +79,7 @@ elif state == "restarted":
# run change commands if we need to # run change commands if we need to
def _run(cmd): def _run(cmd):
return subprocess.call(cmd, return subprocess.call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
rc = 0 rc = 0
if changed: if changed: