|
OverviewAttributes are Node data such as the IP address, hostname, loaded kernel modules, version of programming languages available on the system and more. New attributes can be dynamically added to the node in a variety of ways.
|
|
PrecedenceThe precedence of the attributes is as follows, from low to high:
default attributes applied in an attributes file have the lowest priority and override attributes applied on a node directly in a recipe have the highest priority. Write your cookbooks with default attributes, but override these with role-specific or node-specific values as necessary. The exception to this are Automatic Attributes. They have the highest precedence and may not be modified, as they are regenerated by Ohai every time Chef runs. NotationAttributes are a special key-value store called a Mash within the Ruby DSL context. A Mash is just a Hash where the key can be either a Ruby symbol (:key) or a string ("key"). It's easier to stick with string notation if you are just starting out with Chef and/or Ruby. This allows you to just "quote it", rather than also having to learn about special cases, name space overlap (and method_missing), character constraints, and escaping work at the beginning. That way, by the time you may want to use symbols stylistically or contextually, you can learn to wield them with an existing base of knowledge. Our examples and detail on attributes use a string notation for that reason. If you already know your way around symbols in ruby, you may of course use them. Cookbook AttributesCookbook attribute files are found in the attributes subdirectory of the cookbook. They are evaluated in the context of the Node object and Node methods are used to set attribute values. e.g. from Opscode's Apache cookbook: cookbooks/apache2/attributes/default.rb The use of the node object is implicit here. The following is equivalent: cookbooks/apache2/attributes/default.rb Attributes can be set in a recipe as well, but node must be used. Cookbook Attribute MethodsUse the following methods within a cookbook's attributes file or in a recipe. They correspond to the attribute type of the same name (set is an alias for normal).
Additionally, there are _unless methods available. See the end of this document for information on conditionally setting attributes Another handy method available related to attributes is the attribute? method. This will check for the existence of an attribute, so you can do processing in an attributes file or recipe only if a specific attribute exists. attribute?() in attributes file attribute?() in recipe In the recipe, we need to use the method on the node object. In the attributes file, the node object is implicit. In either, we can also look for a sub-key of an attribute by chaining the attribute as methods: attribute?() in recipe Cookbook Attribute File OrderingWhen Chef loads cookbook attribute files, it does so in alphabetical order for all the cookbooks. If you need to ensure that one attribute file is loaded before another (for example, if your Rails cookbook requires that the Apache attributes are available first) you can use the include_attribute method, like so: include_attribute This loads apache/attributes/default.rb before continuing the processing of the current attribute file. The syntax for this follows the same "double colon" pattern as include_recipe, so a statement like: include_attribute This loads the attributes/tunables.rb file in a rails cookbook. Reloading Attribute Files From RecipesAttributes sometimes depend on actions taken from within recipes, so it may be necessary to reload a given attribute from within a recipe. For example: if you have an attribute that reads firewall rules, and a recipe that installs a firewall package, the firewall attributes will not be set the first time you execute the cookbook. Since include_attribute is not available from inside recipes, you will need to manually reload your firewall::default attribute: reloading attributes from recipes Attribute Accessor MethodsAttribute accessor methods are automatically created and the method invocation can be used interchangeably with the keys. The following is equivalent to the usage above: cookbooks/apache2/attributes/default.rb This is a matter of style, and may be seen when "retrieving" the value of an attribute.
|
|
|


