def read_inventory_file_to_dict(inventory_filename):
    """ Reads inventory file in ini format (for example in Ansible format),
        returns a dictionary, where keys correspond to lower case sections,
        and resolved first level-inclusions
    """
    config = configparser.ConfigParser(allow_no_value=True)
    config.read(inventory_filename)
    # extract the IPs into lower-case groups per section
    _groups = {
        key.lower(): [ ip.lower().split(" ")[0] for (ip, group) in config.items(key)]
        for key in config.sections()
    }
    # resolve one level groups hierarchy
    groups = {}
    for group, ips in _groups.items():
        ips_set = set()
        for ip in ips:
            subgroup = _groups.get(ip)
            if subgroup is not None:
                for sub in subgroup:
                    ips_set.add(sub)
            else:
                ips_set.add(ip)
        groups[group] = list(ips_set)
    return groups
 Reads inventory file in ini format (for example in Ansible format),
        returns a dictionary, where keys correspond to lower case sections,
        and resolved first level-inclusions
    
powerfulseal/node/inventory.py