def add_group(inventory, groupname, hostname=None):
    """Add a group to inventory, optionally add it with hostname."""
    all_group = inventory["all"]
    group = {}
    if hostname:
        group["hosts"] = {hostname: {}}
    all_group["children"][groupname] = group
    return group