ansible/bin/ansible

94 lines
3.7 KiB
Python
Executable file

#!/usr/bin/python
# Copyright (c) 2012 Michael DeHaan <michael.dehaan@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from optparse import OptionParser
import json
import os
import getpass
import ansible.runner
import ansible.playbook
import ansible.constants as C
class Cli(object):
def __init__(self):
pass
def runner(self):
parser = OptionParser()
parser.add_option("-P", "--askpass", default=False, action="store_true",
help="ask the user to input the ssh password for connecting")
parser.add_option("-H", "--host-list", dest="host_list",
help="path to hosts list", default=C.DEFAULT_HOST_LIST)
parser.add_option("-L", "--library", dest="module_path",
help="path to module library", default=C.DEFAULT_MODULE_PATH)
parser.add_option("-f", "--forks", dest="forks", type="int",
help="level of parallelism", default=C.DEFAULT_FORKS)
parser.add_option("-n", "--name", dest="module_name",
help="module name to execute", default=C.DEFAULT_MODULE_NAME)
parser.add_option("-a", "--args", dest="module_args",
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
parser.add_option("-p", "--pattern", dest="pattern",
help="hostname pattern", default=C.DEFAULT_PATTERN)
parser.add_option("-u", "--remote-user", dest="remote_user",
help="remote username", default=C.DEFAULT_REMOTE_USER)
parser.add_option("-r", "--run-playbook", dest="playbook",
help="playbook file, instead of -n and -a", default=None)
options, args = parser.parse_args()
# TODO: more shell like splitting on module_args would
# be a good idea
sshpass = None
if options.askpass:
sshpass = getpass.getpass(prompt="SSH password: ")
if options.playbook is None:
return ansible.runner.Runner(
module_name=options.module_name,
module_path=options.module_path,
module_args=options.module_args.split(' '),
remote_user=options.remote_user,
remote_pass=sshpass,
host_list=options.host_list,
forks=options.forks,
pattern=options.pattern,
verbose=False,
)
else:
return ansible.playbook.PlayBook(
module_path=options.module_path,
remote_user=options.remote_user,
remote_pass=sshpass,
host_list=options.host_list,
forks=options.forks,
verbose=False,
)
if __name__ == '__main__':
result = Cli().runner().run()
print json.dumps(result, sort_keys=True, indent=4)