---
- name: explicitly load vars under a subkey for reliable access
  include_tasks: load_vars.yml

- name: set facts from role vars
  set_fact:
    demo_list: "{{ job_template_definitions|dict2items|map(attribute='key')|list }}"
    full_demo_info: "{{ job_template_definitions|dict2items }}"

- name: provide info to terminal window
  debug:
    msg:
      - "install {{ demo }} on {{ my_tower_host }}"
      - "total demos: {{ demo_list | length }}"
      - "available demos are: {{ demo_list }}"

- name: make sure demo is a valid demo
  assert:
    that:
      - demo is defined
      - demo in demo_list or demo == "all"
    msg:
      - "demo must be defined and be one of: {{ demo_list }}"
      - "full list can be found on https://github.com/ansible/product-demos"

- name: install all job templates
  include_tasks: job_template_loop.yml
  loop: "{{ full_demo_info }}"
  when: demo == "all"

- name: install single job template
  include_tasks: job_template.yml
  when:
    - demo != "all"
    - job_template_definitions[demo].workflow is not defined or not job_template_definitions[demo].workflow

- name: install single workflow
  include_tasks: workflow.yml
  when:
    - demo != "all"
    - job_template_definitions[demo].workflow is defined
    - job_template_definitions[demo].workflow
