attributeschef-infrachef-recipecookbookchef-attributes

How are Chef attributes stored internally


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]

Solution

  • 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.