Last active
November 15, 2017 21:42
-
-
Save absolutejam/5647949a6f31b005ee43f3a86d9da89a to your computer and use it in GitHub Desktop.
Dict to list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file.managed and cmd.run echo output | |
{"client": {"services": {"critical": "ReportServer$OLIVER,MSSQL$OLIVER,SoftlinkOliver"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- set vars = salt['pillar.get']('sensu:vars', {}) -%} | |
{%- set vars_listified = {} -%} | |
{%- for service, values in vars.iteritems() -%} | |
{# Create the service entry #} | |
{%- do vars_listified.update({ service: {} }) -%} | |
{%- if values is mapping -%} | |
{%- for importance, items in values.items() -%} | |
{%- set items_list = items.keys() | join(',') -%} | |
{%- do vars_listified[service].update({ importance: items_list }) -%} | |
{%- endfor -%} | |
{%- else -%} | |
{%- do vars_listified.update({ service: values }) -%} | |
{%- endif -%} | |
{%- endfor -%} | |
{%- set client_vars = {} -%} | |
{%- do client_vars.update({ 'client': vars_listified }) -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sensu: | |
vars: | |
services: | |
critical: | |
# Use dict notation ('item':) instead :f list | |
# notation (- 'item') for dict merging! | |
'ReportServer$OLIVER': | |
'MSSQL$OLIVER': | |
'SoftlinkOliver': |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
...snip... | |
"vars": { | |
"services": { | |
"important": "u'salt-minion", | |
"telegraf'": "None" | |
} | |
}, | |
...snip... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client: | |
{# REQUIRED #} | |
{% if salt['grains.get']('os_family') == 'Windows' %} | |
name: {{ salt['grains.get']('host') | lower }} | |
{% else %} | |
name: {{ salt['grains.get']('localhost') | lower }} | |
{% endif %} | |
address: {{ salt['grains.get']('fqdn_ip4', [ 'localhost' ])[0] }} | |
subscriptions: {{ salt['pillar.get']('nodegroups') }} | |
{# KEEPALIVE #} | |
keepalive: | |
handlers: | |
- default | |
- saltstack | |
{# CLIENT SOCKET #} | |
socket: | |
bind: 127.0.0.1 | |
port: 3030 | |
{%- set vars = salt['pillar.get']('sensu:vars', {}) -%} | |
{%- set vars_listified = {} -%} | |
{%- for service, values in vars.iteritems() -%} | |
{# Create the service entry #} | |
{%- do vars_listified.update({ service: {} }) -%} | |
{%- if values is mapping -%} | |
{%- for importance, items in values.items() -%} | |
{%- set items_list = items.keys() | join(',') -%} | |
{%- do vars_listified[service].update({ importance: items_list }) -%} | |
{%- endfor -%} | |
{%- else -%} | |
{%- do vars_listified.update({ service: values }) -%} | |
{%- endif -%} | |
{%- endfor -%} | |
{%- set client_vars = {} -%} | |
{% do client_vars.update({ 'client': vars_listified }) %} | |
vars: {{ vars_listified }} | |
{# OPTIONAL #} | |
domain: {{ salt['grains.get']('windowsdomain') }} | |
os: | |
family: {{ salt['grains.get']('os_family') }} | |
name: {{ salt['grains.get']('osfullname') }} | |
specs: | |
cpu_model: {{ salt['grains.get']('cpu_model') }} | |
cpu_num: {{ salt['grains.get']('num_cpus') }} | |
mem: {{ salt['grains.get']('mem_total') }} |
yeah, totally understand what you are trying to do.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I realise this is ugly and hacky, but I needed a way to have values merged from different pillars, and empty dicts was the only way.