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')
parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int',
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,
help='log output to this directory')
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,
remote_port=options.remote_port, forks=options.forks,
background=options.seconds, pattern=pattern,
callbacks=self.callbacks, verbose=True,
callbacks=self.callbacks, sudo=options.sudo, verbose=True,
)
return (runner, runner.run())

View file

@ -45,7 +45,6 @@ def main(args):
help="run playbook against these hosts regardless of inventory settings")
parser.add_option('-p', '--port', default=C.DEFAULT_REMOTE_PORT, type='int',
dest='remote_port', help='set the remote ssh port')
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
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)
return (stdin, stdout, stderr)
else:
# this code is a work in progress, so it's disabled...
self.ssh.close()
ssh_sudo = self._get_conn()
sudo_chan = ssh_sudo.invoke_shell()
sudo_chan.send("sudo -s\n")
sudo_chan.recv(1024)
sudo_chan.send("echo && %s && echo\n" % cmd)
while not sudo_chan.recv_ready():
# TODO: timeout support
sudo_chan.send("echo 'START==>';%s;echo '<==STOP'\n" % cmd)
timeout = 60 # make configurable?
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()
self.ssh = self._get_conn()
out = self._expect_like(out)
@ -105,18 +107,9 @@ class ParamikoConnection(object):
def _expect_like(self, msg):
''' hack to make invoke_shell more or less work for sudo usage '''
lines = msg.split("\n")
index = 0
for x in lines:
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()
left = msg.rindex("START==>")
right = msg.rindex("<==STOP")
return msg[left+8:right].lstrip().rstrip()
def put_file(self, in_path, out_path):
''' transfer a file from local to remote '''

View file

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