From 76c83a4096d5f58aba7130842d0adc6356452917 Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Thu, 2 Aug 2018 16:23:05 +0100
Subject: [PATCH] Allow 400 response when creating kibana ES index

If the index already exists, kibana will respond with a 400 error. This
is required to support reconfigure and upgrade of kibana.

Change-Id: Ieee6d636fe81692dffb886f917b8398df10a525f
---
 ansible/roles/kibana/tasks/post_config.yml | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/ansible/roles/kibana/tasks/post_config.yml b/ansible/roles/kibana/tasks/post_config.yml
index 108c25662f..75536f319c 100644
--- a/ansible/roles/kibana/tasks/post_config.yml
+++ b/ansible/roles/kibana/tasks/post_config.yml
@@ -11,7 +11,13 @@
     method: PUT
     body: "{{ kibana_default_index_options | to_json }}"
     body_format: json
-    status_code: 200, 201
+    status_code: 200, 201, 400
+  register: result
+  failed_when:
+    # If the index already exists, Elasticsearch will respond with a 400 error.
+    - result.status == 400
+    # Format: {"json": {"error": {"type": "index_already_exists_exception"}}}
+    - result.get('json', {}).get('error', {}).get('type') != 'index_already_exists_exception'
   run_once: true
 
 - name: Wait for kibana to register in elasticsearch
-- 
GitLab