ansible/bin/ansible

129 lines
4.3 KiB
Text
Raw Normal View History

2012-02-23 20:56:14 +01:00
#!/usr/bin/python
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
2012-02-24 01:42:05 +01:00
#
# This file is part of Ansible
2012-02-24 01:42:05 +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
#
# 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
try:
import json
except ImportError:
import simplejson as json
2012-02-23 20:56:14 +01:00
from optparse import OptionParser
import sys
2012-02-23 20:56:14 +01:00
import os
import getpass
import shlex
import ansible.runner
import ansible.playbook
import ansible.constants as C
from optparse import OptionParser
from ansible.utils import *
2012-02-23 20:56:14 +01:00
class Cli(object):
# ----------------------------------------------
2012-02-23 20:56:14 +01:00
def __init__(self):
pass
# ----------------------------------------------
def parse(self):
parser = OptionParser(usage = 'ansible <host-pattern> [options]')
parser.add_option("-a", "--args", dest="module_args",
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
parser.add_option('-f','--forks', dest='forks', default=C.DEFAULT_FORKS, type='int',
help='number of parallel processes to use')
parser.add_option("-i", "--inventory-file", dest="inventory",
help="inventory host file", default=C.DEFAULT_HOST_LIST)
parser.add_option("-k", "--ask-pass", default=False, action="store_true",
help="ask for SSH password")
parser.add_option("-M", "--module-path", dest="module_path",
help="path to module library", default=C.DEFAULT_MODULE_PATH)
parser.add_option("-m", "--module-name", dest="module_name",
help="module name to execute", default=C.DEFAULT_MODULE_NAME)
parser.add_option('-o', '--one-line', dest='one_line', action='store_true',
help="condense output")
2012-02-28 09:57:43 +01:00
parser.add_option('-t', '--tree', dest='tree', default=None,
help="log output to this directory")
parser.add_option('-T', '--timeout', default=C.DEFAULT_TIMEOUT, type='int',
dest='timeout', help="set the SSH timeout in seconds")
parser.add_option('-u', '--user', default=C.DEFAULT_REMOTE_USER,
dest='remote_user', help='connect as this user')
2012-02-23 20:56:14 +01:00
options, args = parser.parse_args()
if len(args) == 0 or len(args) > 1:
parser.print_help()
sys.exit(1)
return (options, args)
# ----------------------------------------------
def run(self, options, args):
pattern = args[0]
sshpass = None
if options.ask_pass:
sshpass = getpass.getpass(prompt="SSH password: ")
return ansible.runner.Runner(
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.inventory,
timeout=options.timeout,
forks=options.forks,
pattern=pattern,
verbose=True,
).run()
2012-02-23 20:56:14 +01:00
# ----------------------------------------------
def output(self, results, options, args):
if results is None:
exit("No hosts matched")
2012-02-28 09:57:43 +01:00
if options.tree:
prepare_writeable_dir(options.tree)
buf = ''
for hostname in contacted_hosts(results):
msg = host_report_msg(
hostname,
options.module_name,
contacted_host_result(results, hostname),
options.one_line
)
if options.tree:
write_tree_file(hostname, msg)
buf += msg
if has_dark_hosts(results):
buf += dark_hosts_msg(results)
print buf
if __name__ == '__main__':
cli = Cli()
(options, args) = cli.parse()
results = cli.run(options, args)
cli.output(results, options, args)