Hiera lookup failure on nested array - undefined method `[]' for nil:NilClass

Visto 461 veces
Saltar al primer mensaje no leído

Rob Shand

no leída,
23 jun 2014, 8:29:5923/6/14
a puppet...@googlegroups.com
Hi All,

So this was working on a Puppet Enterprise 3.2 puppet master but when testing on a 3.4 Puppet Master I'm getting the following failure 'undefined method `[]' for nil:NilClass' . So I know that this is telling me it's failed to retrieve a value, but I'm wondering why I might be failing to retrieve the value from the nested hash for 'locations' and 'terminals' when it was previously working on puppet 3.2.

Does anyone have any clues on why this is failing when previously it was working on an earlier release . . .? thanks in advance.

$ sudo /usr/local/bin/puppet apply --modulepath /opt/puppet/share/puppet/modules:/etc/puppetlabs/puppet/modules ./puppet-modules/digital/tests/init.pp --environment uat --noop

This file (./puppet-modules/digital/tests/init.pp) is literally just a single line containing an include on the class include 'digital' 
 
Error: Failed to parse template digital/adapter-config.json.erb:
  Filepath: /etc/puppetlabs/puppet/modules/digital/templates/adapter-config.json.erb
  Line: 6
  Detail: undefined method `[]' for nil:NilClass
 at /etc/puppetlabs/puppet/modules/digital/manifests/app.pp:41 on node localhost
Error: Failed to parse template digital/adapter-config.json.erb:
  Filepath: /etc/puppetlabs/puppet/modules/digital/templates/adapter-config.json.erb
  Line: 6
  Detail: undefined method `[]' for nil:NilClass
 at /etc/puppetlabs/puppet/modules/digital/manifests/app.pp:41 on node localhost
[ERROR] - Running puppet-modules/digital/tests/init.pp

Hiera Data :- From a hiera lookup test here's the data structure I'm using (simplified for the purposes of this thread)

$ hiera -d -c /etc/puppetlabs/puppet/hiera.yaml hash_data calling_module=digital

{"ABC"=> 
  {"name"=>"test01", "port"=>"20000", "host"=> 
    {"foo01"=> {"locations"=>"0001", "terminals"=>"1,2,3,4"}, 
     "foo02"=>{"locations"=>"0002", "terminals"=>"1,2,3,4"}}}, 
"DEF"=> {"name"=>"test02", "port"=>"20000", "host"=> 
   {"foo01"=>{"locations"=>"0002", "terminals"=>"5,6,7,8"}, 
    "foo02"=>{"locations"=>"0001", "terminals"=>"5,6,7,8"}}}}

ERB - Template File (json output file, hence the 'hash_data.size' at the end to prevent a trailing ',' in order to preserve syntax)

{
  "wager_hosts":[<% @hash_data.each_with_index do |(key, hash) ,i| -%>{
    "domain":"<%= key -%>",
    "name":"<%= hash['name'] -%>",
    "port":<%= hash['port'] -%>,
    "locations":[<%= hash['host']['foo01']['locations'] -%>],
    "terminals":[<%= hash['host']['foo01']['terminals'] -%>]
    }<%= ',' if i < (@hash_data.size - 1) %><%end-%>
  ]
}

Version info:

$ puppet --version
3.4.3 (Puppet Enterprise 3.2.3)

$ /opt/puppet/bin/ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

Server is a CentOS 6.5 box

PS - I've manually run a simplified piece of ruby code and works fine to retrieve the values when I assign the data structure, so what's going on ?

$ cat test.erb
#!/opt/pupppet/bin/ruby
#
require 'erb'

data={"NSW"=> {"name"=>"test01", "port"=>"20000", "host"=> {"foo01"=>{"locations"=>"0001", "terminals"=>"1,2,3,4"}, "foo02"=>{"locations"=>"0002", "terminals"=>"1,2,3,4"}}}, "VIC"=> {"name"=>"test02", "port"=>"20000", "host"=> {"foo01"=>{"locations"=>"0002", "terminals"=>"5,6,7,8"}, "foo02"=>{"locations"=>"0001", "terminals"=>"5,6,7,8"}}}}

data.each_with_index do |(key, hash) ,i|
  p hash['name']
  p hash['port']
  p hash['host']['foo01']['locations']
  p hash['host']['foo01']['terminals']
end
[vagrant@vagrant-centos65 ~]$ /opt/puppet/bin/ruby test.erb
"test01"
"20000"
"0001"
"1,2,3,4"
"test02"
"20000"
"0002"
"5,6,7,8"

jcbollinger

no leída,
24 jun 2014, 10:29:3624/6/14
a puppet...@googlegroups.com


On Monday, June 23, 2014 7:29:59 AM UTC-5, Rob Shand wrote:
Hi All,

So this was working on a Puppet Enterprise 3.2 puppet master but when testing on a 3.4 Puppet Master I'm getting the following failure 'undefined method `[]' for nil:NilClass' . So I know that this is telling me it's failed to retrieve a value, but I'm wondering why I might be failing to retrieve the value from the nested hash for 'locations' and 'terminals' when it was previously working on puppet 3.2.



I'm inclined to suspect that either it wasn't quite the same thing that worked on PE 3.2 (considering both manifests and data), or that it didn't work as well as you thought.

Alternatively, is the Puppet version the only thing that changed?  For example, are you running on the same version of Ruby?

 
Does anyone have any clues on why this is failing when previously it was working on an earlier release . . .? thanks in advance.

$ sudo /usr/local/bin/puppet apply --modulepath /opt/puppet/share/puppet/modules:/etc/puppetlabs/puppet/modules ./puppet-modules/digital/tests/init.pp --environment uat --noop

This file (./puppet-modules/digital/tests/init.pp) is literally just a single line containing an include on the class include 'digital' 
 
Error: Failed to parse template digital/adapter-config.json.erb:
  Filepath: /etc/puppetlabs/puppet/modules/digital/templates/adapter-config.json.erb
  Line: 6
  Detail: undefined method `[]' for nil:NilClass
 at /etc/puppetlabs/puppet/modules/digital/manifests/app.pp:41 on node localhost
Error: Failed to parse template digital/adapter-config.json.erb:
  Filepath: /etc/puppetlabs/puppet/modules/digital/templates/adapter-config.json.erb
  Line: 6
  Detail: undefined method `[]' for nil:NilClass
 at /etc/puppetlabs/puppet/modules/digital/manifests/app.pp:41 on node localhost
[ERROR] - Running puppet-modules/digital/tests/init.pp

Hiera Data :- From a hiera lookup test here's the data structure I'm using (simplified for the purposes of this thread)


And you can reproduce your error with that simplified data?  Otherwise it's useless for us to examine it.

 

$ hiera -d -c /etc/puppetlabs/puppet/hiera.yaml hash_data calling_module=digital

{"ABC"=> 
  {"name"=>"test01", "port"=>"20000", "host"=> 
    {"foo01"=> {"locations"=>"0001", "terminals"=>"1,2,3,4"}, 
     "foo02"=>{"locations"=>"0002", "terminals"=>"1,2,3,4"}}}, 
"DEF"=> {"name"=>"test02", "port"=>"20000", "host"=> 
   {"foo01"=>{"locations"=>"0002", "terminals"=>"5,6,7,8"}, 
    "foo02"=>{"locations"=>"0001", "terminals"=>"5,6,7,8"}}}}

ERB - Template File (json output file, hence the 'hash_data.size' at the end to prevent a trailing ',' in order to preserve syntax)

{
  "wager_hosts":[<% @hash_data.each_with_index do |(key, hash) ,i| -%>{


I don't like "hash" as a variable name, as it seems both uninformative and needlessly risky of conflicting with a keyword or function name (though I am not aware of any actual conflict).

 
    "domain":"<%= key -%>",
    "name":"<%= hash['name'] -%>",
    "port":<%= hash['port'] -%>,
    "locations":[<%= hash['host']['foo01']['locations'] -%>],
    "terminals":[<%= hash['host']['foo01']['terminals'] -%>]
    }<%= ',' if i < (@hash_data.size - 1) %><%end-%>
  ]
}



My comment above notwithstanding, I don't see anything clearly wrong with your template.  That leaves me suspicious of your data, but I don't think the template you present would break when the value of @hash_data is what you present.

If it is really true that exactly the same code, template, and manifests work under PE 3.2, but fail under PE 3.4, then the most likely locus of the problem is the part you didn't show us: the manifests.  The most relevant is the one containing the template() call that causes the given template to be evaluated.


John

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos