---
- name: set facts from role vars
  set_fact:
    demo_list: "{{ (dict(vars|dictsort|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.workshop_type', 'defined'))|dict2items)|map(attribute='key')|list }}"
    full_demo_info: "{{ (dict(vars|dictsort|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.workshop_type', 'defined'))|dict2items) }}"

- name: provide info to terminal window
  debug:
    msg:
      - "install {{ vars.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:
      - vars.demo is defined
      - vars.demo in demo_list or vars.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 Azure cred 
  include_tasks: azure_cred.yml
  when:
    - azure_demo == True

- 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"
    - vars[demo].workflow is not defined or not vars[demo].workflow

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