Skip to content

Instantly share code, notes, and snippets.

@ohadlevy
Forked from alvagante/add_arguments.rb
Created October 28, 2011 09:28
Show Gist options
  • Save ohadlevy/1321951 to your computer and use it in GitHub Desktop.
Save ohadlevy/1321951 to your computer and use it in GitHub Desktop.
Puppet add_arguments.rb
Puppet::Parser::Functions::newfunction(:add_arguments, :doc => <<-'ENDHEREDOC') do |args|
Converts a hash into a set of arguments that are added to the specified resource.
This function takes two arguments: a resource name, and a hash describing
a set of resources. The hash should be in the form `{title => {parameters} }`:
# A hash of arguments:
$myarguments = {
uid => '1330',
group => allstaff,
groups => ['developers', 'operations', 'release'],
}
add_arguments(User["al"], $myarguments)
ENDHEREDOC
raise ArgumentError, ("add_arguments(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2
resource_name = args[0]
args[1].each do |title, params|
raise ArgumentError, 'params should not contain title' if(params['title'])
p_resource = Puppet::Parser::Resource.new(resource_name)
# p_resource = Puppet::Parser::Resource.new(type_name, title, :scope => self, :source => resource)
params.merge(:name => title).each do |k,v|
p_resource.set_parameter(k,v)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment