2012-02-23 20:56:14 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2012-02-29 01:08:09 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 01:42:05 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# This file is part of Ansible
|
2012-02-24 01:42:05 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +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
|
|
|
#
|
2012-02-29 01:08:09 +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-02-29 18:30:02 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2012-02-23 20:56:14 +01:00
|
|
|
from optparse import OptionParser
|
2012-02-28 10:00:31 +01:00
|
|
|
import sys
|
2012-02-23 20:56:14 +01:00
|
|
|
import os
|
2012-02-25 00:13:11 +01:00
|
|
|
import getpass
|
2012-02-25 05:35:09 +01:00
|
|
|
import shlex
|
2012-02-24 05:28:58 +01:00
|
|
|
import ansible.runner
|
2012-02-24 05:26:16 +01:00
|
|
|
import ansible.playbook
|
|
|
|
import ansible.constants as C
|
2012-02-28 10:20:25 +01:00
|
|
|
from optparse import OptionParser
|
2012-02-23 20:56:14 +01:00
|
|
|
|
|
|
|
class Cli(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def runner(self):
|
2012-02-28 10:20:25 +01:00
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-l", "--host-list", dest="host_list",
|
|
|
|
help="path to hosts list", default=C.DEFAULT_HOST_LIST)
|
|
|
|
parser.add_option("-m", "--module-path", dest="module_path",
|
|
|
|
help="path to module library", default=C.DEFAULT_MODULE_PATH)
|
|
|
|
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
|
|
|
|
dest='remote_user', help='set the default username')
|
|
|
|
parser.add_option("-p", "--pattern", dest="pattern",
|
|
|
|
help="hostname pattern", default=C.DEFAULT_PATTERN)
|
|
|
|
parser.add_option("-k", "--ask-pass", default=False, action="store_true",
|
|
|
|
help="ask the user to input the ssh password for connecting")
|
|
|
|
parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int',
|
|
|
|
help='set the number of forks to start up')
|
2012-02-23 20:56:14 +01:00
|
|
|
parser.add_option("-n", "--name", dest="module_name",
|
2012-02-28 10:00:31 +01:00
|
|
|
help="module name to execute", default=None)
|
2012-02-23 20:56:14 +01:00
|
|
|
parser.add_option("-a", "--args", dest="module_args",
|
2012-02-24 05:26:16 +01:00
|
|
|
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
|
2012-02-28 09:54:41 +01:00
|
|
|
parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
|
2012-02-28 09:57:43 +01:00
|
|
|
help="try to print output on one line")
|
|
|
|
parser.add_option('-t', '--tree', dest='tree', default=None,
|
2012-02-28 09:54:41 +01:00
|
|
|
help="if specified, a directory name to save output to, one file per host")
|
2012-03-02 01:54:17 +01:00
|
|
|
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
|
|
|
|
dest='timeout', help="set the timeout in seconds for ssh")
|
2012-02-28 09:54:41 +01:00
|
|
|
|
2012-02-23 20:56:14 +01:00
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
2012-02-28 10:00:31 +01:00
|
|
|
if options.module_name is None:
|
|
|
|
print >> sys.stderr, "-n is required"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-23 22:07:10 +01:00
|
|
|
# TODO: more shell like splitting on module_args would
|
|
|
|
# be a good idea
|
|
|
|
|
2012-02-25 00:13:11 +01:00
|
|
|
sshpass = None
|
2012-02-28 09:54:41 +01:00
|
|
|
if options.ask_pass:
|
2012-02-25 00:13:11 +01:00
|
|
|
sshpass = getpass.getpass(prompt="SSH password: ")
|
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
self.options = options
|
|
|
|
|
|
|
|
runner = ansible.runner.Runner(
|
2012-02-28 07:33:22 +01:00
|
|
|
module_name=options.module_name,
|
|
|
|
module_path=options.module_path,
|
|
|
|
module_args=shlex.split(options.module_args),
|
|
|
|
remote_user=options.remote_user,
|
|
|
|
remote_pass=sshpass,
|
|
|
|
host_list=options.host_list,
|
2012-03-02 01:54:17 +01:00
|
|
|
timeout=options.timeout,
|
2012-02-28 07:33:22 +01:00
|
|
|
forks=options.forks,
|
|
|
|
pattern=options.pattern,
|
|
|
|
verbose=True,
|
|
|
|
)
|
2012-02-28 09:54:41 +01:00
|
|
|
return runner
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
def output(self, results):
|
|
|
|
|
|
|
|
# if specifying output destination (aka tree output saves), create the
|
|
|
|
# directory to output to
|
|
|
|
|
|
|
|
options = self.options
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
# TODO: split into function
|
2012-02-28 09:57:43 +01:00
|
|
|
if options.tree:
|
|
|
|
if options.tree[0] != '/':
|
|
|
|
options.tree = os.path.realpath(os.path.expanduser(options.tree))
|
|
|
|
if not os.path.exists(options.tree):
|
2012-02-28 09:54:41 +01:00
|
|
|
try:
|
2012-02-28 09:57:43 +01:00
|
|
|
os.makedirs(options.tree)
|
2012-02-28 09:54:41 +01:00
|
|
|
except (IOError, OSError), e:
|
2012-02-28 09:57:43 +01:00
|
|
|
print >> sys.stderr, "Could not make dir %s: %s" % (options.tree, e)
|
2012-02-28 09:54:41 +01:00
|
|
|
sys.exit(1)
|
2012-02-28 09:57:43 +01:00
|
|
|
if not os.access(options.tree, os.W_OK):
|
|
|
|
print >> sys.stderr, "Cannot write to path %s" % options.tree
|
2012-02-28 09:54:41 +01:00
|
|
|
sys.exit(1)
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
# now walk results and print output
|
2012-02-23 20:56:14 +01:00
|
|
|
|
2012-02-28 09:54:41 +01:00
|
|
|
module_name = self.options.module_name
|
|
|
|
|
|
|
|
for hostname in sorted(results['contacted']):
|
|
|
|
result = results['contacted'][hostname]
|
|
|
|
rc = 0
|
|
|
|
failed = False
|
|
|
|
stdout = None
|
|
|
|
stderr = None
|
|
|
|
traceback = None
|
|
|
|
error = None
|
|
|
|
if type(result) == dict:
|
|
|
|
failed = result.get('failed', 0)
|
|
|
|
if module_name == 'command':
|
|
|
|
rc = result.get('rc',0)
|
|
|
|
stdout = result.get('stdout', '')
|
|
|
|
stderr = result.get('stderr', '')
|
|
|
|
traceback = result.get('traceback', '')
|
|
|
|
error = result.get('error', '')
|
|
|
|
|
|
|
|
# detect and show failures, if any
|
|
|
|
if rc != 0 or failed:
|
|
|
|
msg = "Error: %s: \n" % hostname
|
|
|
|
if stdout:
|
|
|
|
msg += stdout
|
|
|
|
if stderr:
|
|
|
|
msg += stderr
|
|
|
|
if traceback:
|
|
|
|
msg += traceback
|
|
|
|
if error:
|
|
|
|
msg += error
|
2012-02-28 10:20:25 +01:00
|
|
|
print >> sys.stderr, msg
|
2012-02-28 09:54:41 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
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
|
|
|
|
if module_name == 'command':
|
|
|
|
msg = "(stdout) %s" % stdout
|
|
|
|
if stderr.rstrip() != '':
|
|
|
|
msg = "(stdout) %s (stderr) %s" % (stdout,stderr)
|
|
|
|
print "%s | rc=%s | %s" % (
|
|
|
|
hostname, rc, msg
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
print "%s | %s" % (hostname, result)
|
|
|
|
else:
|
|
|
|
# summarize response from command in multiple lines
|
|
|
|
if module_name == 'command':
|
|
|
|
buf = ''
|
|
|
|
buf += "%s | rc=%s >>\n" % (hostname, rc)
|
|
|
|
buf += stdout
|
|
|
|
if stderr:
|
|
|
|
buf += stderr
|
|
|
|
print buf
|
2012-02-28 09:57:43 +01:00
|
|
|
if options.tree:
|
|
|
|
path = os.path.join(options.tree, hostname)
|
2012-02-28 09:54:41 +01:00
|
|
|
fd = open(path, "w+")
|
|
|
|
fd.write(buf)
|
|
|
|
fd.close()
|
|
|
|
else:
|
|
|
|
print "%s >>" % hostname
|
|
|
|
print json.dumps(result, indent=4, sort_keys=True)
|
|
|
|
|
|
|
|
if len(results['dark'].keys()) > 0:
|
2012-02-28 10:20:25 +01:00
|
|
|
print >> sys.stderr, "*** Hosts which could not be contacted or did not respond: ***"
|
2012-02-28 09:54:41 +01:00
|
|
|
failed_hosts = results['dark'].keys()
|
|
|
|
for hostname in failed_hosts:
|
2012-02-28 10:20:25 +01:00
|
|
|
print >> sys.stderr, "%s:\n%s\n" % (hostname, results['dark'][hostname])
|
|
|
|
print ""
|
2012-02-28 09:54:41 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cli = Cli()
|
|
|
|
cli.output(cli.runner().run())
|
|
|
|
|