1
votes

Is there any way to pass attributes into a chef-apply run of a cookbook?

The very first line of my cookbook is:

if node[:my_attr][:enabled]

which leads to:

NoMethodError: undefined method '[]' for nil:NilClass

when I run

chef-apply recipes/default.rb

Adding the appropriate attributes into the attributes/default.rb does not fix this problem, neither does adding an initialization for node[:my_attr] = {}.

How can I pass node attributes into a chef-apply run?

2
Did any of the answers work for you? Please don't forget to mark an answer as correct! :) - sethvargo

2 Answers

0
votes

In the attributes/default.rb declare

default['my_attr']['enabled']= "value"

then you would be able to access them in your recipe

You can also pass the value of your the attribute using the chef-client -j option.

0
votes

Abraham P has it right. chef-apply is much like a shell script interpreter. You can even hash bang your file.

#!/usr/bin/env chef-apply

# define attributes here

# create chef resources and other valid chef/ruby code