Fix ansible & shell errors

Oh the joy of multi-step implement-run-debug situations like this...

Change-Id: Icf63ca02469f2ceb377b4ceb42bdc3004325dbd7
diff --git a/roles/download-artifacts-from-check/tasks/main.yaml b/roles/download-artifacts-from-check/tasks/main.yaml
new file mode 100644
index 0000000..75d490c
--- /dev/null
+++ b/roles/download-artifacts-from-check/tasks/main.yaml
@@ -0,0 +1,18 @@
+- fail:
+    msg: "This job only works in the 'promote' pipeline."
+  when: zuul.pipeline != 'promote'
+
+- name: Retrieve artifacts built within the check pipeline
+  shell: |
+    set -ex
+    curl "https://zuul.gerrit.cesnet.cz/api/tenant/{{ zuul.tenant }}/builds?pipeline=check&change={{ zuul.change }}&patchset={{ zuul.patchset }}" --output {{ zuul_output_dir }}/logs/check_jobs_for_change.json
+    ALL_JOBS=$(jq -r '.[].job_name' < {{ zuul_output_dir }}/logs/check_jobs_for_change.json | sort | uniq)
+    for JOB_NAME in ${ALL_JOBS}; do
+      ARTIFACTS_URL=$(jq -r ". | map(select(.job_name == \"${JOB_NAME}\"))[0].artifacts[].url" < {{ zuul_output_dir }}/logs/check_jobs_for_change.json)
+      mkdir {{ zuul_output_dir }}/artifacts/${JOB_NAME}
+      pushd {{ zuul_output_dir }}/artifacts/${JOB_NAME}
+      for ONE_URL in ${ARTIFACTS_URL}; do
+        curl -O "${ONE_URL}"
+      done
+      popd
+    done