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 getpass
|
||||||
import ansible.runner
|
import ansible.runner
|
||||||
import shlex
|
import shlex
|
||||||
from ansible.scripts import base_ans_parser, errorprint
|
from ansible.scripts import base_ans_parser, error_print
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
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.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")
|
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 \
|
help="output all things as one line - to make grepping easier, will \
|
||||||
not remove \\n's from output of commands, though")
|
not remove \\n's from output of commands, though")
|
||||||
parser.add_option('-o', '--output-to-dir', dest='output_dest', default=None,
|
parser.add_option('-o', '--output-dir', dest='output_dest', default=None,
|
||||||
help="output each hosts results to a file in a dir named for the host")
|
help="output each host's results to a file in a dir named for the host")
|
||||||
options, args = parser.parse_args(args)
|
options, args = parser.parse_args(args)
|
||||||
|
|
||||||
sshpass = None
|
sshpass = None
|
||||||
|
@ -61,7 +61,14 @@ def main(args):
|
||||||
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 len(args) == 0:
|
||||||
|
print >> sys.stderr, "Missing argument. What should be executed?"
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
mycmd = ' '.join(args)
|
mycmd = ' '.join(args)
|
||||||
|
|
||||||
|
print "FORKS=%s" % options.forks
|
||||||
|
|
||||||
runner = ansible.runner.Runner(
|
runner = ansible.runner.Runner(
|
||||||
module_name='command',
|
module_name='command',
|
||||||
module_path=options.module_path,
|
module_path=options.module_path,
|
||||||
|
@ -86,36 +93,36 @@ def main(args):
|
||||||
msg += d.get('stderr', '')
|
msg += d.get('stderr', '')
|
||||||
msg += d.get('traceback', '')
|
msg += d.get('traceback', '')
|
||||||
msg += d.get('error', '')
|
msg += d.get('error', '')
|
||||||
errorprint(msg)
|
error_print(msg)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if options.output_dest:
|
if options.output_dest:
|
||||||
fo = open(options.output_dest + '/' + hn +'.output', 'w')
|
fo = open(options.output_dest + '/' + hn +'.output', 'w')
|
||||||
fo.write(mycmd + '\n')
|
fo.write(mycmd + '\n')
|
||||||
fo.write('%s:\n' % hn)
|
fo.write('%s:\n' % hn)
|
||||||
if options.returncodes:
|
if options.return_codes:
|
||||||
fo.write('return code: %s\n' % d['rc'])
|
fo.write('return code: %s\n' % d['rc'])
|
||||||
fo.write('%s\nErrors:\n%s\n' % (d['stdout'], d['stderr']))
|
fo.write('%s\nErrors:\n%s\n' % (d['stdout'], d['stderr']))
|
||||||
fo.close()
|
fo.close()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if options.oneline:
|
if options.one_line:
|
||||||
if options.returncodes:
|
if options.return_codes:
|
||||||
print '%s:%s:%s:%s' % (hn, d['rc'], d['stdout'], d['stderr'])
|
print '%s:%s:%s:%s' % (hn, d['rc'], d['stdout'], d['stderr'])
|
||||||
else:
|
else:
|
||||||
print '%s:%s:%s' % (hn, d['stdout'], d['stderr'])
|
print '%s:%s:%s' % (hn, d['stdout'], d['stderr'])
|
||||||
else:
|
else:
|
||||||
print '%s:' % hn
|
print '%s:' % hn
|
||||||
if options.returncodes:
|
if options.return_codes:
|
||||||
print 'return code:%s\n' % d['rc']
|
print 'return code:%s\n' % d['rc']
|
||||||
print '%s' % d['stdout']
|
print '%s' % d['stdout']
|
||||||
if d.get('stderr', None):
|
if d.get('stderr', None):
|
||||||
print '%s' % d['stderr']
|
print '%s' % d['stderr']
|
||||||
|
|
||||||
if results['dark']:
|
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']):
|
for hn in sorted(results['dark']):
|
||||||
errorprint(hn)
|
error_print(hn)
|
||||||
print ''
|
print ''
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ from optparse import OptionParser
|
||||||
import sys
|
import sys
|
||||||
import constants as C
|
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()
|
parser = OptionParser()
|
||||||
if opthosts:
|
if opthosts:
|
||||||
parser.add_option('--host', default=[], action='append',
|
parser.add_option('--host', default=[], action='append',
|
||||||
|
@ -30,11 +30,11 @@ def base_ans_parser(opthosts=True, outputpath=True, forkdef=C.DEFAULT_FORKS):
|
||||||
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
|
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
|
||||||
dest='remote_user', help='set the default username')
|
dest='remote_user', help='set the default username')
|
||||||
parser.add_option("-P", "--askpass", default=False, action="store_true",
|
parser.add_option("-P", "--askpass", default=False, action="store_true",
|
||||||
help="ask the user to input the ssh password for connecting")
|
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')
|
help='set the number of forks to start up')
|
||||||
if outputpath:
|
if output_path:
|
||||||
parser.add_option('--outputpath', default='/tmp/ansible', dest="outputpath",
|
parser.add_option('--output-path', default='/tmp/ansible', dest="output_path",
|
||||||
help="basepath to store results/errors output.")
|
help="basepath to store results/errors output.")
|
||||||
return parser
|
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
|
# 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
|
print >> sys.stderr, msg
|
||||||
|
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -30,5 +30,6 @@ setup(name='ansible',
|
||||||
],
|
],
|
||||||
scripts=[
|
scripts=[
|
||||||
'bin/ansible',
|
'bin/ansible',
|
||||||
|
'bin/ans-command',
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue