Minor error handling tweaks (yell if no args) for ansible-command and some minor
style bits (underscores between compound words, use dest always in optparse)
This commit is contained in:
parent
a3a426b4c5
commit
0321afb1e3
3 changed files with 28 additions and 20 deletions
|
@ -25,19 +25,19 @@ import os
|
|||
import getpass
|
||||
import ansible.runner
|
||||
import shlex
|
||||
from ansible.scripts import base_ans_parser, errorprint
|
||||
from ansible.scripts import base_ans_parser, error_print
|
||||
|
||||
|
||||
def main(args):
|
||||
parser = base_ans_parser(outputpath=False)
|
||||
parser = base_ans_parser(output_path=False)
|
||||
parser.usage = "ans-command [options] command-to-run"
|
||||
parser.add_option('--returncodes', action='store_true',
|
||||
parser.add_option('-c', '--return-codes', dest='return_codes', action='store_true',
|
||||
help="prefix each line with the commands returncode")
|
||||
parser.add_option('-1', '--oneline', action='store_true',
|
||||
parser.add_option('-1', '--one-line', dest='one_line', action='store_true',
|
||||
help="output all things as one line - to make grepping easier, will \
|
||||
not remove \\n's from output of commands, though")
|
||||
parser.add_option('-o', '--output-to-dir', dest='output_dest', default=None,
|
||||
help="output each hosts results to a file in a dir named for the host")
|
||||
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")
|
||||
options, args = parser.parse_args(args)
|
||||
|
||||
sshpass = None
|
||||
|
@ -61,7 +61,14 @@ def main(args):
|
|||
print >> sys.stderr, "Cannot write to path %s" % options.output_dest
|
||||
sys.exit(1)
|
||||
|
||||
if len(args) == 0:
|
||||
print >> sys.stderr, "Missing argument. What should be executed?"
|
||||
sys.exit(1)
|
||||
|
||||
mycmd = ' '.join(args)
|
||||
|
||||
print "FORKS=%s" % options.forks
|
||||
|
||||
runner = ansible.runner.Runner(
|
||||
module_name='command',
|
||||
module_path=options.module_path,
|
||||
|
@ -86,36 +93,36 @@ def main(args):
|
|||
msg += d.get('stderr', '')
|
||||
msg += d.get('traceback', '')
|
||||
msg += d.get('error', '')
|
||||
errorprint(msg)
|
||||
error_print(msg)
|
||||
continue
|
||||
|
||||
if options.output_dest:
|
||||
fo = open(options.output_dest + '/' + hn +'.output', 'w')
|
||||
fo.write(mycmd + '\n')
|
||||
fo.write('%s:\n' % hn)
|
||||
if options.returncodes:
|
||||
if options.return_codes:
|
||||
fo.write('return code: %s\n' % d['rc'])
|
||||
fo.write('%s\nErrors:\n%s\n' % (d['stdout'], d['stderr']))
|
||||
fo.close()
|
||||
continue
|
||||
|
||||
if options.oneline:
|
||||
if options.returncodes:
|
||||
if options.one_line:
|
||||
if options.return_codes:
|
||||
print '%s:%s:%s:%s' % (hn, d['rc'], d['stdout'], d['stderr'])
|
||||
else:
|
||||
print '%s:%s:%s' % (hn, d['stdout'], d['stderr'])
|
||||
else:
|
||||
print '%s:' % hn
|
||||
if options.returncodes:
|
||||
if options.return_codes:
|
||||
print 'return code:%s\n' % d['rc']
|
||||
print '%s' % d['stdout']
|
||||
if d.get('stderr', None):
|
||||
print '%s' % d['stderr']
|
||||
|
||||
if results['dark']:
|
||||
errorprint('Hosts which could not be contacted or did not respond:')
|
||||
error_print('Hosts which could not be contacted or did not respond:')
|
||||
for hn in sorted(results['dark']):
|
||||
errorprint(hn)
|
||||
error_print(hn)
|
||||
print ''
|
||||
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ from optparse import OptionParser
|
|||
import sys
|
||||
import constants as C
|
||||
|
||||
def base_ans_parser(opthosts=True, outputpath=True, forkdef=C.DEFAULT_FORKS):
|
||||
def base_ans_parser(opthosts=True, output_path=True, forkdef=C.DEFAULT_FORKS):
|
||||
parser = OptionParser()
|
||||
if opthosts:
|
||||
parser.add_option('--host', default=[], action='append',
|
||||
|
@ -31,10 +31,10 @@ def base_ans_parser(opthosts=True, outputpath=True, forkdef=C.DEFAULT_FORKS):
|
|||
dest='remote_user', help='set the default username')
|
||||
parser.add_option("-P", "--askpass", default=False, action="store_true",
|
||||
help="ask the user to input the ssh password for connecting")
|
||||
parser.add_option('-f','--forks', default=forkdef, type='int',
|
||||
parser.add_option('-f','--forks', dest='forks', default=forkdef, type='int',
|
||||
help='set the number of forks to start up')
|
||||
if outputpath:
|
||||
parser.add_option('--outputpath', default='/tmp/ansible', dest="outputpath",
|
||||
if output_path:
|
||||
parser.add_option('--output-path', default='/tmp/ansible', dest="output_path",
|
||||
help="basepath to store results/errors output.")
|
||||
return parser
|
||||
|
||||
|
@ -42,6 +42,6 @@ def base_ans_parser(opthosts=True, outputpath=True, forkdef=C.DEFAULT_FORKS):
|
|||
# to things people might be more inclined to deal with at a bash prompt
|
||||
|
||||
|
||||
def errorprint(msg):
|
||||
def error_print(msg):
|
||||
print >> sys.stderr, msg
|
||||
|
||||
|
|
1
setup.py
1
setup.py
|
@ -30,5 +30,6 @@ setup(name='ansible',
|
|||
],
|
||||
scripts=[
|
||||
'bin/ansible',
|
||||
'bin/ans-command',
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue