0

I know where all we can define the chef attributes, attribute types and also their precendence levels. I just want to understand how they are stored internally.

Suppose I declare an attribute

default[:app][:install] = "/etc/app"

1) How is it stored internally? Is it using in a tree structure(heirearchy) in the node object or is it as hashmaps or a list of variables in the node object?

2) Also, in most of the cookbooks I see that attributes are declared in 2 or 3 levels something as above I dont understand if it is a standard or is it a best practice? Are there any guidelines for the way the attributes have to be declared? Is it something to do with its internal storage. Can't I declare the attribute as

 default[:appinstall]= "/etc/app"

and access it as below in my recipe?

  node[:appinstall]
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sun_dare
  • 1,146
  • 2
  • 13
  • 33

1 Answers1

2

Just four Mashes (subclass of Hash which does the string vs. symbol key fixups). When you access the merged view via node['foo'] it uses a Chef::Node::Attribute object to traverse all four in parallel until it finds a leaf value.

What you have shown is correct for setting and using attributes, though string keys are preferred over symbols. You should also in general scope your attributes with the name of the cookbook like:

default['mycookbook']['appinstall'] = '/etc/app'

This will reduce the chances of collisions with other cookbooks.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • By the the way why are string preferred over symbols? I have read some where that if you are using the same attribute multiple times its better to use symbols. – sun_dare Sep 17 '14 at 14:10
  • 2
    Mostly because it is clearer to people that don't know Ruby as well. Almost every language ever has used quoted strings to represent strings, but symbols and the Ruby syntax in particular are harder to explain. – coderanger Sep 17 '14 at 16:25