I defined a custom ruby function called create_type Then if in my hiera, I define :
localclasses:
- class1:
arg1: value
arg2: value
- class2:
arg1: value
arg2: value
- class3
A simple call to :
$localclasses = hiera_array("localclasses", [])
create_type("class", $localclasses) will do the trick.
I use hiera_array to merge many hiera files.
It allows to have very readable hiera files to my taste.
I wrote create_type to have a ressource creation that was not too picky about
the way it's called.
You need to put create_type.rb in <modules>/lib/puppet/parser/functions
Le 3 janv. 2014 à 15:54, Andrey Kozichev <[email protected]> a écrit :
> Hi!
> Couldn't find the answer in the documentation.
> Is it possible to source data from Hiera directly into parametrised classes?
>
> To explain:
>
> I have class mcollective(type,middleware_hosts,securityprovider ....)
>
> Can I define all arguments for this class in Hiera and fetch them all in one
> Hiera call and then use to declare the class?
>
> Similar what is done with create_resources for defined type but for Class?
>
>
> Just seems waste to me doing individually:
>
> $type = hiera('mcollective::type')
> $middleware_hosts = hiera('mcollective::middleware_hosts')
>
> and then in the class declaration:
> class { '::mcollective':
>
> type => $type
>
> middleware_hosts => $middleware_hosts
>
>
>
> Andrey
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CACzr%3DFf9kEP7nzX8D72Kw2Uj_J%3DTgyMO5jkckuYz60qDJE%2BB2A%40mail.gmail.com.
> For more options, visit https://groups.google.com/groups/opt_out.
-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/0C0BCC51-CB2A-4E74-AA7A-B027B5FC1DB2%40spamcop.net. For more options, visit https://groups.google.com/groups/opt_out.
Le 3 janv. 2014 à 15:54, Andrey Kozichev <[email protected]> a écrit :
|
require 'puppet/parser/functions'
def do_type(type, class_data)
if class_data.is_a?(String)
title = class_data.strip
if title.length > 0
function_create_resources([type, {title => {} }])
end
elsif class_data.is_a?(Hash)
class_data.each do |title,params|
function_create_resources([type, { title.strip => params }])
end
elsif class_data.is_a?(Array)
class_data.each do |params|
do_type(type, params)
end
else
class_data_info = class_data.class
raise(Puppet::ParseError, "invalid create_type instanciation arguments: #{class_data_info}")
end
end
def split_args(args)
if args[0].is_a?(Array)
args[0].each do |sub_args|
split_args([sub_args]) do | type, ressources |
yield type, ressources
end
end
elsif args[0].is_a?(String)
#raise(Puppet::ParseError, "create_type(): Wrong number of arguments " +
# "given (#{args.size} for 2)") if args.size < 2
type = args[0]
ressources = args[1..args.count]
yield type, ressources
elsif args[0].is_a?(Hash)
args[0].each do | type, ressources |
yield type, ressources
end
else
args_info = args[0].class
raise(Puppet::ParseError, "invalid create_type arguments: #{args_info}")
end
end
# create_type can type 3 types of call :
# create_type([{'type1' => rsrc, ...])
# create_type(type, rsrc, ...)
# create_type({'type1' => rsrc, ... })
# rsrc can be a single ressource or a array of ressources
# rsrc is a string for a ressource title ( a class) or a map title => arguments
Puppet::Parser::Functions::newfunction(:create_type,
:type => :statement
) do |args|
Puppet::Parser::Functions.function(:create_resources)
Puppet::Parser::Functions.function(:defined_with_params)
Puppet::Parser::Functions.function(:capitalize)
split_args(args) do | type, ressources |
type = function_capitalize([type])
# If current arg is an array, iterate other it
if ressources.is_a?(Array)
ressources.each do |sub_arg|
do_type(type, sub_arg)
end
# if arg is not false or nil, instanciate it
elsif ressources
do_type(type, ressources)
end
end
end
