0
votes

I am newbie in ruby and chef. I was trying to create a file through template resource. After running chef-apply command , I am getting some error "NoMethodError :undefined method `preferred_filename_on_disk_location' for nil:NilClass " . I tried to google it, but no correct solution found.

Please help.

chef-apply abc.rb

Recipe: (chef-apply cookbook)::(chef-apply recipe) * template[file.txt] action create

================================================================================
Error executing action `create` on resource 'template[file.txt]'
================================================================================

NoMethodError
-------------
undefined method `preferred_filename_on_disk_location' for nil:NilClass

Resource Declaration:
---------------------
# In abc.rb

 10: template "file.txt" do
 11:     source "file.erb"
 12:     mode "0666"
 13: end
 14: 

Compiled Resource:

# Declared in abc.rb:10:in `run_chef_recipe'

template("file.txt") do
  action [:create]
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  source "file.erb"
  declared_type :template
  cookbook_name "(chef-apply cookbook)"
  recipe_name "(chef-apply recipe)"
  mode "0666"
  path "file.txt"
end

Platform:
---------
x86_64-linux

[2017-01-02T18:37:07-05:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out [2017-01-02T18:37:07-05:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report [2017-01-02T18:37:07-05:00] FATAL: NoMethodError: template[file.txt] ((chef-apply cookbook)::(chef-apply recipe) line 10) had an error: NoMethodError: undefined method `preferred_filename_on_disk_location' for nil:NilClass

1
Welcome to StackOverflow. Please try and fix the formatting of your question - it will be much easier to help you if your code and error messages are properly formatted.Michael Lihs

1 Answers

1
votes

It seems like chef-apply is not able to run the template resource properly (see https://tickets.opscode.com/browse/CHEF-5317). As a better approach to run your recipes, you should use chef-solo.

The command would be

chef-solo -o 'recipe[COOKBOOK_NAME]' -c CONFIG_FILE

where CONFIG_FILE contains some configuration for Chef, e.g.

current_dir = File.dirname(__FILE__)

file_cache_path '/tmp/chef-cache'
cookbook_path ["#{current_dir}/cookbooks"]
data_bag_path "#{current_dir}/databags"
ssl_verify_mode :verify_none

You can call this file solo.rb, put it in the same directory where you run the Chef command above, then it will look like

chef-solo -o 'recipe[COOKBOOK_NAME]' -c solo.rb

Replace COOKBOOK_NAME with the name of the cookbook you want to run, add ::recipe_name if you want to run the recipe_name.rb recipe and not the default.rb recipe.