Skip to content

Instantly share code, notes, and snippets.

@absolutejam
Last active November 15, 2017 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absolutejam/5647949a6f31b005ee43f3a86d9da89a to your computer and use it in GitHub Desktop.
Save absolutejam/5647949a6f31b005ee43f3a86d9da89a to your computer and use it in GitHub Desktop.
Dict to list
# file.managed and cmd.run echo output
{"client": {"services": {"critical": "ReportServer$OLIVER,MSSQL$OLIVER,SoftlinkOliver"}}}
{%- 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 }) -%}
sensu:
vars:
services:
critical:
# Use dict notation ('item':) instead :f list
# notation (- 'item') for dict merging!
'ReportServer$OLIVER':
'MSSQL$OLIVER':
'SoftlinkOliver':
...snip...
"vars": {
"services": {
"important": "u'salt-minion",
"telegraf'": "None"
}
},
...snip...
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') }}
@absolutejam
Copy link
Author

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.

@gtmanfred
Copy link

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