import re


def template_token_subst(buf, key, val):
    """
        Given a string (buf), a key (e.g. '@@MASTER_IP@@') and val, replace all
        occurrences of key in buf with val. Return the new string.
    """
    targetre = re.compile(re.escape(key))
    return re.sub(targetre, str(val), buf)