1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/bin/ansible

60 lines
1.9 KiB
Text
Raw Normal View History

2012-02-23 20:56:14 +01:00
#!/usr/bin/python
from optparse import OptionParser
import json
import os
import ansible
DEFAULT_HOST_LIST = '~/.ansible_hosts'
DEFAULT_MODULE_PATH = '~/ansible'
DEFAULT_MODULE_NAME = 'ping'
DEFAULT_PATTERN = '*'
DEFAULT_FORKS = 3
DEFAULT_MODULE_ARGS = ''
class Cli(object):
def __init__(self):
pass
def runner(self):
parser = OptionParser()
parser.add_option("-H", "--host-list", dest="host_list",
help="path to hosts list", default=DEFAULT_HOST_LIST)
parser.add_option("-L", "--library", dest="module_path",
help="path to module library", default=DEFAULT_MODULE_PATH)
parser.add_option("-F", "--forks", dest="forks",
help="level of parallelism", default=DEFAULT_FORKS)
parser.add_option("-n", "--name", dest="module_name",
help="module name to execute", default=DEFAULT_MODULE_NAME)
parser.add_option("-a", "--args", dest="module_args",
help="module arguments", default=DEFAULT_MODULE_ARGS)
parser.add_option("-p", "--pattern", dest="pattern",
help="hostname pattern", default=DEFAULT_PATTERN)
options, args = parser.parse_args()
host_list = self._host_list(options.host_list)
# TODO: more shell like splitting on module_args would
# be a good idea
2012-02-23 20:56:14 +01:00
return ansible.Runner(
module_name=options.module_name,
module_path=options.module_path,
module_args=options.module_args.split(' '),
2012-02-23 20:56:14 +01:00
host_list=host_list,
forks=options.forks,
pattern=options.pattern,
)
def _host_list(self, host_list):
host_list = os.path.expanduser(host_list)
return file(host_list).read().split("\n")
if __name__ == '__main__':
result = Cli().runner().run()
print json.dumps(result, sort_keys=True, indent=4)