2012-03-27 04:07:04 +02:00
|
|
|
#!/usr/bin/env python
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-02-29 01:08:09 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 01:42:05 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# This file is part of Ansible
|
2012-02-24 01:42:05 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2012-02-24 01:42:05 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2012-02-24 01:42:05 +01:00
|
|
|
|
2012-03-03 03:11:43 +01:00
|
|
|
########################################################
|
|
|
|
|
2014-02-26 17:00:48 +01:00
|
|
|
import os
|
2012-02-28 10:00:31 +01:00
|
|
|
import sys
|
2012-03-18 22:41:44 +01:00
|
|
|
|
2012-05-02 14:03:40 +02:00
|
|
|
from ansible.runner import Runner
|
2012-02-24 05:26:16 +01:00
|
|
|
import ansible.constants as C
|
2012-03-18 22:41:44 +01:00
|
|
|
from ansible import utils
|
|
|
|
from ansible import errors
|
2012-03-26 01:05:27 +02:00
|
|
|
from ansible import callbacks
|
2012-04-21 17:46:32 +02:00
|
|
|
from ansible import inventory
|
2012-03-03 03:11:43 +01:00
|
|
|
########################################################
|
|
|
|
|
2012-02-23 20:56:14 +01:00
|
|
|
class Cli(object):
|
2012-03-03 03:11:43 +01:00
|
|
|
''' code behind bin/ansible '''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
|
|
|
# ----------------------------------------------
|
2012-02-23 20:56:14 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2012-03-26 01:05:27 +02:00
|
|
|
self.stats = callbacks.AggregateStats()
|
2012-03-27 03:17:11 +02:00
|
|
|
self.callbacks = callbacks.CliRunnerCallbacks()
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
# ----------------------------------------------
|
2012-03-03 03:08:48 +01:00
|
|
|
|
|
|
|
def parse(self):
|
2012-03-03 03:11:43 +01:00
|
|
|
''' create an options parser for bin/ansible '''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-02-08 04:51:33 +01:00
|
|
|
parser = utils.base_parser(
|
|
|
|
constants=C,
|
|
|
|
runas_opts=True,
|
|
|
|
subset_opts=True,
|
|
|
|
async_opts=True,
|
|
|
|
output_opts=True,
|
|
|
|
connect_opts=True,
|
|
|
|
check_opts=True,
|
|
|
|
diff_opts=False,
|
|
|
|
usage='%prog <host-pattern> [options]'
|
|
|
|
)
|
|
|
|
|
2012-04-10 19:51:58 +02:00
|
|
|
parser.add_option('-a', '--args', dest='module_args',
|
|
|
|
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
|
|
|
|
parser.add_option('-m', '--module-name', dest='module_name',
|
2012-08-07 02:07:02 +02:00
|
|
|
help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME,
|
2012-04-13 03:30:49 +02:00
|
|
|
default=C.DEFAULT_MODULE_NAME)
|
2013-02-08 04:51:33 +01:00
|
|
|
|
2012-02-23 20:56:14 +01:00
|
|
|
options, args = parser.parse_args()
|
2012-08-07 02:07:02 +02:00
|
|
|
self.callbacks.options = options
|
2012-03-27 03:17:11 +02:00
|
|
|
|
2012-03-02 04:10:47 +01:00
|
|
|
if len(args) == 0 or len(args) > 1:
|
|
|
|
parser.print_help()
|
2012-02-28 10:00:31 +01:00
|
|
|
sys.exit(1)
|
2014-01-21 02:19:03 +01:00
|
|
|
|
|
|
|
# su and sudo command line arguments need to be mutually exclusive
|
|
|
|
if (options.su or options.su_user or options.ask_su_pass) and \
|
|
|
|
(options.sudo or options.sudo_user or options.ask_sudo_pass):
|
|
|
|
parser.error("Sudo arguments ('--sudo', '--sudo-user', and '--ask-sudo-pass') "
|
|
|
|
"and su arguments ('-su', '--su-user', and '--ask-su-pass') are "
|
|
|
|
"mutually exclusive")
|
|
|
|
|
2014-02-26 17:00:48 +01:00
|
|
|
if (options.ask_vault_pass and options.vault_password_file):
|
|
|
|
parser.error("--ask-vault-pass and --vault-password-file are mutually exclusive")
|
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
return (options, args)
|
|
|
|
|
|
|
|
# ----------------------------------------------
|
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
def run(self, options, args):
|
2012-03-03 03:11:43 +01:00
|
|
|
''' use Runner lib to do SSH things '''
|
2012-02-23 22:07:10 +01:00
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
pattern = args[0]
|
2012-04-21 17:46:32 +02:00
|
|
|
|
2014-02-19 17:23:46 +01:00
|
|
|
"""
|
2012-04-21 17:46:32 +02:00
|
|
|
inventory_manager = inventory.Inventory(options.inventory)
|
2013-02-05 21:16:26 +01:00
|
|
|
if options.subset:
|
|
|
|
inventory_manager.subset(options.subset)
|
2012-04-21 17:46:32 +02:00
|
|
|
hosts = inventory_manager.list_hosts(pattern)
|
|
|
|
if len(hosts) == 0:
|
2014-07-28 16:48:22 +02:00
|
|
|
callbacks.display("No hosts matched", stderr=True)
|
2013-11-06 06:37:31 +01:00
|
|
|
sys.exit(0)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-12-07 21:50:25 +01:00
|
|
|
if options.listhosts:
|
|
|
|
for host in hosts:
|
2013-04-27 16:24:26 +02:00
|
|
|
callbacks.display(' %s' % host)
|
2012-12-07 21:50:25 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2013-04-11 19:43:31 +02:00
|
|
|
if ((options.module_name == 'command' or options.module_name == 'shell')
|
|
|
|
and not options.module_args):
|
2013-04-27 16:24:26 +02:00
|
|
|
callbacks.display("No argument passed to %s module" % options.module_name, color='red', stderr=True)
|
2012-12-07 21:50:25 +01:00
|
|
|
sys.exit(1)
|
2014-02-19 17:23:46 +01:00
|
|
|
"""
|
2012-12-07 21:50:25 +01:00
|
|
|
|
2012-02-25 00:13:11 +01:00
|
|
|
sshpass = None
|
2012-04-14 01:33:19 +02:00
|
|
|
sudopass = None
|
2014-01-21 02:19:03 +01:00
|
|
|
su_pass = None
|
2014-02-26 17:00:48 +01:00
|
|
|
vault_pass = None
|
|
|
|
|
2012-11-02 16:53:26 +01:00
|
|
|
options.ask_pass = options.ask_pass or C.DEFAULT_ASK_PASS
|
2013-08-07 14:05:59 +02:00
|
|
|
# Never ask for an SSH password when we run with local connection
|
|
|
|
if options.connection == "local":
|
2013-08-10 23:59:05 +02:00
|
|
|
options.ask_pass = False
|
|
|
|
options.ask_sudo_pass = options.ask_sudo_pass or C.DEFAULT_ASK_SUDO_PASS
|
2014-01-21 02:19:03 +01:00
|
|
|
options.ask_su_pass = options.ask_su_pass or C.DEFAULT_ASK_SU_PASS
|
2014-02-26 17:00:48 +01:00
|
|
|
options.ask_vault_pass = options.ask_vault_pass or C.DEFAULT_ASK_VAULT_PASS
|
|
|
|
|
2014-02-19 17:23:46 +01:00
|
|
|
(sshpass, sudopass, su_pass, vault_pass) = utils.ask_passwords(ask_pass=options.ask_pass, ask_sudo_pass=options.ask_sudo_pass, ask_su_pass=options.ask_su_pass, ask_vault_pass=options.ask_vault_pass)
|
|
|
|
|
2014-02-26 17:00:48 +01:00
|
|
|
# read vault_pass from a file
|
2014-06-03 16:34:42 +02:00
|
|
|
if not options.ask_vault_pass and options.vault_password_file:
|
|
|
|
vault_pass = utils.read_vault_file(options.vault_password_file)
|
2014-02-26 17:00:48 +01:00
|
|
|
|
2014-03-17 18:05:30 +01:00
|
|
|
inventory_manager = inventory.Inventory(options.inventory, vault_password=vault_pass)
|
2014-02-19 17:23:46 +01:00
|
|
|
if options.subset:
|
|
|
|
inventory_manager.subset(options.subset)
|
|
|
|
hosts = inventory_manager.list_hosts(pattern)
|
|
|
|
if len(hosts) == 0:
|
2014-07-28 16:48:22 +02:00
|
|
|
callbacks.display("No hosts matched", stderr=True)
|
2014-02-19 17:23:46 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if options.listhosts:
|
|
|
|
for host in hosts:
|
|
|
|
callbacks.display(' %s' % host)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if ((options.module_name == 'command' or options.module_name == 'shell')
|
|
|
|
and not options.module_args):
|
|
|
|
callbacks.display("No argument passed to %s module" % options.module_name, color='red', stderr=True)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2014-01-21 02:19:03 +01:00
|
|
|
if options.su_user or options.ask_su_pass:
|
|
|
|
options.su = True
|
2012-05-07 17:37:50 +02:00
|
|
|
options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER
|
2014-01-21 02:19:03 +01:00
|
|
|
options.su_user = options.su_user or C.DEFAULT_SU_USER
|
2012-03-27 03:17:11 +02:00
|
|
|
if options.tree:
|
|
|
|
utils.prepare_writeable_dir(options.tree)
|
2012-02-25 00:13:11 +01:00
|
|
|
|
2012-05-02 14:03:40 +02:00
|
|
|
runner = Runner(
|
2014-01-21 02:19:03 +01:00
|
|
|
module_name=options.module_name,
|
|
|
|
module_path=options.module_path,
|
2012-03-31 04:47:58 +02:00
|
|
|
module_args=options.module_args,
|
2014-01-21 02:19:03 +01:00
|
|
|
remote_user=options.remote_user,
|
|
|
|
remote_pass=sshpass,
|
|
|
|
inventory=inventory_manager,
|
|
|
|
timeout=options.timeout,
|
2012-05-14 22:22:05 +02:00
|
|
|
private_key_file=options.private_key_file,
|
2012-08-07 02:07:02 +02:00
|
|
|
forks=options.forks,
|
|
|
|
pattern=pattern,
|
2014-01-21 02:19:03 +01:00
|
|
|
callbacks=self.callbacks,
|
|
|
|
sudo=options.sudo,
|
|
|
|
sudo_pass=sudopass,
|
|
|
|
sudo_user=options.sudo_user,
|
|
|
|
transport=options.connection,
|
|
|
|
subset=options.subset,
|
2013-02-08 04:51:33 +01:00
|
|
|
check=options.check,
|
2014-01-21 02:19:03 +01:00
|
|
|
diff=options.check,
|
|
|
|
su=options.su,
|
|
|
|
su_pass=su_pass,
|
2014-02-19 17:23:46 +01:00
|
|
|
su_user=options.su_user,
|
|
|
|
vault_pass=vault_pass
|
2012-03-12 01:54:54 +01:00
|
|
|
)
|
|
|
|
|
2012-04-26 20:35:19 +02:00
|
|
|
if options.seconds:
|
2013-04-27 16:24:26 +02:00
|
|
|
callbacks.display("background launch...\n\n", color='cyan')
|
2012-05-26 01:18:02 +02:00
|
|
|
results, poller = runner.run_async(options.seconds)
|
2012-04-26 20:35:19 +02:00
|
|
|
results = self.poll_while_needed(poller, options)
|
|
|
|
else:
|
|
|
|
results = runner.run()
|
2012-03-12 01:54:54 +01:00
|
|
|
|
2012-04-26 20:35:19 +02:00
|
|
|
return (runner, results)
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
# ----------------------------------------------
|
2012-02-28 09:54:41 +01:00
|
|
|
|
2012-04-26 20:35:19 +02:00
|
|
|
def poll_while_needed(self, poller, options):
|
2012-03-03 03:11:43 +01:00
|
|
|
''' summarize results from Runner '''
|
2012-02-28 09:54:41 +01:00
|
|
|
|
2012-03-12 01:54:54 +01:00
|
|
|
# BACKGROUND POLL LOGIC when -B and -P are specified
|
|
|
|
if options.seconds and options.poll_interval > 0:
|
2012-04-26 20:35:19 +02:00
|
|
|
poller.wait(options.seconds, options.poll_interval)
|
|
|
|
|
|
|
|
return poller.results
|
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
|
2012-03-03 03:11:43 +01:00
|
|
|
########################################################
|
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
if __name__ == '__main__':
|
2013-04-27 16:24:26 +02:00
|
|
|
callbacks.display("", log_only=True)
|
|
|
|
callbacks.display(" ".join(sys.argv), log_only=True)
|
|
|
|
callbacks.display("", log_only=True)
|
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
cli = Cli()
|
2012-03-03 03:08:48 +01:00
|
|
|
(options, args) = cli.parse()
|
2012-03-13 04:11:54 +01:00
|
|
|
try:
|
|
|
|
(runner, results) = cli.run(options, args)
|
2012-10-03 22:11:14 +02:00
|
|
|
for result in results['contacted'].values():
|
|
|
|
if 'failed' in result or result.get('rc', 0) != 0:
|
2012-10-04 12:43:25 +02:00
|
|
|
sys.exit(2)
|
2012-10-03 22:11:14 +02:00
|
|
|
if results['dark']:
|
2013-08-14 22:26:59 +02:00
|
|
|
sys.exit(3)
|
2012-03-29 20:19:09 +02:00
|
|
|
except errors.AnsibleError, e:
|
2012-03-13 04:11:54 +01:00
|
|
|
# Generic handler for ansible specific errors
|
2013-04-27 16:24:26 +02:00
|
|
|
callbacks.display("ERROR: %s" % str(e), stderr=True, color='red')
|
2012-03-13 04:11:54 +01:00
|
|
|
sys.exit(1)
|
2012-03-14 01:59:05 +01:00
|
|
|
|