diff --git a/bin/ansible b/bin/ansible index 978bddf33ce..cb853eaa504 100755 --- a/bin/ansible +++ b/bin/ansible @@ -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()) diff --git a/bin/ansible-playbook b/bin/ansible-playbook index 4c2afd736d2..cd276bff97f 100755 --- a/bin/ansible-playbook +++ b/bin/ansible-playbook @@ -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") diff --git a/lib/ansible/connection.py b/lib/ansible/connection.py index b83d84267a3..11838a18a7d 100755 --- a/lib/ansible/connection.py +++ b/lib/ansible/connection.py @@ -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) + sudo_chan.send("echo 'START==>';%s;echo '<==STOP'\n" % cmd) + timeout = 60 # make configurable? + time.sleep(1) while not sudo_chan.recv_ready(): - # TODO: timeout support time.sleep(1) - out = sudo_chan.recv(1024) + 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 ''' diff --git a/library/service b/library/service index f0e5da101b7..65a8e8c4c7a 100755 --- a/library/service +++ b/library/service @@ -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: