Skip to content

Instantly share code, notes, and snippets.

@absolutejam
Last active December 6, 2017 10:29
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/4c371d87670b3b1d21f88dd8abfd2f83 to your computer and use it in GitHub Desktop.
Save absolutejam/4c371d87670b3b1d21f88dd8abfd2f83 to your computer and use it in GitHub Desktop.
Convert a list to a comma separated string, within a dict
{%- macro recurse_dict(data) -%}
{%- set topdict = {} -%}
{%- for k,v in data.iteritems() %}
{%- if v is mapping -%}
{%- do topdict.update({ k: recurse_dict(v) }) -%}
{%- elif v is iterable and v is not string -%}
{%- set list_as_str = v | join(',') -%}
{%- do topdict.update({ k: list_as_str }) -%}
{%- else -%}
{%- do topdict.update({ k: v }) -%}
{%- endif -%}
{%- endfor -%}
{{ topdict }}
{%- endmacro -%}
vars: {{ recurse_dict(salt['pillar.get']('sensu:vars')) }}
def recurse_dict(data):
topdict = {}
for k,v in data.items():
if isinstance(v, dict):
topdict[k] = recurse_dict(v)
elif isinstance(v, list):
list_as_str = ','.join(v)
topdict[k] = list_as_str
else:
topdict[k] = v
return topdict
# yay!
def stringify_dict_list(pillar_path):
def recurse_dict(data):
topdict = {}
for k,v in data.items():
if isinstance(v, dict):
topdict[k] = recurse_dict(v)
elif isinstance(v, list):
list_as_str = ','.join(v)
topdict[k] = list_as_str
else:
topdict[k] = v
return topdict
data = __salt__['pillar.get'](pillar_path, {})
return recurse_dict(data)
@absolutejam
Copy link
Author

Please don't judge me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment