voltas
voltas

Reputation: 563

Chef node usage

I'm learning Chef and find it difficult to understand the usage of node, like the perspective of its usage.

I went thru the Chef documents but still unable to exactly get the concepts.

Example, below are the scenarios where 'node' is being used. Can anyone explain in a simple term about the usage of node for different scenarios. Thank you.

Scenario 1:

currentUser = node['myusers'][node['myenv'] - 1] #arrays start at 0, doing -1 for 2 pointing to second user
user currentUser do
  gid node['mygroup']
  home "/home/#{currentUser}"
end
execute "sudoers for #{currentUser}" do
  command "echo '#{currentUser} ALL=(ALL) ALL' >> /etc/sudoers"
  not_if "grep -F '#{currentUser} ALL=(ALL) ALL' /etc/sudoers"
end

Scenario 2:

pkg_resource = case node['platform_family']
  when "debian"
    :dpkg_package
  when "fedora", "rhel", "amazon"
    :rpm_package
  end

Scenario 3: By using a node attribute:

source node['nginx']['foo123']['url']

Upvotes: 0

Views: 274

Answers (2)

santhos
santhos

Reputation: 46

For understanding consider Node attributes as a dictionary data type and as in dictionary every key has its own value. same way node['platform_family'] will have value as debian or centos depending upon the value it attain from ohai. ohai generally assigns value to the node attribute. there are other ways such as node.run_state which can use to assign value to a node[key] dynamically during chef run. eg node.run_state[weather]=sunny will assign node[weather] value as sunny.

Upvotes: 1

coderanger
coderanger

Reputation: 54249

Node attributes are a bit set of nested hashes. Basically a global variable that collects information from a lot of sources (roles, environment, cookbooks, the node itself, Ohai) and makes them available via a single API (namely the API of a global hash). How you use that data is entirely up to you and how you prefer to write your code. Some cookbooks use lots of node attributes, some use only data coming from Ohai.

Upvotes: 1

Related Questions