user1529891
user1529891

Reputation:

During a chef run how does node differ from node.default?

I've noticed that attributes in environments do not get applied if I use node.default in cookbooks (instead the cookbook attributes get applied). But, the attributes changed in environments get applied if I use node instead; why? I thought node and node.default are one and the same?

Upvotes: 3

Views: 4624

Answers (2)

Tejay Cardon
Tejay Cardon

Reputation: 4223

node gives you a view into the entire node object. node.default only tells you what values were set at the default level. As a result, you should NEVER read from node.default as you would be explicitly ignoring all higher precedent levels. ie, your overrides would not count. node.default exists to allow you to SET default values on the node.

Upvotes: 3

Mark O'Connor
Mark O'Connor

Reputation: 78021

Node attribute precedence can be confusing. The document does help, once you read it a couple of times

https://docs.chef.io/attributes.html

My advice is as follows:

  1. Set default attributes in an attributes file within your cookbook. Don't confuse yourself by doing this within recipes as well.
  2. Override the attributes of other cookbooks by setting "normal" attributes and again do this in an attributes file within your cookbook
  3. At runtime using Environment or Role attributes to override all attributes set within cookbooks.
  4. Never edit the node attributes directly on the chef server. There is no transactional locking, which means your update could be lost if a chef client is running at the same time....

The objective here is consistency

Upvotes: 4

Related Questions