Skip to content

Instantly share code, notes, and snippets.

@numkem
Last active August 29, 2015 14:17
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 numkem/a3fb9290b0cf74de2092 to your computer and use it in GitHub Desktop.
Save numkem/a3fb9290b0cf74de2092 to your computer and use it in GitHub Desktop.
This sls will look and include a file named by the minion's grain 'host' value from a directory specified. If the file doesn't exists, it will try to create it while leaving it empty.
#!py
import logging
import yaml
import sys
import os
PILLAR_HOST_DIR = "/srv/salt/hosts"
log = logging.getLogger(__name__)
def run():
log.debug("Checking if there is a file for host {0}"
.format(__grains__['host']))
pillar_host_file = os.path.join(PILLAR_HOST_DIR,
__grains__['host']) + '.sls'
log.debug("Pillar host filename: {0}".format(pillar_host_file))
try:
if os.path.isfile(pillar_host_file):
log.debug("File exists, trying to read {0}"
.format(pillar_host_file))
stream = file(pillar_host_file)
data = yaml.load(stream)
return data
else:
log.debug("Pillar host file doesn't exists, creating it")
open(pillar_host_file, 'a').close()
return {}
except IOError as e:
log.error("Cannot open pillar host file {0}: {1}".format(
pillar_host_file, e))
sys.exit(1)
# vim: syntax=python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment