Write contents to a file (locally)

    Arguments:
        dry_run: Whether to apply changes or not
        filename: file you want to write into
        content: content you want to write
        append: whether you want to replace the contents or append to it

    Returns:
        Result object with the following attributes set:
          * changed (``bool``):
          * diff (``str``): unified diff

def write_file(
    task: Task,
    filename: str,
    content: str,
    append: bool = False,
    dry_run: Optional[bool] = None,
) -> Result:
    """
    Write contents to a file (locally)

    Arguments:
        dry_run: Whether to apply changes or not
        filename: file you want to write into
        content: content you want to write
        append: whether you want to replace the contents or append to it

    Returns:
        Result object with the following attributes set:
          * changed (``bool``):
          * diff (``str``): unified diff
    """
    diff = _generate_diff(filename, content, append)

    if not task.is_dry_run(dry_run):
        mode = "a+" if append else "w+"
        with open(filename, mode=mode) as f:
            f.write(content)

    return Result(host=task.host, diff=diff, changed=bool(diff))