ansible/bin/ansible

175 lines
6 KiB
Text
Raw Normal View History

#!/usr/bin/env python
2012-02-23 20:56:14 +01:00
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
2012-02-24 01:42:05 +01:00
#
# This file is part of Ansible
2012-02-24 01:42:05 +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
#
# 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
########################################################
import sys
import getpass
import time
from optparse import OptionParser
import ansible.runner
import ansible.constants as C
from ansible import utils
from ansible import errors
from ansible import callbacks
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-02-23 20:56:14 +01:00
def __init__(self):
self.stats = callbacks.AggregateStats()
self.callbacks = callbacks.CliRunnerCallbacks()
self.silent_callbacks = callbacks.DefaultRunnerCallbacks()
2012-02-23 20:56:14 +01:00
# ----------------------------------------------
def parse(self):
2012-03-03 03:11:43 +01:00
''' create an options parser for bin/ansible '''
options = {
'-a' : dict(long='--args', dest='module_args',
help="module arguments", default=C.DEFAULT_MODULE_ARGS),
'-m' : dict(long='--module-name', dest='module_name',
help="module name to execute", default=C.DEFAULT_MODULE_NAME)
}
parser = utils.make_parser(
options,
usage='ansible <host-pattern> [options]',
runas_opts=True,
async_opts=True,
output_opts=True,
)
2012-03-29 01:31:17 +02:00
2012-02-23 20:56:14 +01:00
options, args = parser.parse_args()
self.callbacks.options = options
if len(args) == 0 or len(args) > 1:
parser.print_help()
sys.exit(1)
return (options, args)
# ----------------------------------------------
def run(self, options, args):
2012-03-03 03:11:43 +01:00
''' use Runner lib to do SSH things '''
pattern = args[0]
sshpass = None
if options.ask_pass:
sshpass = getpass.getpass(prompt="SSH password: ")
if options.tree:
utils.prepare_writeable_dir(options.tree)
if options.seconds:
print "background launch...\n\n"
runner = ansible.runner.Runner(
module_name=options.module_name, module_path=options.module_path,
module_args=options.module_args,
remote_user=options.remote_user, remote_pass=sshpass,
host_list=options.inventory, timeout=options.timeout,
remote_port=options.remote_port, forks=options.forks,
background=options.seconds, pattern=pattern,
callbacks=self.callbacks, sudo=options.sudo, verbose=True,
debug=options.debug
)
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,
remote_port=old_runner.remote_port, pattern='*',
callbacks=self.silent_callbacks, 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
# ----------------------------------------------
def poll_if_needed(self, runner, results, options, args):
2012-03-03 03:11:43 +01:00
''' summarize results from Runner '''
if results is None:
exit("No hosts matched")
# 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
print utils.async_poll_status(jid, host, clock, host_result)
for (host, host_result) in poll_results['dark'].iteritems():
print "FAILED: %s => %s" % (host, host_result)
clock = clock - options.poll_interval
time.sleep(options.poll_interval)
poll_hosts = self.hosts_to_poll(poll_results)
2012-03-03 03:11:43 +01:00
########################################################
if __name__ == '__main__':
cli = Cli()
(options, args) = cli.parse()
try:
(runner, results) = cli.run(options, args)
2012-03-29 20:19:09 +02:00
except errors.AnsibleError, e:
# Generic handler for ansible specific errors
print "ERROR: %s" % str(e)
sys.exit(1)
else:
cli.poll_if_needed(runner, results, options, args)