I have a folder containing a list of named tasks-files and want to include them based on a specific selector.
Eg.
inventory_dir
| stacks
| tasks
| app1.yml
| app2.yml
| app3.yml
The Selector can be a list, and can contain elements that doesn't have a taskfile.
I'm trying different approaches to reach my goal, but
- name: tentative 1
block:
- name: Tasks to execute always
include_tasks: ./generic-tasks.yml
- name: Include specific tasks for specific selector
include_tasks:
file: "{{ inventory_dir }}/stacks/tasks/{{ item }}.yml"
loop:
- '{{ SELECTOR }}'
Here I cannot use a stat task, because I don't know in advance what is the value of {{ item }} (and I cannot loop over a block). So this will fail in case the selector doesn't have a taskfile.
To avoid this I tried to do the contrary: looping over the folder content, and filtering by selector.
- name: Include specific tasks for specific selector
include_tasks:
file: "{{ item }}"
loop: "{{ lookup('fileglob', '{{ inventory_dir }}/stacks/tasks/*.yml',wantlist=True) }}"
when: "item|regex_replace("(?:\/[ \w]+){1,}\/([ \w]+)(?:\.yml)", "\1") in SELECTOR"
But it fail, probably because of the "in" check failing. (The regexp is working correctly anyway).
Do you have a better strategy?