Information Technology Reference
In-Depth Information
NOTE
The string variant
node['virtualization']['system']
tends to be the most commonly
used node attribute form. However, because attribute expressions are evaluated as a
Mash
,
you'll encounter Chef code that uses the other possible
Mash
variants:
▪
node[:virtualization][:system]
▪
node['virtualization']['system']
▪
node.virtualization.system
Use a form that makes the most sense to you.
Let's make these examples more concrete by using them in a Chef recipe.
Accessing Node Information
As we discussed in the last section,
chef-client
collects a lot of information about the state
of a node using
ohai
. Collecting this information is necessary so that Chef can intelligently
reason how to put the node into the desired configuration specified in a recipe. Chef does not
keep this information to itself. It makes this information available to your Chef code as a
node
attribute. An attribute is a variable maintained by Chef.
node
information. Create a new file called
info.rb
on the node with the following sequence of
commands:
[vagrant@default-centos65 ~]$
cat << EOF > info.rb
>
log "IP Address: #{node['ipaddress']}"
>
log "MAC Address: #{node['macaddress']}"
>
log "OS Platform: #{node['platform']} #{node['platform_version']}"
>
log "Running on a #{node['virtualization']['system']} \
>
#{node['virtualization']['role']}"
>
log "Hostname: #{node['hostname']}"
>
EOF
This command will produce the source file seen in
Example 6-3
.