Last active
August 29, 2015 14:17
-
-
Save numkem/75706423e8f0063278f0 to your computer and use it in GitHub Desktop.
This is an ext_pillar for Salt that will go though paths looking for a folder matching the minon's Organization already set in Foreman.
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
#!/usr/bin/env python2 | |
""" | |
foreman_organization: This is an ext_pillar for Salt that will go though paths | |
looking for a folder matching the minon's | |
Organization already set in Foreman. | |
By default it will look for a file named 'init.sls' inside the organization's | |
folder but it will also look for any files in the pillar named 'roles' | |
by it's role name. | |
Example master configuration: | |
ext_pillar: | |
- puppet: /usr/bin/foreman-node | |
- foreman_organization: | |
organization_paths: | |
base: # Name of the salt environment | |
- /srv/salt/base/pillar/organizations | |
default_organization: common | |
""" | |
import logging | |
import yaml | |
import sys | |
import os | |
log = logging.getLogger(__name__) | |
def read_yaml_file(filename): | |
stream = file(filename) | |
data = yaml.load(stream) | |
return data if data is not None else {} | |
def ext_pillar(minion_id, pillar, organization_paths, default_organization): | |
# Does the minion has a organization specified by Foreman | |
organization = pillar['organization'].lower() if 'organization' in pillar \ | |
else default_organization.lower() | |
data = {} | |
for organization_path in organization_paths: | |
try: | |
# Take everything from the default organization data | |
default_data_filename = os.path.join(organization_path, | |
organization, 'init.sls') | |
if os.path.isfile(default_data_filename): | |
default_data = read_yaml_file(default_data_filename) | |
else: | |
default_data = {} | |
# Now take everything from every roles this minion is assigned to | |
roles = pillar['roles'] if 'roles' in pillar else [] | |
roles_data = [] | |
for role in roles: | |
role_filename = os.path.join(organization_path, organization, | |
'roles', role + '.sls') | |
if os.path.isfile(role_filename): | |
roles_data.append(read_yaml_file(role_filename)) | |
# Data obtained, merge everything together | |
data.update(default_data) | |
for role in roles_data: | |
data.update(role) | |
except IOError as e: | |
logger.error("Error while handling file: {0}".format(e)) | |
sys.exit(1) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment