#!/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(parse("hosts.yml"))