From daf758775c3dc16e61c895702b517e9464beead3 Mon Sep 17 00:00:00 2001
From: Jesse Pretorius <jesse.pretorius@rackspace.co.uk>
Date: Wed, 28 Nov 2018 13:34:35 +0000
Subject: [PATCH] MNAIO: Fix warning for guestfish workaround task

This patch resolves the warning about using jinja braces
the conditional for the guestfish workaround task. While
doing so, the task is made idempotent and we also ensure
that the task that follows exits immediately on any errors.

Change-Id: I7ec254a96312e07229b0a5c799d4a98b5d9c8957
---
 multi-node-aio/playbooks/setup-host.yml | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/multi-node-aio/playbooks/setup-host.yml b/multi-node-aio/playbooks/setup-host.yml
index c47ef79c..c96cd718 100644
--- a/multi-node-aio/playbooks/setup-host.yml
+++ b/multi-node-aio/playbooks/setup-host.yml
@@ -100,14 +100,28 @@
     # See:
     # https://bugzilla.redhat.com/show_bug.cgi?id=1591617
     # https://bugs.launchpad.net/ubuntu/+source/libguestfs/+bug/1615337
+    # Exit codes:
+    # 0 - Fix already enabled
+    # 1 - Error
+    # 2 - Fix enabled by task (should show task as changed)
     - name: Apply workaround for older versions to make guestfish work
       shell: |
-        echo dash > /usr/lib/x86_64-linux-gnu/guestfs/supermin.d/zz-dash-packages
-        rm -rf /var/tmp/.guestfs*
+        set -e
+        TEST_FILE_PATH='/usr/lib/x86_64-linux-gnu/guestfs/supermin.d/zz-dash-packages'
+        if [[ ! -e ${TEST_FILE_PATH} ]] || ! grep -q dash ${TEST_FILE_PATH}; then
+          echo dash > ${TEST_FILE_PATH}
+          rm -rf /var/tmp/.guestfs*
+          exit 2
+        fi
+      args:
+        executable: /bin/bash
       when:
-        - "{{ (_libguestfs_version.stdout is version('1.38.1', '<')) or
-              ((_libguestfs_version.stdout is version('1.39.0', '>=')) and
-               (_libguestfs_version.stdout is version('1.39.1', '<'))) }}"
+        - (_libguestfs_version.stdout is version('1.38.1', '<')) or
+          ((_libguestfs_version.stdout is version('1.39.0', '>=')) and
+           (_libguestfs_version.stdout is version('1.39.1', '<')))
+      register: _libguestfs_fix
+      changed_when: _libguestfs_fix.rc == 2
+      failed_when: _libguestfs_fix.rc not in [0, 2]
 
     # If the host had already installed kvm_intel.ko without nested=1, then
     # re-load it now, honoring whatever is in qemu-system-x86.modprobe
@@ -118,6 +132,7 @@
     # 3 - Nested virt not available
     - name: Ensure that nested virtualization is enabled (if it is available)
       shell: |
+        set -e
         INTEL_NESTED=/sys/module/kvm_intel/parameters/nested
         if grep -q kvm_intel /proc/modules; then
           echo "Intel CPU found. Checking for nested virtualization capabilities."