Craig , i looked at that module puppetlabs-inifile, it allows to change the settings in ini file , i want to read/parse a ini file , assign a value to a variable.
@henrik you were correct problem was with iniFile.load class . Now the functions looks like require_relative 'inifile' *This allows me to keep inifile.rb file inside the function directory. Is this correct ?* Puppet::Parser::Functions.newfunction( :inireadvalue, :type => :rvalue, :arity => 3, :doc => "Reads an .ini file and...") do |args| filename = args[0] section = args[1] key = args[2] if !File.exist?(filename) raise(Puppet::ParseError, 'inireadvalue(): Path and file provided does not exists ' + 'Provide the correct path') end fileload = IniFile.load(filename) data = fileload[section] value = data[key] return value end Thanks for your help. Regards, Ritesh Nanda On Wed, Jul 23, 2014 at 2:31 PM, Craig Barr <craig.b...@rubiconred.com> wrote: > Does this <https://forge.puppetlabs.com/puppetlabs/inifile> meet your use > case? > > > On Wednesday, 23 July 2014 07:36:45 UTC+10, 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' >> require 'inifile' >> module Puppet::Parser::Functions >> newfunction(:iniread, :type => :rvalue) do |args| >> raise(Puppet::ParseError, 'inifile read(): Wrong number of arguments ' + >> "given (#{args.size} for 3)") if args.size != 3 >> >> 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 >> >> 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 a topic in the > Google Groups "Puppet Users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/puppet-users/mNzxsAqTZ7I/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > puppet-users+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/puppet-users/705ed63d-1ae5-4fb5-afbc-6dfa7b579154%40googlegroups.com > <https://groups.google.com/d/msgid/puppet-users/705ed63d-1ae5-4fb5-afbc-6dfa7b579154%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- * With Regards * * Ritesh Nanda* -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAO5CbpBOAynKgnZFOr_QVe80Dxo5ccRwrYOsy6GhGg4_rvrbjA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.