2012-02-28 07:12:04 +01:00
|
|
|
#!/usr/bin/python -tt
|
2012-02-28 08:11:51 +01:00
|
|
|
|
|
|
|
# skvidal, (c) Red Hat, Inc 2012
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-29 01:09:30 +01:00
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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-28 07:12:04 +01:00
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# TODO: add 'execution time' option output to the output
|
2012-02-28 07:12:04 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import getpass
|
|
|
|
import ansible.runner
|
|
|
|
import shlex
|
2012-02-28 06:28:43 +01:00
|
|
|
from ansible.scripts import base_ans_parser, error_print
|
2012-02-28 07:12:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
2012-02-28 07:01:44 +01:00
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
|
|
|
|
# ==================================
|
|
|
|
# parse options
|
2012-02-28 07:40:56 +01:00
|
|
|
parser = base_ans_parser()
|
2012-02-28 07:12:04 +01:00
|
|
|
parser.usage = "ans-command [options] command-to-run"
|
2012-02-28 07:01:44 +01:00
|
|
|
|
2012-02-28 08:38:36 +01:00
|
|
|
parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
|
2012-02-28 07:01:44 +01:00
|
|
|
help="output results on one line to make grepping easier, however will \
|
|
|
|
not remove newlines from command output")
|
2012-02-28 08:38:36 +01:00
|
|
|
parser.add_option('-t', '--tree', dest='output_dest', default=None,
|
2012-02-28 08:11:51 +01:00
|
|
|
help="if specified, a directory name to save output to, one file per host")
|
2012-02-28 07:01:44 +01:00
|
|
|
|
2012-02-28 07:12:04 +01:00
|
|
|
options, args = parser.parse_args(args)
|
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# get user's password if not supplied
|
|
|
|
# TODO: move into lib/ansible/scripts.py (?)
|
2012-02-28 07:12:04 +01:00
|
|
|
sshpass = None
|
|
|
|
if options.askpass:
|
|
|
|
sshpass = getpass.getpass(prompt="SSH password: ")
|
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# if specifying output destination (aka tree output saves), create the
|
|
|
|
# directory to output to
|
|
|
|
|
2012-02-28 07:12:04 +01:00
|
|
|
if options.output_dest:
|
|
|
|
if options.output_dest[0] != '/':
|
|
|
|
options.output_dest = os.path.realpath(os.path.expanduser(options.output_dest))
|
|
|
|
if not os.path.exists(options.output_dest):
|
|
|
|
try:
|
|
|
|
os.makedirs(options.output_dest)
|
|
|
|
except (IOError, OSError), e:
|
|
|
|
print >> sys.stderr, "Could not make dir %s: %s" % (options.output_dest, e)
|
|
|
|
sys.exit(1)
|
|
|
|
if not os.access(options.output_dest, os.W_OK):
|
|
|
|
print >> sys.stderr, "Cannot write to path %s" % options.output_dest
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# if no arguments are specified, error out
|
|
|
|
|
2012-02-28 06:28:43 +01:00
|
|
|
if len(args) == 0:
|
|
|
|
print >> sys.stderr, "Missing argument. What should be executed?"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# make the actual ansible API call
|
|
|
|
|
2012-02-28 07:12:04 +01:00
|
|
|
mycmd = ' '.join(args)
|
|
|
|
runner = ansible.runner.Runner(
|
|
|
|
module_name='command',
|
|
|
|
module_path=options.module_path,
|
|
|
|
module_args=shlex.split(mycmd),
|
|
|
|
remote_user=options.remote_user,
|
|
|
|
remote_pass=sshpass,
|
|
|
|
host_list=options.host_list,
|
|
|
|
forks=options.forks,
|
2012-02-28 07:34:28 +01:00
|
|
|
pattern=options.pattern,
|
2012-02-28 07:12:04 +01:00
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
results = runner.run()
|
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# walk through results and summarize them neatly
|
|
|
|
|
2012-02-28 07:59:21 +01:00
|
|
|
for hostname in sorted(results['contacted']):
|
|
|
|
|
|
|
|
result = results['contacted'][hostname]
|
|
|
|
rc = result.get('rc',0)
|
|
|
|
failed = result.get('failed', 0)
|
|
|
|
stdout = result.get('stdout', '')
|
|
|
|
stderr = result.get('stderr', '')
|
|
|
|
traceback = result.get('traceback', '')
|
|
|
|
error = result.get('error', '')
|
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# detect and show failures, if any
|
|
|
|
|
2012-02-28 07:59:21 +01:00
|
|
|
if rc != 0 or failed:
|
2012-02-28 08:11:51 +01:00
|
|
|
msg = "Error: %s: \n" % hostname
|
2012-02-28 07:59:21 +01:00
|
|
|
msg += stdout
|
|
|
|
msg += stderr
|
|
|
|
msg += traceback
|
|
|
|
msg += error
|
2012-02-28 06:28:43 +01:00
|
|
|
error_print(msg)
|
2012-02-28 07:12:04 +01:00
|
|
|
continue
|
|
|
|
|
2012-02-28 06:28:43 +01:00
|
|
|
if options.one_line:
|
2012-02-28 08:11:51 +01:00
|
|
|
# try to print everything on one line, but don't strip newlines
|
|
|
|
# if the command output happend to be too long
|
2012-02-28 07:59:21 +01:00
|
|
|
msg = "(stdout) %s" % stdout
|
|
|
|
if stderr.rstrip() != '':
|
|
|
|
msg = "(stdout) %s (stderr) %s" % (stdout,stderr)
|
|
|
|
print "%s | rc=%s | %s" % (
|
|
|
|
hostname, rc, msg
|
|
|
|
)
|
2012-02-28 07:12:04 +01:00
|
|
|
else:
|
2012-02-28 08:11:51 +01:00
|
|
|
# summarize response from command in multiple lines
|
2012-02-28 08:03:07 +01:00
|
|
|
buf = ''
|
|
|
|
buf += "%s | rc=%s >>\n" % (hostname, rc)
|
|
|
|
buf += stdout
|
2012-02-28 07:59:21 +01:00
|
|
|
if stderr:
|
2012-02-28 08:03:07 +01:00
|
|
|
buf += stderr
|
|
|
|
print buf
|
|
|
|
if options.output_dest:
|
|
|
|
path = os.path.join(options.output_dest, hostname)
|
|
|
|
fd = open(path, "w+")
|
|
|
|
fd.write(buf)
|
|
|
|
fd.close()
|
2012-02-28 07:12:04 +01:00
|
|
|
|
2012-02-28 08:11:51 +01:00
|
|
|
# print errors for hosts we could not reach
|
2012-02-28 07:12:04 +01:00
|
|
|
if results['dark']:
|
2012-02-28 08:11:51 +01:00
|
|
|
print ''
|
2012-02-28 06:28:43 +01:00
|
|
|
error_print('Hosts which could not be contacted or did not respond:')
|
2012-02-28 07:59:21 +01:00
|
|
|
failed_hosts = results['dark'].keys()
|
2012-02-28 08:11:51 +01:00
|
|
|
for hostname in failed_hosts:
|
|
|
|
error_print("%s:\n%s\n" % (hostname, results['dark'][hostname]))
|
2012-02-28 07:12:04 +01:00
|
|
|
print ''
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main(sys.argv[1:]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|