Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Why in all OpsCode's cookbooks there is a

default[:netkernel][:version] = "4.1.1"
default[:netkernel][:install_url] = "http://apposite.netkernel.org/dist/1060-NetKernel-SE/1060-NetKernel-SE-4.1.1.jar"

default[:netkernel][:frontend][:port] = 8080

default[:netkernel][:backend][:address] = "localhost"
default[:netkernel][:backend][:port] = 1060

default[:netkernel][:user] = "netkernel"
default[:netkernel][:log_path] = "/var/log/netkernel"

instead of

default[:netkernel] = {
  :version => "4.1.1",
  :install_url => "http://apposite.netkernel.org/dist/1060-NetKernel-SE/1060-NetKernel-SE-4.1.1.jar",
  :frontend => {
    :port => 8080
  },
  :backend => {
    :address => "localhost"
  },
  :backend => {
    :port => 1060
  },
  :user => "netkernel",
  :log_path => "/var/log/netkernel"
}

Is this attributes definition equal to the first one?

Why first variant is prefered?

share|improve this question

2 Answers 2

They are indeed quite different and there for a reason, Mike's answer doesn't take into account how Chef works ( no offence meant ).

When looking a node attributes, you have to take into account the fact that a node has a run_list which defines the order in which cookbooks are run. Before any of the recipes are run, chef will iterate through the run_list and bring in cookbooks, each time merging in that cookbook's attributes.

In addition, each of the run_list element's dependent cookbooks will also be rolled in ( worth nothing dependent cookbooks are evaluated before the "parent" ).

So let's say you had two cookbooks "base_netkernel" and "specific_case_netkernel", you could have valid defaults set in base_netkernel and then only provide the attributes that are different / specific in specific_case_netkernel.

Hopefully that makes sense ;) If not, there's always the docs : https://docs.getchef.com/chef_overview_attributes.html

share|improve this answer

These are identical. The only advantage I can see with the first example is that it's possible to create attributes based on the values of other attributes. e.g.

default[:foo][:a] = "a"
default[:foo][:b] = "b"
default[:foo][:ab] = "#{foo[:a]}#{foo[:b]}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.