include_tasks and noop

This is potential feature idea.

I’m developing a small collection and as an extension points I expose various variable that refer to ‘task files’. For example, I have a role named prepare with a task file named _yum.yml and I have a variable repositories_tasks with a default value _yum.yml.

Users can override the variable repositories_tasks and provide their own tasks file. Or they could disable it entirely by providing an empty tasks file(maybe with a debug task).
repositories_tasks: noop.yml

Another case is for ‘hooks’ to run tasks at various phases in a role’s execution. For example a pre_upgrade_hook or a pre_restart hook and have them undefined or set to None or an empty string. Sample code:


- include_tasks: "{{ pre_kubelet_upgrade_hook }}"
when: pre_kubelet_upgrade_hook is defined and (not pre_kubelet_upgrade_hook is none) and (pre_kubelet_upgrade_hook | trim != '')

After seeing this pattern I thought that this could br a lot simpler(from a user perspective). Suppose we have a special value that when it appears as a value in include_tasks nothing happens. I will use ansible_noop as the special value. the following:

  • include_tasks: ansible_noop
    simply does nothing.

When I user wants to disable a set tasks exposed through a variable, they can assign it to ansible_noop, and as a default value for all hook variables you can assign them to ansible_noop.

Sample code for role author:
defaults/main.yml

pre_kubelet_upgrade_hook: ansible_noop

update_kube_tasks: _update_kube.yml
copy_config_tasks: _copy_config.yml

Sample code for role clients:
include_role:
name: upgrade
vars:
pre_kubelet_upgrade_hook: cri_upgrade.yml # File supplied by user
update_kube_tasks: update_kube.yml

copy_config_tasks: ansible_noop

What do you think?

I don’t see any benefit to the proposed feature. If you want to skip an include based on a flag value, you can do that without adding complexity to the engine.

  • include_tasks: “{{ task_file }}.yml”
    when: task_file | default(‘noop’) != ‘noop’