Fix ansible & shell errors

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

Change-Id: Icf63ca02469f2ceb377b4ceb42bdc3004325dbd7
diff --git a/playbooks/download-artifacts-from-check/run.yaml b/playbooks/download-artifacts-from-check/run.yaml
new file mode 100644
index 0000000..920dce2
--- /dev/null
+++ b/playbooks/download-artifacts-from-check/run.yaml
@@ -0,0 +1,3 @@
+- hosts: all
+  roles:
+    - download-artifacts-from-check
diff --git a/playbooks/retrieve-artifacts-from-check/run.yaml b/playbooks/retrieve-artifacts-from-check/run.yaml
deleted file mode 100644
index 0e44b5b..0000000
--- a/playbooks/retrieve-artifacts-from-check/run.yaml
+++ /dev/null
@@ -1,3 +0,0 @@
-- hosts: all
-  roles:
-    - retrieve-artifacts-from-check
diff --git a/roles/download-artifacts-from-check/defaults/main.yaml b/roles/download-artifacts-from-check/defaults/main.yaml
new file mode 100644
index 0000000..3e6da11
--- /dev/null
+++ b/roles/download-artifacts-from-check/defaults/main.yaml
@@ -0,0 +1 @@
+zuul_output_dir: "{{ ansible_user_dir }}/zuul-output"
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
diff --git a/roles/fetch-artifacts-for-publish/defaults/main.yaml b/roles/fetch-artifacts-for-publish/defaults/main.yaml
new file mode 100644
index 0000000..3e6da11
--- /dev/null
+++ b/roles/fetch-artifacts-for-publish/defaults/main.yaml
@@ -0,0 +1 @@
+zuul_output_dir: "{{ ansible_user_dir }}/zuul-output"
diff --git a/roles/fetch-artifacts-for-publish/tasks/main.yaml b/roles/fetch-artifacts-for-publish/tasks/main.yaml
new file mode 100644
index 0000000..5283d65
--- /dev/null
+++ b/roles/fetch-artifacts-for-publish/tasks/main.yaml
@@ -0,0 +1,26 @@
+- fail:
+    msg: "This role only works in the 'promote' pipeline."
+  when: zuul.pipeline != 'promote'
+
+- name: Ensure artifacts directory exists
+  delegate_to: localhost
+  file:
+    path: "{{ zuul.executor.work_root }}/artifacts"
+    state: directory
+
+- name: Collect artifacts
+  synchronize:
+    dest: "{{ zuul.executor.work_root }}/artifacts/"
+    mode: pull
+    src: "{{ zuul_output_dir }}/artifacts/"
+    verify_host: true
+
+- name: "Purge artifacts on the build node so that they are not uploaded the promote job's log directory"
+  file:
+    path: "{{ zuul_output_dir }}/artifacts/"
+    state: absent
+
+- name: "Re-create an empty artifacts directory on the build node so that `fetch-output` doesn't complain"
+  file:
+    path: "{{ zuul_output_dir }}/artifacts/"
+    state: directory
diff --git a/roles/remove-artifacts-from-executor/tasks/main.yaml b/roles/remove-artifacts-from-executor/tasks/main.yaml
new file mode 100644
index 0000000..5d1dad8
--- /dev/null
+++ b/roles/remove-artifacts-from-executor/tasks/main.yaml
@@ -0,0 +1,9 @@
+- fail:
+    msg: "This role only works in the 'promote' pipeline."
+  when: zuul.pipeline != 'promote'
+
+- name: Nuke artifacts directory on executor as it is not needed anymore
+  delegate_to: localhost
+  file:
+    path: "{{ zuul.executor.work_root }}/artifacts"
+    state: absent
diff --git a/roles/retrieve-artifacts-from-check/tasks/main.yaml b/roles/retrieve-artifacts-from-check/tasks/main.yaml
deleted file mode 100644
index ce10454..0000000
--- a/roles/retrieve-artifacts-from-check/tasks/main.yaml
+++ /dev/null
@@ -1,19 +0,0 @@
-- fail: "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/logs/check_jobs_for_change.json
-    ALL_JOBS=$(jq -r '.[].job_name' < ~/zuul-output/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' < ~/check_jobs_for_change_ps)
-      mkdir ~/zuul-output/artifacts/${JOB_NAME}
-      pushd ~/zuul-output/artifacts/${JOB_NAME}
-      for ONE_URL in ${ARTIFACTS_URL}; do
-        echo "*** Found ${JOB_NAME}: ${ONE_URL}"
-        curl -O "${ONE_URL}"
-      done
-      popd
-    done
-
diff --git a/roles/return-built-artifact/defaults/main.yaml b/roles/return-built-artifact/defaults/main.yaml
index f822316..d419ead 100644
--- a/roles/return-built-artifact/defaults/main.yaml
+++ b/roles/return-built-artifact/defaults/main.yaml
@@ -1 +1,2 @@
 artifacts: []
+zuul_output_dir: "{{ ansible_user_dir }}/zuul-output"
diff --git a/roles/return-built-artifact/tasks/main.yaml b/roles/return-built-artifact/tasks/main.yaml
index 0a1e4d1..f7b5945 100644
--- a/roles/return-built-artifact/tasks/main.yaml
+++ b/roles/return-built-artifact/tasks/main.yaml
@@ -1,6 +1,6 @@
 - name: find artifact name
   find:
-    paths: '{{ ansible_user_dir }}/zuul-output/artifacts'
+    paths: '{{ zuul_output_dir }}/artifacts'
     patterns: '{{ artifacts.glob }}'
   register: found_artifacts
 
diff --git a/zuul.yaml b/zuul.yaml
index 674d501..61e080f 100644
--- a/zuul.yaml
+++ b/zuul.yaml
@@ -13,4 +13,4 @@
       Retrieve all artifacts built by the last matching run in the check
       pipeline, and promote them into a permanent location in the log server.
       This is intended to be run within a promote pipeline.
-    run: playbooks/retrieve-artifacts-from-check/run.yaml
+    run: playbooks/download-artifacts-from-check/run.yaml