From 179d1a25160c44d3d001f3ed7b8173aa4be1f5c2 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Mon, 30 Mar 2020 13:55:16 -0500
Subject: [PATCH] Collect production playbook output

We need to log to a file and then collect it to the log output on
zuul. Default to true to that steady-state reads nicely. When we
add new jobs we should make sure to set to false first so that we
can vett the output before publishing it.

Change-Id: Ia4f759b82a5fff6e36e4284c11281254c0d5627d
---
 .zuul.yaml                                  |  1 +
 playbooks/zuul/run-production-playbook.yaml | 29 +++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/.zuul.yaml b/.zuul.yaml
index 9a7b55c658..bb58ba1e13 100644
--- a/.zuul.yaml
+++ b/.zuul.yaml
@@ -1408,6 +1408,7 @@
     run: playbooks/zuul/run-production-playbook.yaml
     vars:
       ansible_forks: 5
+      infra_prod_playbook_collect_log: true
     nodeset:
       nodes: []
 
diff --git a/playbooks/zuul/run-production-playbook.yaml b/playbooks/zuul/run-production-playbook.yaml
index f21dc215e1..d91d131a07 100644
--- a/playbooks/zuul/run-production-playbook.yaml
+++ b/playbooks/zuul/run-production-playbook.yaml
@@ -15,6 +15,31 @@
 
 - hosts: bridge.openstack.org
   tasks:
-    - name: Run specified playbook on bridge.o.o
+    - name: Log a playbook start header
       become: yes
-      command: ansible-playbook -f {{ ansible_forks }} /opt/system-config/playbooks/{{ playbook_name }}
+      shell: 'echo "Running {{ ansible_date_time.iso8601 }}: ansible-playbook -v -f {{ ansible_forks }} /opt/system-config/playbooks/{{ playbook_name }}" > /var/log/ansible/{{ playbook_name }}.log'
+
+    - name: Run specified playbook on bridge.o.o and redirect output
+      become: yes
+      shell: 'ansible-playbook -v -f {{ ansible_forks }} /opt/system-config/playbooks/{{ playbook_name }} >> /var/log/ansible/{{ playbook_name }}.log'
+
+    # Not using normal zuul job roles as bridge.openstack.org is not a
+    # test node with all the normal bits in place.
+    - name: Collect log output
+      synchronize:
+        dest: "{{ zuul.executor.log_root }}/{{ playbook_name }}.log"
+        mode: pull
+        src: "/var/log/ansible/{{ playbook_name }}.log"
+        verify_host: true
+      when: infra_prod_playbook_collect_log
+
+    - name: Return playbook log artifact to Zuul
+      when: infra_prod_playbook_collect_log
+      zuul_return:
+        data:
+          zuul:
+            artifacts:
+              - name: "Playbook Log"
+                url: "{{ playbook_name }}.log"
+                metadata:
+                  type: text