From 1f00d0c2cfeda690060522ac9da0b41a6b6a434c Mon Sep 17 00:00:00 2001 From: Michael Mol Date: Mon, 12 Jun 2017 17:30:22 -0400 Subject: [PATCH 1/2] Add flexjoin macro The flexjoin() macro simplifies support for mergable and composable structures. All three of these data structures will result in the string 'a s d f'. The third structure, using dicts, also supports pillar merging. - 'a s d f' - [ a, s, d, f] - { a: ~, s: ~, d: ~, f: ~} --- .../default/etc/zabbix/zabbix_agentd.conf.jinja | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja b/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja index a71f338e..d27e9772 100644 --- a/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja +++ b/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja @@ -1,4 +1,17 @@ {% from "zabbix/map.jinja" import zabbix with context -%} +{# Consider a value, keep a string, join a list, or join a dict's sorted keys -#} +{% macro flexjoin(srcval, sep=' ') -%} + {# If it's a simple value, emit it. -#} + {% if srcval is string or srcval is number -%} + {{ srcval -}} + {# If it's a dict, take the keys (for pillar merging), sort them (for stability), flexjoin them. -#} + {% elif srcval is mapping -%} + {{ flexjoin( srcval.keys()|sort, sep) -}} + {# If it's a list, join it. -#} + {% else -%} + {{ srcval|join(sep) -}} + {% endif -%} +{% endmacro -%} {% set settings = salt['pillar.get']('zabbix-agent', {}) -%} # Managed by saltstack # do not edit this file. From af7d74148b268f31804dbf47258f0ba30645c462 Mon Sep 17 00:00:00 2001 From: Michael Mol Date: Mon, 12 Jun 2017 17:50:01 -0400 Subject: [PATCH 2/2] Support composable Host Metadata Take advantage of Salt's pillar merging to compose host metadata; in this way, we can apply multiple roles to a node and have the Zabbix agent report the combination of applied roles to the Zabbix server. In turn, we can automatically apply role-specific templates through the Zabbix server's host registration process. The following pillar data structures are all valid: - zabbix-agent: # Not mergable hostmetadata: my-host-metadata # Not mergable - zabbix-agent: # Not mergable; Zabbix cannot merge lists hostmetadata: - metadata component 1 - metadata component 2 - metadata component 3 - zabbix-agent: # Supports pillar merging hostmetadata: metadata component 1: metadata component 2: metadata component 3: --- zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja b/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja index d27e9772..684dc64d 100644 --- a/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja +++ b/zabbix/files/default/etc/zabbix/zabbix_agentd.conf.jinja @@ -143,7 +143,7 @@ Hostname={{ settings.get('hostname', salt['grains.get']('id')) }} # An agent will issue an error and not start if the value is over limit of 255 characters. # If not defined, value will be acquired from HostMetadataItem. # -{% if settings.get('hostmetadata', false) %}HostMetadata={{ settings.get('hostmetadata') }}{% endif %} +{% if settings.get('hostmetadata', false) %}HostMetadata={{ flexjoin(settings.get('hostmetadata'), ' ') }}{% endif %} ### Option: HostMetadataItem # Optional parameter that defines an item used for getting host metadata.