From 9a896aa81a4c37eb344de6a5852f747e2f61b122 Mon Sep 17 00:00:00 2001 From: Kevin Carter Date: Thu, 20 Dec 2018 16:58:25 -0600 Subject: [PATCH] Fence options before casting to json These options could be "undefined" which is an object and not json serializable. This change ensures if an option is undefined it defaults to an empty set which will allow the option to be json serialized. Change-Id: I1a81bafa441aa6400bfbec50d57e56df4d09bda3 Signed-off-by: Kevin Carter --- .../roles/elastic_heartbeat/templates/heartbeat.yml.j2 | 10 ++++++---- elk_metrics_6x/templates/_macros.j2 | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/elk_metrics_6x/roles/elastic_heartbeat/templates/heartbeat.yml.j2 b/elk_metrics_6x/roles/elastic_heartbeat/templates/heartbeat.yml.j2 index 732acae4..49324b97 100644 --- a/elk_metrics_6x/roles/elastic_heartbeat/templates/heartbeat.yml.j2 +++ b/elk_metrics_6x/roles/elastic_heartbeat/templates/heartbeat.yml.j2 @@ -11,7 +11,9 @@ ############################# Heartbeat ###################################### {% set icmp_hosts = [] %} {% for host_item in groups['all'] %} -{% set _ = icmp_hosts.extend([hostvars[host_item]['ansible_host']]) %} +{% if hostvars[host_item]['ansible_host'] is defined %} +{% set _ = icmp_hosts.extend([hostvars[host_item]['ansible_host']]) %} +{% endif %} {% endfor %} # Configure monitors heartbeat.monitors: @@ -28,7 +30,7 @@ heartbeat.monitors: schedule: '@every 30s' # every 30 seconds from start of beat # List of hosts to ping - hosts: {{ icmp_hosts | to_json }} + hosts: {{ (icmp_hosts | default([])) | to_json }} # Configure IP protocol types to ping on if hostnames are configured. # Ping all resolvable IPs if `mode` is `all`, or only one IP if `mode` is `any`. ipv4: true @@ -103,7 +105,7 @@ heartbeat.monitors: # Using `tls`/`ssl`, an SSL connection is established. If no ssl is configured, # system defaults will be used (not supported on windows). # If `port` is missing in url, the ports setting is required. - hosts: {{ hosts | to_json }} + hosts: {{ (hosts | default([])) | to_json }} # Configure IP protocol types to ping on if hostnames are configured. # Ping all resolvable IPs if `mode` is `all`, or only one IP if `mode` is `any`. @@ -169,7 +171,7 @@ heartbeat.monitors: schedule: '@every 60s' # every 30 seconds from start of beat # Configure URLs to ping - urls: {{ hosts | to_json }} + urls: {{ (hosts | default([])) | to_json }} # Configure IP protocol types to ping on if hostnames are configured. # Ping all resolvable IPs if `mode` is `all`, or only one IP if `mode` is `any`. diff --git a/elk_metrics_6x/templates/_macros.j2 b/elk_metrics_6x/templates/_macros.j2 index c53d2709..ee8a4466 100644 --- a/elk_metrics_6x/templates/_macros.j2 +++ b/elk_metrics_6x/templates/_macros.j2 @@ -8,7 +8,7 @@ output.elasticsearch: # Scheme and port can be left out and will be set to the default (http and 9200) # In case you specify and additional path, the scheme is required: http://localhost:9200/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:9200 - hosts: {{ data_hosts | to_json }} + hosts: {{ (data_hosts | default([])) | to_json }} # Set gzip compression level. compression_level: 3 @@ -99,7 +99,7 @@ output.logstash: enabled: true # The Logstash hosts - hosts: {{ data_hosts | to_json }} + hosts: {{ (data_hosts | default([])) | to_json }} # Number of workers per Logstash host. worker: 1 @@ -393,7 +393,7 @@ xpack.monitoring.elasticsearch: # Scheme and port can be left out and will be set to the default (http and 9200) # In case you specify and additional path, the scheme is required: http://localhost:9200/path # IPv6 addresses should always be defined as: https://[2001:db8::1]:9200 - hosts: {{ data_hosts | to_json }} + hosts: {{ (data_hosts | default([])) | to_json }} # Set gzip compression level. compression_level: 9