Fix non-sudo execution, add --sudo-user to ansible-playbook.

This commit is contained in:
John Kleint 2012-05-07 11:37:50 -04:00 committed by Michael DeHaan
parent dcbe48e2d4
commit 710d085def
4 changed files with 13 additions and 8 deletions

View file

@ -52,8 +52,6 @@ class Cli(object):
parser.add_option('-m', '--module-name', dest='module_name', parser.add_option('-m', '--module-name', dest='module_name',
help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME, help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME,
default=C.DEFAULT_MODULE_NAME) default=C.DEFAULT_MODULE_NAME)
parser.add_option('-U','--sudo-user', dest='sudo_user', help='desired sudo user (default=root)',
default=C.DEFAULT_SUDO_USER)
options, args = parser.parse_args() options, args = parser.parse_args()
self.callbacks.options = options self.callbacks.options = options
@ -72,19 +70,19 @@ class Cli(object):
inventory_manager = inventory.Inventory(options.inventory) inventory_manager = inventory.Inventory(options.inventory)
hosts = inventory_manager.list_hosts(pattern) hosts = inventory_manager.list_hosts(pattern)
if len(hosts) == 0: if len(hosts) == 0:
print >>sys.stderr, "No hosts matched" print >>sys.stderr, "No hosts matched"
sys.exit(1) sys.exit(1)
sshpass = None sshpass = None
sudopass = None sudopass = None
sudouser = options.sudo_user
if options.ask_pass: if options.ask_pass:
sshpass = getpass.getpass(prompt="SSH password: ") sshpass = getpass.getpass(prompt="SSH password: ")
if options.ask_sudo_pass: if options.ask_sudo_pass:
sudopass = getpass.getpass(prompt="sudo password: ") sudopass = getpass.getpass(prompt="sudo password: ")
options.sudo = True
if options.sudo_user: if options.sudo_user:
options.sudo=True options.sudo = True
options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER
if options.tree: if options.tree:
utils.prepare_writeable_dir(options.tree) utils.prepare_writeable_dir(options.tree)

View file

@ -50,6 +50,10 @@ def main(args):
sshpass = getpass.getpass(prompt="SSH password: ") sshpass = getpass.getpass(prompt="SSH password: ")
if options.ask_sudo_pass: if options.ask_sudo_pass:
sudopass = getpass.getpass(prompt="sudo password: ") sudopass = getpass.getpass(prompt="sudo password: ")
options.sudo = True
if options.sudo_user:
options.sudo = True
options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER
override_hosts = None override_hosts = None
if options.override_hosts: if options.override_hosts:
override_hosts = options.override_hosts.split(",") override_hosts = options.override_hosts.split(",")
@ -77,6 +81,7 @@ def main(args):
timeout=options.timeout, timeout=options.timeout,
transport=options.connection, transport=options.connection,
sudo=options.sudo, sudo=options.sudo,
sudo_user=options.sudo_user,
sudo_pass=sudopass, sudo_pass=sudopass,
extra_vars=extra_vars extra_vars=extra_vars
) )

View file

@ -62,7 +62,7 @@ class PlayBook(object):
runner_callbacks = None, runner_callbacks = None,
stats = None, stats = None,
sudo = False, sudo = False,
sudo_user = 'root', sudo_user = C.DEFAULT_SUDO_USER,
extra_vars = None): extra_vars = None):
""" """

View file

@ -309,6 +309,8 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, asyn
if runas_opts: if runas_opts:
parser.add_option("-s", "--sudo", default=False, action="store_true", parser.add_option("-s", "--sudo", default=False, action="store_true",
dest='sudo', help="run operations with sudo (nopasswd)") dest='sudo', help="run operations with sudo (nopasswd)")
parser.add_option('-U', '--sudo-user', dest='sudo_user', help='desired sudo user (default=root)',
default=None) # Can't default to root because we need to detect when this option was given
parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER, parser.add_option('-u', '--user', default=constants.DEFAULT_REMOTE_USER,
dest='remote_user', dest='remote_user',
help='connect as this user (default=%s)' % constants.DEFAULT_REMOTE_USER) help='connect as this user (default=%s)' % constants.DEFAULT_REMOTE_USER)