Stacy Proficy
Stacy Proficy

Reputation: 41

What's the difference between these two statements in a Chef script?

I am using Chef to install packages. I am getting an error on line

node.default["installed_pkgs"] << 'amanda'


Undefined node attribute or method `<<' on `node'. To set an attribute, use `<<=value' instead.

If I change it to:

node.default["installed_pkgs"] = 'amanda'

it seems to work, or at least it doesn't error out. I took this over from someone that quit so I'm not sure of his code since I don't know Chef or Ruby that much.

Upvotes: 0

Views: 663

Answers (2)

Makoto
Makoto

Reputation: 106508

The difference is in Ruby.

<< is shorthand for appending to the end to an array. If you have an array and you want to append to the end of it, then you would use <<.

= is vanilla assignment. You use this to assign a value to a variable or hash, but you have to be sure that the value you're assigning is appropriate; if you assign a scalar value when Ruby expects an array, then your program will error out.

Upvotes: 1

Mircea
Mircea

Reputation: 10566

your problem comes from her: https://github.com/chef/chef/blob/d8172e646d9fbf43e57bca5e20d0ac352ba9a66a/lib/chef/node/attribute_collections.rb#L175

node does not know about << and thinks it's an attribute.

use

node.default["installed_pkgs"] = 'amanda'

Upvotes: 2

Related Questions