#!/usr/bin/python

# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# 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.
#
# 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/>.

from optparse import OptionParser
import json
import os
import getpass
import shlex
import ansible.runner
import ansible.playbook
import ansible.constants as C
from ansible.scripts import base_ans_parser

class Cli(object):

    def __init__(self):
        pass

    def runner(self):
        parser = base_ans_parser()
        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)
        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: ")

        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.host_list,
            forks=options.forks,
            pattern=options.pattern,
            verbose=True,
        )

if __name__ == '__main__':

    result = Cli().runner().run()
    print json.dumps(result, sort_keys=True, indent=4)


