Hosting: AWS
Chef-Server: Hosted Chef
Chef: v12.11.18 64bit (Issue also seen in 12.8.1)
OS: Windows 2012 R2 64 Bit
attributes/default.rb
default['cookbook_name']['services'] = %w(CertSvc NTDS ADWS DNS)
recipes/default.rb
node['cookbook_name']['services'].each do |service|
template "C:/Program Files/Amazon/cloudwatchMetrics/windows_service_#{service}.ps1" do
source 'cloudwatch_metric_windows_service.ps1.erb'
variables(
servicename: service.to_s
)
end
end
This code converges successfully with test kitchen. It also is successful when I run Chef in local mode. But when chef converges as a running service or via command line, I get the following:
NoMethodError
undefined method `each' for nil:NilClass
Platform:
x64-mingw32
←[0m
Running handlers:
Running handlers complete
Chef Client failed. 0 resources updated in 11 seconds
We have seen this behavior with multiple cookbooks. We have reached out to Chef support, but have been unable to get a fix. Any ideas on how to fix this? We do not have an on premise Chef-server, I am curious if the issue can be reproduced.
Hello @jsmickey
Do you need to store the services in an attribute? If these are fixed and never change you can just iterate your array directly in the recipe
%w(CertSvc NTDS ADWS DNS).each do |service|
Does the nodes run list include your cookbook?
It sounds obvious but have you got any role or attribute overrides or do you have an attribute file (not default) that also declares this attribute?
We’ve seen some community cookbooks attribute names clashing with something Ohai will retrieve, so we have a rule that the attributes are name spaced, prefixing with a company name.
Are you in a position where you can you use chef-shell to load the cookbooks and query the attribute? It’s the easiest way to debug attribute values.
Thanks for the response @chris_sullivan
If I put the array in the recipe, it does work. This is the second cookbook where we have experienced this issue and we would like to figure out a root cause.
I did some work with chef-shell. The value of default[‘cookbook_name’][‘services’] is nil.
When I run this cookbook with chef-zero via test-kitchen or in local mode, the converge is successful.
It appears the Windows chef-client relationship between node and chef-server has an issue evaluating attributes in the form of an array. I’m not sure how to debug this further and prove my hypothesis.
Thanks for the update @jsmickey
I’m interested to know what the problem is, when you used Chef-Shell with the recipes/run list did you also use the attribute debug_value method to see if it was being set but overridden?
https://docs.chef.io/debug.html#debug-value
On one of the nodes having the issue, I ran:
chef-shell -z
The output is the following
NoMethodError
undefined method `each' for nil:NilClass
Platform:
x64-mingw32
epic fail!
C:/chef/cache/cookbooks/aws_tools/recipes/aws_cloudwatch_windows.rb:100:in from _file': undefined method
each' for nil:NilClass (NoMethodError)
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/mixin/from_file.rb:30:in instance_eval' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/mixin/from_file.rb:30:in
from_file'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/cookbook_version.rb:233:in load_recipe' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context.rb:354:in
load_recipe'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context.rb:310:in block in include_recipe' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context.rb:309:in
each'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context.rb:309:in include_recipe' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/dsl/include_recipe.rb:26:in
include_recipe'
from C:/chef/cache/cookbooks/aws_tools/recipes/default.rb:16:in from_fi le' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/mixin/from_file.rb:30:in
instance_eval'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/mixin/from_file.rb:30:in from_file' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/cookbook_version.rb:233:in
load_recipe'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context.rb:354:in load_recipe' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context.rb:310:in
block in include_recipe'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context.rb:309:in each' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context.rb:309:in
include_recipe'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/dsl/include_recipe.rb:26:in include_recipe' from C:/chef/cache/cookbooks/crib/recipes/default.rb:17:in
from_file'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/mixin/from_file.rb:30:in instance_eval' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/mixin/from_file.rb:30:in
from_file'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/cookbook_version.rb:233:in load_recipe' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context.rb:354:in
load_recipe'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context/cookbook_compiler.rb:140:in block in compil e_recipes' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context/cookbook_compiler.rb:138:in
each'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context/cookbook_compiler.rb:138:in compile_recipes ' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/run_context/cookbook_compiler.rb:75:in
compile'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/run_context.rb:176:in load' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/shell/shell_session.rb:208:in
rebuild_context'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/shell/shell_session.rb:59:in block in reset!' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/shell/shell_session.rb:101:in
loading'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/shell/shell_session.rb:54:in reset!' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/shell.rb:127:in
session'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/lib/chef/shell.rb:136:in init' from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni versal-mingw32/lib/chef/shell.rb:65:in
start'
from C:/opscode/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.11.18-uni
versal-mingw32/bin/chef-shell:34:in <top (required)>' from C:/opscode/chef/bin/chef-shell:62:in
load'
from C:/opscode/chef/bin/chef-shell:62:in `'
I am not able to load chef-shell because the initialize fails on the missing attribute
Hi, you could add a require 'pry
;binding.pry right above the loop.
Next, run the recipe, and when it hits the breakpoint, take a look at
the debug_value
as Chris suggested.
You could also run chef-client in -l debug
mode and see if anything
useful pops out.
Thank you @Alex_Vinyar and @chris_sullivan
Pry was very helpful. We were able to dig in and see the issue with the variable. Problem solved. Thank you.
What ended up being the problem?