---
- name: installing demo
  debug:
    msg: "installing demo: {{item}}"

- name: block for job template loop
  block:
    - name: "add tower project job_template_loop.yml"
      tower_project:
        name: "{{ item.value.project.name }}"
        description: "{{ item.value.project.description }}"
        organization: "{{ item.value.project.organization }}"
        scm_type: "{{ item.value.project.scm_type }}"
        scm_url: "{{ item.value.project.scm_url }}"
        tower_username: "{{ my_tower_username }}"
        tower_password: "{{ my_tower_password }}"
        tower_host: "{{ my_tower_host }}"
        validate_certs: false
      when:
        - workshop_type in item.value.workshop_type

    - name: "add job template {{ item.value.name }} without survey job_template_loop.yml"
      tower_job_template:
        name: "{{ item.value.name }}"
        description: "{{ item.value.description }}"
        job_type: "{{ item.value.job_type }}"
        inventory: "{{ item.value.inventory }}"
        project: "{{ item.value.project.name }}"
        playbook: "{{ item.value.playbook }}"
        fact_caching_enabled: "{{ item.value.fact_caching_enabled | default('false') }}"
        credential: "{{ item.value.credential }}"
        survey_enabled: "{{ item.value.survey_enabled }}"
        tower_username: "{{ my_tower_username }}"
        tower_password: "{{ my_tower_password }}"
        tower_host: "{{ my_tower_host }}"
        validate_certs: false
      when:
        - workshop_type in item.value.workshop_type
        - not item.value.survey_enabled
      register: add_project
      until: add_project is not failed
      retries: 5

    - name: "add job template {{ item.value.name }} with survey"
      tower_job_template:
        name: "{{ item.value.name }}"
        description: "{{ item.value.description }}"
        job_type: "{{ item.value.job_type }}"
        inventory: "{{ item.value.inventory }}"
        project: "{{ item.value.project.name }}"
        playbook: "{{ item.value.playbook }}"
        fact_caching_enabled: "{{ item.value.fact_caching_enabled | default('false') }}"
        credential: "{{ item.value.credential }}"
        survey_enabled: "{{ item.value.survey_enabled }}"
        survey_spec: "{{ item.value.survey_spec }}"
        tower_username: "{{ my_tower_username }}"
        tower_password: "{{ my_tower_password }}"
        tower_host: "{{ my_tower_host }}"
        validate_certs: false
      when:
        - workshop_type in item.value.workshop_type
        - item.value.survey_enabled
      register: add_project
      until: add_project is not failed
      retries: 5
  when:
    - item.value.workflow is not defined or not item.value.workflow

- name: install workflow inside loop (workflow)
  include_tasks: workflow_loop.yml
  when:
    - item.value.workflow is defined
    - item.value.workflow
    - workshop_type in item.value.workshop_type
