def generate_inventory(inventory, to_stdout=False, tmp_dir=None):
    """
    Generates a host file to use with an ansible-playbook call.

    The first argument is a dictionary mapping that contains the group name as
    the key and a list of hostnames as values

    For example:

        {'mons': ['mon.host'], 'osds': ['osd1.host', 'osd1.host']}
    """
    result = []
    for section, hosts in inventory.items():
        group_name = section
        result.append('[{0}]'.format(group_name))
        if not isinstance(hosts, list):
            hosts = [hosts]
        result.extend(hosts)
    result_str = '\n'.join(result) + '\n'
    if to_stdout:
        print(result_str)
        return
    with open('hosts_file', 'w') as hosts_file:
        hosts_file.write(result_str)
