34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
import argparse
|
|
import dynamic_inventory
|
|
|
|
|
|
class Script(object):
|
|
def __init__(self, environment_path):
|
|
config = dynamic_inventory.Config(environment_path)
|
|
self.inventory = dynamic_inventory.Inventory(config)
|
|
|
|
def execute(self):
|
|
args = self._parse_args()
|
|
if args.host is None:
|
|
self._do_list()
|
|
else:
|
|
self._do_host(args.host)
|
|
|
|
def _parse_args(self):
|
|
p = argparse.ArgumentParser(description='Produce Ansible inventory')
|
|
p.add_argument(
|
|
'--list', action='store_true', default=True,
|
|
help='List all hosts')
|
|
p.add_argument(
|
|
'--host', action='store',
|
|
help='Show variable for a single host')
|
|
return p.parse_args()
|
|
|
|
def _do_list(self):
|
|
data = self.inventory.groups.copy()
|
|
data["_meta"] = {"hostvars": self.inventory.nodes}
|
|
print(dynamic_inventory.convert_to_json(data))
|
|
|
|
def _do_host(self, name):
|
|
print(dynamic_inventory.convert_to_json(self.inventory.nodes[name]))
|