Fix output on failed hosts
This commit is contained in:
parent
b9b0240543
commit
42767b30fe
1 changed files with 29 additions and 12 deletions
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/python -tt
|
#!/usr/bin/python -tt
|
||||||
#skvidal, (c) Red Hat, Inc 2012
|
|
||||||
|
# skvidal, (c) Red Hat, Inc 2012
|
||||||
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
||||||
|
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
|
@ -17,8 +19,7 @@
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
#todo
|
# TODO: add 'execution time' option output to the output
|
||||||
# add 'execution time' option output to the output
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
@ -30,6 +31,9 @@ from ansible.scripts import base_ans_parser, error_print
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
|
|
||||||
|
|
||||||
|
# ==================================
|
||||||
|
# parse options
|
||||||
parser = base_ans_parser()
|
parser = base_ans_parser()
|
||||||
parser.usage = "ans-command [options] command-to-run"
|
parser.usage = "ans-command [options] command-to-run"
|
||||||
|
|
||||||
|
@ -37,15 +41,19 @@ def main(args):
|
||||||
help="output results on one line to make grepping easier, however will \
|
help="output results on one line to make grepping easier, however will \
|
||||||
not remove newlines from command output")
|
not remove newlines from command output")
|
||||||
parser.add_option('-o', '--output-dir', dest='output_dest', default=None,
|
parser.add_option('-o', '--output-dir', dest='output_dest', default=None,
|
||||||
help="output each host's results to a file in a dir named for the host")
|
help="if specified, a directory name to save output to, one file per host")
|
||||||
|
|
||||||
options, args = parser.parse_args(args)
|
options, args = parser.parse_args(args)
|
||||||
|
|
||||||
# TODO: move into lib/ansible/scripts.py
|
# get user's password if not supplied
|
||||||
|
# TODO: move into lib/ansible/scripts.py (?)
|
||||||
sshpass = None
|
sshpass = None
|
||||||
if options.askpass:
|
if options.askpass:
|
||||||
sshpass = getpass.getpass(prompt="SSH password: ")
|
sshpass = getpass.getpass(prompt="SSH password: ")
|
||||||
|
|
||||||
|
# if specifying output destination (aka tree output saves), create the
|
||||||
|
# directory to output to
|
||||||
|
|
||||||
if options.output_dest:
|
if options.output_dest:
|
||||||
if options.output_dest[0] != '/':
|
if options.output_dest[0] != '/':
|
||||||
options.output_dest = os.path.realpath(os.path.expanduser(options.output_dest))
|
options.output_dest = os.path.realpath(os.path.expanduser(options.output_dest))
|
||||||
|
@ -55,17 +63,19 @@ def main(args):
|
||||||
except (IOError, OSError), e:
|
except (IOError, OSError), e:
|
||||||
print >> sys.stderr, "Could not make dir %s: %s" % (options.output_dest, e)
|
print >> sys.stderr, "Could not make dir %s: %s" % (options.output_dest, e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not os.access(options.output_dest, os.W_OK):
|
if not os.access(options.output_dest, os.W_OK):
|
||||||
print >> sys.stderr, "Cannot write to path %s" % options.output_dest
|
print >> sys.stderr, "Cannot write to path %s" % options.output_dest
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# if no arguments are specified, error out
|
||||||
|
|
||||||
if len(args) == 0:
|
if len(args) == 0:
|
||||||
print >> sys.stderr, "Missing argument. What should be executed?"
|
print >> sys.stderr, "Missing argument. What should be executed?"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
mycmd = ' '.join(args)
|
# make the actual ansible API call
|
||||||
|
|
||||||
|
mycmd = ' '.join(args)
|
||||||
runner = ansible.runner.Runner(
|
runner = ansible.runner.Runner(
|
||||||
module_name='command',
|
module_name='command',
|
||||||
module_path=options.module_path,
|
module_path=options.module_path,
|
||||||
|
@ -77,9 +87,10 @@ def main(args):
|
||||||
pattern=options.pattern,
|
pattern=options.pattern,
|
||||||
verbose=True,
|
verbose=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
results = runner.run()
|
results = runner.run()
|
||||||
|
|
||||||
|
# walk through results and summarize them neatly
|
||||||
|
|
||||||
for hostname in sorted(results['contacted']):
|
for hostname in sorted(results['contacted']):
|
||||||
|
|
||||||
result = results['contacted'][hostname]
|
result = results['contacted'][hostname]
|
||||||
|
@ -90,9 +101,10 @@ def main(args):
|
||||||
traceback = result.get('traceback', '')
|
traceback = result.get('traceback', '')
|
||||||
error = result.get('error', '')
|
error = result.get('error', '')
|
||||||
|
|
||||||
|
# detect and show failures, if any
|
||||||
|
|
||||||
if rc != 0 or failed:
|
if rc != 0 or failed:
|
||||||
msg = 'Error: %s: ' % hostname
|
msg = "Error: %s: \n" % hostname
|
||||||
# too bad stdout/stderr is not interleaved :(
|
|
||||||
msg += stdout
|
msg += stdout
|
||||||
msg += stderr
|
msg += stderr
|
||||||
msg += traceback
|
msg += traceback
|
||||||
|
@ -101,6 +113,8 @@ def main(args):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if options.one_line:
|
if options.one_line:
|
||||||
|
# try to print everything on one line, but don't strip newlines
|
||||||
|
# if the command output happend to be too long
|
||||||
msg = "(stdout) %s" % stdout
|
msg = "(stdout) %s" % stdout
|
||||||
if stderr.rstrip() != '':
|
if stderr.rstrip() != '':
|
||||||
msg = "(stdout) %s (stderr) %s" % (stdout,stderr)
|
msg = "(stdout) %s (stderr) %s" % (stdout,stderr)
|
||||||
|
@ -108,6 +122,7 @@ def main(args):
|
||||||
hostname, rc, msg
|
hostname, rc, msg
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
# summarize response from command in multiple lines
|
||||||
buf = ''
|
buf = ''
|
||||||
buf += "%s | rc=%s >>\n" % (hostname, rc)
|
buf += "%s | rc=%s >>\n" % (hostname, rc)
|
||||||
buf += stdout
|
buf += stdout
|
||||||
|
@ -120,11 +135,13 @@ def main(args):
|
||||||
fd.write(buf)
|
fd.write(buf)
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
|
# print errors for hosts we could not reach
|
||||||
if results['dark']:
|
if results['dark']:
|
||||||
|
print ''
|
||||||
error_print('Hosts which could not be contacted or did not respond:')
|
error_print('Hosts which could not be contacted or did not respond:')
|
||||||
failed_hosts = results['dark'].keys()
|
failed_hosts = results['dark'].keys()
|
||||||
for hostname in keys:
|
for hostname in failed_hosts:
|
||||||
error_print("%s:%s" % (hostname, results['dark'][hostname]))
|
error_print("%s:\n%s\n" % (hostname, results['dark'][hostname]))
|
||||||
print ''
|
print ''
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue