Hello ,

In this custom function i am trying to download a file and then parse it , 
that works correctly , now i want the url value to be in a variable or 
hiera and this custom function fetches that value from variable.
Using lookupvar doesn't works . Below is the code and error.

require_relative 'inifile'
require 'open-uri'

Puppet::Parser::Functions.newfunction(
  :inireadvalue, :type => :rvalue,
  :arity => 4,
  :doc => "Reads an .ini file and...") do |args|
        package = args[0]
        filename = args[1]
        section = args[2]
        key = args[3]

remote_base_url = lookupvar('$::windows_l2_agent::l2_agent_list::url')
remote_page_name = "#{package}" + "/" + "#{filename}"
remote_full_url = remote_base_url + "/" + remote_page_name
remote_data = open(remote_full_url).read
my_local_file = open("/tmp/#{filename}", "w")
my_local_file.write(remote_data)
my_local_file.close

if !File.exist?("/tmp/#{filename}")

raise(Puppet::ParseError, 'inireadvalue(): Path and file provided does not 
 exists ' +
      'Provide the correct path')
end

fileload = IniFile.load("/tmp/#{filename}")
        data = fileload[section]
        value = data[key]
        return value


      end


*Error it gives is *

Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: 
(Scope(Windows_l2_agent::Install_agent[ADSECGRP])) Could not look up 
qualified variable '$::windows_l2_agent::l2_agent_list::url'; class 
$::windows_l2_agent::l2_agent_list could not be found
Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: undefined method `+' for 
nil:NilClass at 
/etc/puppetlabs/puppet/environments/development/modules/windows_l2_agent/manifests/install_agent.pp:3
 
on node cnpdcccvd0434.cdbt.pldc.kp.org
Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: Wrapped exception:
Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: undefined method `+' for 
nil:NilClass
Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: undefined method `+' for 
nil:NilClass at 
/etc/puppetlabs/puppet/environments/development/modules/windows_l2_agent/manifests/install_agent.pp:3
 
on node cnpdcccvd0434.cdbt.pldc.kp.org
Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: undefined method `+' for 
nil:NilClass at 
/etc/puppetlabs/puppet/environments/development/modules/windows_l2_agent/manifests/install_agent.pp:3
 
on node cnpdcccvd0434.cdbt.pldc.kp.org
Aug  4 11:04:04 pzxdcc0002 puppet-master[10376]: Report processor failed: 
Report from  contained no metrics, which is often caused by a failed 
catalog compilation. Unable to process.

Regards,
Ritesh




On Tuesday, 22 July 2014 15:36:45 UTC-6, 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 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/baa0524d-6172-4fc6-aece-31194ed136e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to