diff --git a/bin/ansible b/bin/ansible index d23c732260..61806cc9dc 100755 --- a/bin/ansible +++ b/bin/ansible @@ -20,7 +20,6 @@ ######################################################## import sys -import getpass from ansible.runner import Runner import ansible.constants as C @@ -101,13 +100,9 @@ class Cli(object): sshpass = None sudopass = None options.ask_pass = options.ask_pass or C.DEFAULT_ASK_PASS - if options.ask_pass: - sshpass = getpass.getpass(prompt="SSH password: ") options.ask_sudo_pass= options.ask_sudo_pass or C.DEFAULT_ASK_SUDO_PASS - if options.ask_sudo_pass: - sudopass = getpass.getpass(prompt="sudo password: ") - options.sudo = True - if options.sudo_user: + ( sshpass, sudopass ) = utils.ask_passwords(ask_pass=options.ask_pass, ask_sudo_pass=options.ask_sudo_pass) + if options.sudo_user or options.ask_sudo_pass: options.sudo = True options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER if options.tree: diff --git a/bin/ansible-playbook b/bin/ansible-playbook index 5bcdb0cd63..a241b8e996 100755 --- a/bin/ansible-playbook +++ b/bin/ansible-playbook @@ -19,7 +19,6 @@ ####################################################### import sys -import getpass import os import ansible.playbook @@ -93,13 +92,9 @@ def main(args): sudopass = None if not options.listhosts and not options.syntax and not options.listtasks: options.ask_pass = options.ask_pass or C.DEFAULT_ASK_PASS - if options.ask_pass: - sshpass = getpass.getpass(prompt="SSH password: ") options.ask_sudo_pass = options.ask_sudo_pass or C.DEFAULT_ASK_SUDO_PASS - if options.ask_sudo_pass: - sudopass = getpass.getpass(prompt="sudo password: ") - options.sudo = True - if options.sudo_user: + ( sshpass, sudopass ) = utils.ask_passwords(ask_pass=options.ask_pass, ask_sudo_pass=options.ask_sudo_pass) + if options.sudo_user or options.ask_sudo_pass: options.sudo = True options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER extra_vars = utils.parse_kv(options.extra_vars) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 325c3e7fcb..7a737acffe 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -38,6 +38,7 @@ import random import difflib import warnings import traceback +import getpass VERBOSITY=0 @@ -496,6 +497,22 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, return parser +def ask_passwords(ask_pass=False, ask_sudo_pass=False): + sshpass = None + sudopass = None + sudo_prompt = "sudo password: " + + if ask_pass: + sshpass = getpass.getpass(prompt="SSH password: ") + sudo_prompt = "sudo password [defaults to SSH password]: " + + if ask_sudo_pass: + sudopass = getpass.getpass(prompt=sudo_prompt) + if ask_pass and sudopass == '': + sudopass = sshpass + + return (sshpass, sudopass) + def do_encrypt(result, encrypt, salt_size=None, salt=None): if PASSLIB_AVAILABLE: try: