# include environment template files from undercloud in overcloud_deploy script
- include_vars:
      file: "{{ templates_list_file}}"
      name: current_file

- name: look in undercloud's default templates
  stat:
      path: "{{ item }}"
  with_items: "{{ current_file.tripleo_heat_templates | default([]) }}"
  register: default_templates

- name: include templates from default location
  lineinfile:
      dest: "~/overcloud_deploy.sh"
      line: "--environment-file {{ item }} \\"
  with_items: "{{ default_templates.results|selectattr('stat.exists')|map(attribute='stat.path')|list }}"

- name: fail because file not found
  vars:
      missing_templates: "{{ default_templates.results|rejectattr('stat.exists')|map(attribute='item')|list }}"
  fail:
      msg:
          error: "Unable to locate templates from file '{{ templates_list_file }}'"
          source_file: "{{ templates_list_file }}"
          missing_templates: "{{ missing_templates }}"
  when: missing_templates | default([])

- name: create custom templates file
  copy:
      dest: "{{ ansible_user_dir }}/{{ install.deployment.files | basename }}/{{ raw_name }}.yaml"
      content: "{{ current_file.custom_templates|to_nice_yaml }}"
  when: current_file.custom_templates is defined
  vars:
      raw_name: "{{ templates_list_file | basename | splitext | first }}"

# include environment template files from undercloud in overcloud_deploy script
- include_vars:
      file: "{{ templates_list_file}}"
      name: current_file

- name: find the template file or corresponding j2 template
  find:
      path: "{{ item| dirname }}"
      pattern: "{{ item|basename|splitext|first }}\\.(?:yaml|j2\\.yaml)$"
      use_regex: yes
  with_items: "{{ current_file.tripleo_heat_templates | default([]) }}"
  register: default_templates

- name: include templates from default location
  lineinfile:
      dest: "~/overcloud_deploy.sh"
      line: "--environment-file {{ item.item }} \\"
  with_items: "{{ default_templates.results }}"
  when: item.matched > 0

- name: fail because file not found
  vars:
      missing_templates: "{{ default_templates.results|rejectattr('matched')|map(attribute='item')|list }}"
  fail:
      msg:
          error: "Unable to locate templates from file '{{ templates_list_file }}'"
          source_file: "{{ templates_list_file }}"
          missing_templates: "{{ missing_templates }}"
  when: missing_templates | default([])

- name: create custom templates file
  copy:
      dest: "{{ ansible_user_dir }}/{{ install.deployment.files | basename }}/{{ raw_name }}.yaml"
      content: "{{ current_file.custom_templates|to_nice_yaml }}"
  when: current_file.custom_templates is defined
  vars:
      raw_name: "{{ templates_list_file | basename | splitext | first }}"

Allow to include *.j2.yaml overcloud templates
Allow to include *.j2.yaml overcloud templates

In the tripleo-heat-templates we now have some of
*j2.yaml templates which are generated when running deploy
command.

Now when user specifies the --overcloud-templates flag we
will be searching additionally for the j2 template file
and include appropriate yaml file into the deploy script.

RHOSINFRA-1311

Change-Id: Idb9e2a7eda0a8504d94ce682dd5f988beecdb70a
