You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/hosts.py

28 lines
766 B
Python

#!/usr/bin/env python3
import json
import sys
import yaml
def parse(path):
with open(path, 'r') as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as e:
return AnsibleError(e)
ret = { "all": { "hosts": list(), "vars": dict(), "children": list() } , "_meta": { "hostvars": {} } }
for host, groups in data.items():
ret["all"]["hosts"].append(host)
if groups is not None:
for group in groups:
if not group in ret:
ret[group] = dict()
ret[group]["hosts"] = list()
ret[group]["vars"] = dict()
ret[group]["children"] = list()
if not host in ret[group]["hosts"]:
ret[group]["hosts"].append(host)
return ret
print(json.dumps(parse("hosts.yml")))