'Creates a new block, with task lists filtered based on the tags contained
within the play_context object.'

def filter_tagged_tasks(self, play_context, all_vars):

    def evaluate_and_append_task(target):
        tmp_list = []
        for task in target:
            if isinstance(task, Block):
                tmp_list.append(evaluate_block(task))
            elif ((task.action == 'meta') or ((task.action == 'include') and task.evaluate_tags([], play_context.skip_tags, all_vars=all_vars)) or task.evaluate_tags(play_context.only_tags, play_context.skip_tags, all_vars=all_vars)):
                tmp_list.append(task)
        return tmp_list
    def evaluate_block(block):
        new_block = self.copy(exclude_tasks=True)
        new_block.block = evaluate_and_append_task(block.block)
        new_block.rescue = evaluate_and_append_task(block.rescue)
        new_block.always = evaluate_and_append_task(block.always)
        return new_block
    return evaluate_block(self)
