Undefined method or attribute error in a Chef recipe

There are multiple reasons Chef can display the following error message

NoMethodError
 -------------
 Undefined method or attribute `...' on `node'

 

There are many ways to reference an attribute in a Chef recipe:

node['ATTRIBUTE_NAME'] (the recommended style)
node["ATTRIBUTE_NAME"] 
node[:ATTRIBUTE_NAME]  (use it only if the single or double quotes (' or ") would cause a problem in the expression. This can happen in the guard of PoweShell resources.)
node.ATTRIBUTE_NAME    (DO NOT USE IT: the bootstrap compiler cannot understand it, chef-client cannot handle it with nil values in if statements)

To check if the attribute value is nil, use the following format:

if ( !node['ATTRIBUTE_NAME'].nil? )
   ...
end

If you use the node.ATTRIBUTE_NAME.nil? to test the value, and the value is nil, Chef throws the above error message.

Leave a comment

Your email address will not be published. Required fields are marked *