1

Here is a example snipit of code:

mwh_dir = "#{node['fmw']['middleware_home_dir']}"

I have just been using this syntax without being able to find details online of why I need to use the node stanza.

If someone could explain what the purpose of node is within that code.

note: fmw is an object defined in the .kitchen.yml and middleware_home_dir is a variable defined within that object.

aphexlog
  • 1,503
  • 3
  • 16
  • 43
  • 1
    "If someone could explain what the purpose of node is within that code." well conspicuously enough that is referencing a [`Node`](http://www.rubydoc.info/gems/chef/Chef/Node). `Node#[]` delegates to [`Node::Attribute#[]`](http://www.rubydoc.info/gems/chef/Chef/Node/Attribute) which is a subclass of [`Mash`](Chef's subclass of a native ruby `Hash`). So in order to access attributes in regards to an instance of `Node` you must as in most cases specify the receiver (`node` in this case) an call a method (`[]` with a given key argument). `node[key]` is identical to `node.attributes[key]` – engineersmnky Jan 02 '18 at 16:59

1 Answers1

2

The chef node is a way of referencing attributes.

In essence, node can be used as a Ruby Hash which is publically available to your recipe code. Attributes can be set in the .kitchen.yml, or defaults can be set in the attributes/default.rb file of the cookbook. These attributes will be provided as hash mapped values in the node variable.

the_storyteller
  • 2,335
  • 1
  • 26
  • 37
  • `node` is a [`Node`](http://www.rubydoc.info/gems/chef/Chef/Node). `Node::Attribute` as delegated by `Node#[]` is similar to a `Hash` but `Node` is an `Object` and contextually very different from a `Hash` – engineersmnky Jan 02 '18 at 16:57