---
- name: Install build_vms_from_tag script
  template:
    src: ../templates/build_vms_from_tag.sh
    dest: /home/ansible/build_vms_from_tag.sh
    mode: 0744

- name: Run build_vms_from_tag scripts
  ## register + no_log + ignore_errors so ansible does NOT print the raw
  ## stdout. Build / git / apt progress output contains lone carriage
  ## returns, which PyYAML cannot place in a block scalar, so the default
  ## rendering collapses the whole log onto one escaped line. The next
  ## task prints a cleaned, multi-line tail instead - a single copy.
  shell: "/home/ansible/build_vms_from_tag.sh"
  register: build_result
  ignore_errors: true
  no_log: true

- name: Show build log tail on failure
  ## stdout_lines is already split on CR/LF; strip any residual CR and
  ## render the last lines as a YAML block scalar (readable, multi-line).
  ## The full log is in the uploaded build.log artifact.
  ansible.builtin.debug:
    msg: "{{ build_result.stdout_lines[-200:] | map('regex_replace', '\\r', '') | join('\n') }}"
  when: build_result.rc != 0

- name: Fail if the build failed
  ansible.builtin.fail:
    msg: "build_vms_from_tag.sh failed (rc={{ build_result.rc }})"
  when: build_result.rc != 0
