On 2014-22-07 23:36, Ritesh Nanda wrote:
> Hello ,
>
> I was trying to write a custom function which would run on puppet master
> take input a ini file , parse a section of that ini file and assign
> its value to a variable ..
> Something like
>
> $test = iniread('example.ini', 'Program', 'path')
>
> This would assign the value to test variable when the functions runs on
> the puppet master.
>
> iniread.rb file looks like
>
> require 'rubygems'
Should typically not be used, since it may force user to use rubygems.
You don't need it since all you want is for the runtime to find the
IniFile class. The gem/or class should simply be present in a location
that puppet loads from (it uses gempath if rubygems is available, but
can also load from other places; such as modules which are not installed
as gems).
> require 'inifile'
> module Puppet::Parser::Functions
> newfunction(:iniread, :type => :rvalue) do |args|
You do not need to open the module to call newfunction, do this instead:
Puppet::Parser::Functions.newfunction(
:iniread, :type => :rvalue,
:arity => 3,
:doc => "Reads an .ini file and...") do |args|
# body here
end
> raise(Puppet::ParseError, 'inifile read(): Wrong number of arguments ' +
> "given (#{args.size} for 3)") if args.size != 3
You get this automatically by specifying :arity=>3 as an option.
(Unless you are on a very old puppet that does not support this).
> filename = args[0]
> section = args[1]
> key = args[2]
>
> file = IniFile.load(filename)
> data = file[section]
> value = data[key]
> return value
>
> end
> end
>
> It gives an error while running
>
> Error 400 on SERVER: undefined method `[]' for nil:NilClass at
> /etc/puppetlabs/puppet/modules/example/manifests/init.pp:45
>
Run with --trace to see where the exception is raised.
My guess is that IniFile.load returns nil for the filename you gave it.
You want to protect the user and raise a specific error about not being
able to load the given file.
Regards
- henrik
> init.pp has
>
> $test =iniread("example.ini", "Program", "path")
>
>
> Doing that in ruby works
>
> require 'inifile'
> filename = ARGV[0]
> section = ARGV[1]
> key = ARGV[2]
> file = IniFile.load(filename)
> data = file[section]
> InstPath = data[key]
> puts InstPath
>
> Help to this would be really appreciated.
>
> Regards,
> Ritesh
>
>
>
> --
> 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
puppet-users...@googlegroups.com
> <mailto:
puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/puppet-users/460bb860-e8cb-4022-a1a3-47fb4b0015e5%40googlegroups.com
> <
https://groups.google.com/d/msgid/puppet-users/460bb860-e8cb-4022-a1a3-47fb4b0015e5%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit
https://groups.google.com/d/optout.
--
Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/