2012-02-23 20:56:14 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
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
|
|
|
########################################################
|
|
|
|
|
2012-02-28 10:00:31 +01:00
|
|
|
import sys
|
2012-02-25 00:13:11 +01:00
|
|
|
import getpass
|
2012-02-25 05:35:09 +01:00
|
|
|
import shlex
|
2012-03-12 01:54:54 +01:00
|
|
|
import time
|
|
|
|
from optparse import OptionParser
|
2012-03-18 22:41:44 +01:00
|
|
|
|
2012-02-24 05:28:58 +01:00
|
|
|
import ansible.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-02-23 20:56:14 +01:00
|
|
|
|
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-03-03 03:08:48 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------
|
2012-02-23 20:56:14 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
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-03-03 03:08:48 +01:00
|
|
|
|
2012-03-02 04:10:47 +01:00
|
|
|
parser = OptionParser(usage = 'ansible <host-pattern> [options]')
|
2012-03-02 03:18:32 +01:00
|
|
|
parser.add_option("-a", "--args", dest="module_args",
|
|
|
|
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
|
2012-03-11 23:40:35 +01:00
|
|
|
parser.add_option("-B", "--background", dest="seconds", type='int', default=0,
|
2012-03-03 18:25:56 +01:00
|
|
|
help="run asynchronously, failing after X seconds")
|
2012-02-28 10:20:25 +01:00
|
|
|
parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int',
|
2012-03-02 04:10:47 +01:00
|
|
|
help='number of parallel processes to use')
|
|
|
|
parser.add_option("-i", "--inventory-file", dest="inventory",
|
|
|
|
help="inventory host file", default=C.DEFAULT_HOST_LIST)
|
2012-03-02 03:18:32 +01:00
|
|
|
parser.add_option("-k", "--ask-pass", default=False, action="store_true",
|
2012-03-02 04:10:47 +01:00
|
|
|
help="ask for SSH password")
|
|
|
|
parser.add_option("-M", "--module-path", dest="module_path",
|
2012-03-02 03:18:32 +01:00
|
|
|
help="path to module library", default=C.DEFAULT_MODULE_PATH)
|
2012-03-02 04:10:47 +01:00
|
|
|
parser.add_option("-m", "--module-name", dest="module_name",
|
|
|
|
help="module name to execute", default=C.DEFAULT_MODULE_NAME)
|
2012-02-28 09:54:41 +01:00
|
|
|
parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
|
2012-03-02 04:10:47 +01:00
|
|
|
help="condense output")
|
2012-03-11 23:40:35 +01:00
|
|
|
parser.add_option('-P', '--poll', default=C.DEFAULT_POLL_INTERVAL, type='int',
|
|
|
|
dest='poll_interval', help="set the poll interval if using -B")
|
2012-02-28 09:57:43 +01:00
|
|
|
parser.add_option('-t', '--tree', dest='tree', default=None,
|
2012-03-02 04:10:47 +01:00
|
|
|
help="log output to this directory")
|
2012-03-02 01:54:17 +01:00
|
|
|
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
|
2012-03-02 04:10:47 +01:00
|
|
|
dest='timeout', help="set the SSH timeout in seconds")
|
2012-03-02 03:18:32 +01:00
|
|
|
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
|
2012-03-02 04:10:47 +01:00
|
|
|
dest='remote_user', help='connect as this user')
|
2012-02-23 20:56:14 +01:00
|
|
|
options, args = parser.parse_args()
|
2012-03-03 03:08:48 +01: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)
|
2012-03-03 03:08:48 +01:00
|
|
|
return (options, args)
|
|
|
|
|
|
|
|
# ----------------------------------------------
|
|
|
|
|
|
|
|
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-02-25 00:13:11 +01:00
|
|
|
sshpass = None
|
2012-02-28 09:54:41 +01:00
|
|
|
if options.ask_pass:
|
2012-02-25 00:13:11 +01:00
|
|
|
sshpass = getpass.getpass(prompt="SSH password: ")
|
|
|
|
|
2012-03-12 01:54:54 +01:00
|
|
|
runner = ansible.runner.Runner(
|
2012-02-28 07:33:22 +01:00
|
|
|
module_name=options.module_name,
|
|
|
|
module_path=options.module_path,
|
|
|
|
module_args=shlex.split(options.module_args),
|
|
|
|
remote_user=options.remote_user,
|
|
|
|
remote_pass=sshpass,
|
2012-03-02 03:18:32 +01:00
|
|
|
host_list=options.inventory,
|
2012-03-02 01:54:17 +01:00
|
|
|
timeout=options.timeout,
|
2012-02-28 07:33:22 +01:00
|
|
|
forks=options.forks,
|
2012-03-03 18:25:56 +01:00
|
|
|
background=options.seconds,
|
2012-03-02 04:10:47 +01:00
|
|
|
pattern=pattern,
|
2012-02-28 07:33:22 +01:00
|
|
|
verbose=True,
|
2012-03-12 01:54:54 +01:00
|
|
|
)
|
|
|
|
return (runner, runner.run())
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------
|
|
|
|
|
|
|
|
def get_polling_runner(self, old_runner, hosts, jid):
|
|
|
|
return ansible.runner.Runner(
|
|
|
|
module_name='async_status',
|
|
|
|
module_path=old_runner.module_path,
|
|
|
|
module_args=[ "jid=%s" % jid ],
|
|
|
|
remote_user=old_runner.remote_user,
|
|
|
|
remote_pass=old_runner.remote_pass,
|
|
|
|
host_list=hosts,
|
|
|
|
timeout=old_runner.timeout,
|
|
|
|
forks=old_runner.forks,
|
|
|
|
pattern='*',
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# ----------------------------------------------
|
|
|
|
|
|
|
|
def hosts_to_poll(self, results):
|
|
|
|
hosts = []
|
|
|
|
for (host, res) in results['contacted'].iteritems():
|
|
|
|
if res.get('started',False):
|
|
|
|
hosts.append(host)
|
|
|
|
return hosts
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
# ----------------------------------------------
|
2012-02-28 09:54:41 +01:00
|
|
|
|
2012-03-12 01:54:54 +01:00
|
|
|
def output(self, runner, results, options, args):
|
2012-03-03 03:11:43 +01:00
|
|
|
''' summarize results from Runner '''
|
2012-02-28 09:54:41 +01:00
|
|
|
|
2012-03-02 04:10:47 +01:00
|
|
|
if results is None:
|
2012-03-03 03:08:48 +01:00
|
|
|
exit("No hosts matched")
|
2012-02-28 09:57:43 +01:00
|
|
|
if options.tree:
|
2012-03-18 22:41:44 +01:00
|
|
|
utils.prepare_writeable_dir(options.tree)
|
2012-03-03 03:08:48 +01:00
|
|
|
|
2012-03-12 01:54:54 +01:00
|
|
|
# BACKGROUND POLL LOGIC when -B and -P are specified
|
|
|
|
# FIXME: refactor
|
|
|
|
if options.seconds and options.poll_interval > 0:
|
|
|
|
poll_hosts = results['contacted'].keys()
|
|
|
|
if len(poll_hosts) == 0:
|
|
|
|
exit("no jobs were launched successfully")
|
|
|
|
ahost = poll_hosts[0]
|
|
|
|
jid = results['contacted'][ahost].get('ansible_job_id', None)
|
|
|
|
if jid is None:
|
|
|
|
exit("unexpected error: unable to determine jid")
|
|
|
|
|
|
|
|
clock = options.seconds
|
|
|
|
while (clock >= 0):
|
|
|
|
polling_runner = self.get_polling_runner(runner, poll_hosts, jid)
|
|
|
|
poll_results = polling_runner.run()
|
|
|
|
if poll_results is None:
|
|
|
|
break
|
|
|
|
for (host, host_result) in poll_results['contacted'].iteritems():
|
|
|
|
# override last result with current status result for report
|
|
|
|
results['contacted'][host] = host_result
|
2012-03-18 22:41:44 +01:00
|
|
|
print utils.async_poll_status(jid, host, clock, host_result)
|
2012-03-12 01:54:54 +01:00
|
|
|
clock = clock - options.poll_interval
|
|
|
|
time.sleep(options.poll_interval)
|
|
|
|
poll_hosts = self.hosts_to_poll(poll_results)
|
|
|
|
for (host, host_result) in results['contacted'].iteritems():
|
|
|
|
if 'started' in host_result:
|
|
|
|
results['contacted'][host] = { 'failed' : 1, 'rc' : None, 'msg' : 'timed out' }
|
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
buf = ''
|
2012-03-18 22:41:44 +01:00
|
|
|
for hostname in utils.contacted_hosts(results):
|
|
|
|
msg = utils.host_report_msg(
|
2012-03-03 03:08:48 +01:00
|
|
|
hostname,
|
|
|
|
options.module_name,
|
2012-03-18 22:41:44 +01:00
|
|
|
utils.contacted_host_result(results, hostname),
|
2012-03-03 03:08:48 +01:00
|
|
|
options.one_line
|
|
|
|
)
|
|
|
|
if options.tree:
|
2012-03-18 22:41:44 +01:00
|
|
|
utils.write_tree_file(options.tree, hostname, utils.bigjson(utils.contacted_host_result(results, hostname)))
|
2012-03-03 03:08:48 +01:00
|
|
|
buf += msg
|
2012-03-02 04:10:47 +01:00
|
|
|
|
2012-03-18 22:41:44 +01:00
|
|
|
if utils.has_dark_hosts(results):
|
|
|
|
buf += utils.dark_hosts_msg(results)
|
2012-03-03 03:08:48 +01:00
|
|
|
|
2012-03-18 22:53:58 +01:00
|
|
|
if not utils.has_hosts(results):
|
|
|
|
print "ERROR: no hosts matched"
|
|
|
|
|
2012-03-03 03:08:48 +01:00
|
|
|
print buf
|
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__':
|
|
|
|
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-03-18 22:41:44 +01:00
|
|
|
except errors.AnsibleError as e:
|
2012-03-13 04:11:54 +01:00
|
|
|
# Generic handler for ansible specific errors
|
2012-03-14 01:59:05 +01:00
|
|
|
print "ERROR: %s" % str(e)
|
2012-03-13 04:11:54 +01:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
cli.output(runner, results, options, args)
|
2012-03-14 01:59:05 +01:00
|
|
|
|