Last active
December 6, 2017 10:29
-
-
Save absolutejam/4c371d87670b3b1d21f88dd8abfd2f83 to your computer and use it in GitHub Desktop.
Convert a list to a comma separated string, within a dict
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
{%- 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')) }} |
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
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 |
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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please don't judge me.