Last active
August 29, 2015 14:16
-
-
Save stefanandres/fcecd1bff8f254d78630 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| facter_to_hiera: | |
| #!/usr/bin/env python | |
| import json | |
| import re | |
| from subprocess import check_output | |
| from optparse import OptionParser | |
| def process_args(): | |
| parser = OptionParser() | |
| parser.add_option("--key", action="append", help="This option takes " | |
| "a <key>=<value> pair as its argument and may be specified " | |
| "multiple times. Existing facts' values that match <key> " | |
| "will be replaced by the specified value. This will only " | |
| "affect facts existing on this system. Non-existent facts " | |
| "will not be added.") | |
| (options, args) = parser.parse_args() | |
| return options, args | |
| if __name__ == '__main__': | |
| options, args = process_args() | |
| facts = json.loads(check_output('facter -j', shell=True)) | |
| facts_new = {} | |
| for fact in facts: | |
| if options.key: | |
| for option in options.key: | |
| o = option.split('=') | |
| if o[0] == fact: | |
| facts[fact] = o[1] | |
| fact_replaced = re.sub('(.*)', '::\g<1>', fact) | |
| facts_new[fact_replaced] = facts[fact] | |
| print json.dumps(facts_new) | |
| hiera_dump: | |
| #!/usr/bin/env ruby | |
| # 2015, s.andres@syseleven.de | |
| require 'rubygems' | |
| require 'hiera' | |
| require 'puppet' | |
| require 'json' | |
| def replace_facts(input, replaceby = {}) | |
| """ | |
| replace occuring hiera variables like %{::fqdn} by the value of | |
| replaceby[::fqdn] if available, but for all keys in replaceby = {} | |
| """ | |
| replaceby.keys.each { |x| | |
| #puts x | |
| if input.include? x | |
| input = input.gsub(/%\{#{x}\}/, replaceby[x]) | |
| end | |
| } | |
| ret = input | |
| ret | |
| end | |
| hiera = Hiera.new(:config => "/etc/puppet/hiera.yaml") | |
| hiera_yaml = hiera.config | |
| datadir = hiera_yaml[:yaml][:datadir] | |
| scope = `facter_to_hiera #{ARGV.join(' ')}` | |
| scope = JSON.load(scope) | |
| hierarchy = [] | |
| hiera_yaml[:hierarchy].each { |x| | |
| hierarchy << replace_facts(datadir + '/' + x, scope) | |
| } | |
| node_vars = [] | |
| node_classes = [] | |
| hierarchy.each { |x| | |
| if File.exist?(x + '.yaml') | |
| myyaml = YAML.load_file(x + '.yaml') | |
| if myyaml | |
| for key in myyaml.keys | |
| if key == 'classes' | |
| node_classes << myyaml['classes'] | |
| next | |
| end | |
| if not node_vars.include?(key) | |
| node_vars << key | |
| end | |
| end | |
| end | |
| end | |
| } | |
| puts 'classes: %s' % [node_classes.flatten.uniq] | |
| for node_var in node_vars | |
| puts "%s: %s" % [ node_var, hiera.lookup(node_var, nil, scope) ] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment