---
# Sorting is not enforeced
- name: "Find KaaS templates"
  command: "echo {{ item | quote }}"
  register: results
  changed_when: false
  when: (kaas_project_pods | length > 0) or not (item | regex_search('kaas-pods')) 
  with_fileglob:
    - "{{ role_path }}/templates/{{ kaas_template_glob | default('*') }}.j2"
    - "{{ kaas_project_path }}/templates/{{ kaas_template_glob | default('*') }}.j2"


#- debug: msg="{{ results }}"

- name: "Sort and execute KaaS templates"
  include_tasks: "template.yml"
#  delegate_to: "{{ groups.masters[0] }}"
  run_once: true
  with_items: "{{ sorted_tmpl }}"
  vars:
    sorted_tmpl: "{{ (results.results[0] is defined) | ternary (results | json_query('results[*].stdout_lines') | sum(start=[]) | map('basename') | sort | unique, []) }}"
    instantiate: true
    load: false
    pods: "{{ kaas_project_pods }}"
  loop_control:
    loop_var: tmpl_name