From 1f9171082e3482986339af0dc2e540be462dec1b Mon Sep 17 00:00:00 2001
From: Kevin Carter <kevin.carter@rackspace.com>
Date: Tue, 18 Sep 2018 19:02:10 -0500
Subject: [PATCH] Add more optionality when customizing node roles

The node roles would apply attributes to hosts if an override was set or
if a node was part of a given group as determined through auto-detection.
This change will now add nodes to a given role when set manually and
will ensure no extra nodes are added to the role if the count meets or
exceeds what's required to run the service.

Change-Id: Ied5f564f0328488d3359ec4dc8e9ad17fefe5eaf
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
---
 .../vars/data-node-variables.yml              | 27 ++++++++++++++-----
 .../templates/elasticsearch.yml.j2            |  4 +--
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/elk_metrics_6x/roles/elastic_data_hosts/vars/data-node-variables.yml b/elk_metrics_6x/roles/elastic_data_hosts/vars/data-node-variables.yml
index a161df10..0cb7de96 100644
--- a/elk_metrics_6x/roles/elastic_data_hosts/vars/data-node-variables.yml
+++ b/elk_metrics_6x/roles/elastic_data_hosts/vars/data-node-variables.yml
@@ -47,8 +47,13 @@ data_nodes: |-
   {% for node in groups['elastic-logstash'] %}
   {%   if (hostvars[node]['elasticsearch_node_data'] is defined) and (hostvars[node]['elasticsearch_node_data'] | bool) %}
   {%     set _ = nodes.append(node) %}
-  {%   elif (node in _data_nodes) %}
-  {%     set _ = nodes.append(node) %}
+  {%   endif %}
+  {% endfor %}
+  {% for node in groups['elastic-logstash'] %}
+  {%   if (nodes | length) <= (_data_nodes | length) %}
+  {%     if (node in _data_nodes) %}
+  {%       set _ = nodes.append(node) %}
+  {%     endif %}
   {%   endif %}
   {% endfor %}
   {{ nodes }}
@@ -62,8 +67,13 @@ logstash_nodes: |-
   {% for node in groups['elastic-logstash'] %}
   {%   if (hostvars[node]['elasticsearch_node_ingest'] is defined) and (hostvars[node]['elasticsearch_node_ingest'] | bool) %}
   {%     set _ = nodes.append(node) %}
-  {%   elif (node in _logstash_nodes) %}
-  {%     set _ = nodes.append(node) %}
+  {%   endif %}
+  {% endfor %}
+  {% for node in groups['elastic-logstash'] %}
+  {%   if (nodes | length) <= (_logstash_nodes | length) %}
+  {%     if (node in _logstash_nodes) %}
+  {%       set _ = nodes.append(node) %}
+  {%     endif %}
   {%   endif %}
   {% endfor %}
   {{ nodes }}
@@ -77,8 +87,13 @@ ingest_nodes: |-
   {% for node in groups['elastic-logstash'] %}
   {%   if (hostvars[node]['elasticsearch_node_ingest'] is defined) and (hostvars[node]['elasticsearch_node_ingest'] | bool) %}
   {%     set _ = nodes.append(node) %}
-  {%   elif (node in _ingest_nodes) %}
-  {%     set _ = nodes.append(node) %}
+  {%   endif %}
+  {% endfor %}
+  {% for node in groups['elastic-logstash'] %}
+  {%   if (nodes | length) <= (_ingest_nodes | length) %}
+  {%     if (node in _ingest_nodes) %}
+  {%       set _ = nodes.append(node) %}
+  {%     endif %}
   {%   endif %}
   {% endfor %}
   {{ nodes }}
diff --git a/elk_metrics_6x/roles/elasticsearch/templates/elasticsearch.yml.j2 b/elk_metrics_6x/roles/elasticsearch/templates/elasticsearch.yml.j2
index 655aa494..3abdee83 100644
--- a/elk_metrics_6x/roles/elasticsearch/templates/elasticsearch.yml.j2
+++ b/elk_metrics_6x/roles/elasticsearch/templates/elasticsearch.yml.j2
@@ -64,7 +64,7 @@ http.port: {{ elastic_port }}
 #<https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-node.html>
 discovery.zen.ping.unicast.hosts: {{ zen_nodes | to_json }}
 # Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
-discovery.zen.minimum_master_nodes: {{ ((master_node_count | int) // 2) + 1 }}
+discovery.zen.minimum_master_nodes: {{ elasticsearch_master_node_count | default(((master_node_count | int) // 2) + 1) }}
 # The first set of nodes in the master_node_count are marked as such
 node.master: {{ elasticsearch_node_master | default(master_node) }}
 # Every node in the master list and every other node after will be a data node
@@ -91,7 +91,7 @@ search.remote.connect: {{ data_node }}
 #
 # Block initial recovery after a full cluster restart until N nodes are started:
 #
-gateway.recover_after_nodes: {{ ((master_node_count | int) // 2) + 1 }}
+gateway.recover_after_nodes: {{ elasticsearch_master_node_count | default(((master_node_count | int) // 2) + 1) }}
 #
 # For more information, see the documentation at:
 # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>