From 08b45d6da1d546fa64db076da58c56ea496685ae Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Fri, 24 Feb 2012 18:13:11 -0500 Subject: [PATCH] add support to prompt for ssh password on the cli --- bin/ansible | 8 ++++++++ lib/ansible/__init__.py | 7 +++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/bin/ansible b/bin/ansible index bb8cce7295..690fcb6dea 100755 --- a/bin/ansible +++ b/bin/ansible @@ -24,6 +24,7 @@ from optparse import OptionParser import json import os +import getpass import ansible DEFAULT_HOST_LIST = '/etc/ansible/hosts' @@ -41,6 +42,8 @@ class Cli(object): def runner(self): parser = OptionParser() + parser.add_option("-P", "--askpass", default=False, action="store_true", + help="ask the user to input the ssh password for connecting") parser.add_option("-H", "--host-list", dest="host_list", help="path to hosts list", default=DEFAULT_HOST_LIST) parser.add_option("-L", "--library", dest="module_path", @@ -61,11 +64,16 @@ class Cli(object): # TODO: more shell like splitting on module_args would # be a good idea + sshpass = None + if options.askpass: + sshpass = getpass.getpass(prompt="SSH password: ") + return ansible.Runner( module_name=options.module_name, module_path=options.module_path, module_args=options.module_args.split(' '), remote_user=options.remote_user, + remote_pass=sshpass, host_list=options.host_list, forks=options.forks, pattern=options.pattern, diff --git a/lib/ansible/__init__.py b/lib/ansible/__init__.py index b57aee7aaa..f4f3d9a4b2 100755 --- a/lib/ansible/__init__.py +++ b/lib/ansible/__init__.py @@ -36,6 +36,7 @@ DEFAULT_FORKS = 3 DEFAULT_MODULE_ARGS = '' DEFAULT_TIMEOUT = 60 DEFAULT_REMOTE_USER = 'root' +DEFAULT_REMOTE_PASS = None def _executor_hook(x): ''' callback used by multiprocessing pool ''' @@ -53,6 +54,7 @@ class Runner(object): timeout=DEFAULT_TIMEOUT, pattern=DEFAULT_PATTERN, remote_user=DEFAULT_REMOTE_USER, + remote_pass=DEFAULT_REMOTE_PASS, verbose=False): @@ -69,6 +71,7 @@ class Runner(object): self.timeout = timeout self.verbose = verbose self.remote_user = remote_user + self.remote_pass = remote_pass def _parse_hosts(self, host_list): ''' parse the host inventory file if not sent as an array ''' @@ -95,8 +98,8 @@ class Runner(object): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: - ssh.connect(host, username=self.remote_user, - allow_agent=True, look_for_keys=True) + ssh.connect(host, username=self.remote_user, allow_agent=True, + look_for_keys=True, password=self.remote_pass) return [ True, ssh ] except: return [ False, traceback.format_exc() ]