Skip to content

Instantly share code, notes, and snippets.

@munhitsu
Created March 26, 2015 10:55
Show Gist options
  • Save munhitsu/f66fa0b4300669874664 to your computer and use it in GitHub Desktop.
Save munhitsu/f66fa0b4300669874664 to your computer and use it in GitHub Desktop.
salt module function to save dictionary as a json file
import json
import logging
log = logging.getLogger(__name__)
def save_dictionary(filename, # whe to output dictionary
**kwargs # the dictionary itself
):
"""
creates json file in a format:
{
"key1":"value1",
...
}
discarding all keys starting from "__" as it's used internally by salt
saves file as salt running user with current umask
"""
data = {}
for k, v in kwargs.iteritems():
if not k.startswith("__"):
data[k] = v
with open(filename, mode='w') as f:
f.write(json.dumps(data, sort_keys=True, indent=2))
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment