def METHOD_NAME(env):
    templates = {t["name"]: t for t in env["templates"]}
    for i, profile in enumerate(env["profiles"]):
        # Ensure that profile is bound once.
        name = profile.pop("template", None)
        if name is None:
            continue
        if name not in templates:
            raise ValueError(f"Unknown template: {name}")
        # Deep copy the template so profiles do not share anything.
        template = copy.deepcopy(templates[name])
        # Merge template and profile, overiding template keys.
        env["profiles"][i] = {**template, **profile}