Node Name in Chef Template?
Asked Answered
B

3

23

I've been trying to figure out how to get the node's name (knife bootstrap -N) into a template. I've tried a dozen different ideas and haven't found anything that works yet. Does anyone know how to access this from a Chef recipe?

Also -- is there a way to list all of the variables available to a Chef recipe?

Beat answered 16/10, 2011 at 0:40 Comment(0)
A
35

A cleaner more concise way is shown on the Attributes wiki page:

node.name
Alow answered 24/4, 2012 at 14:17 Comment(5)
This gives the FQDN. Any way to get the short name?Marijn
I don't know a very chef-ly way to do it, but node.name.split('.')[0] works okMarijn
You should always use the node[:attribute] syntax when getting chef attributes. The ohai dervied attribute is at node[:hostname]Supernaturalism
@Alow note that your link is now broken because chef docs have moved. Would you be able to update it to the right one (i think docs.chef.io/ohai.html)?Jinx
@Jinx I have updated the answer. Thanks for the heads up!Alow
B
31

You can use Chef::Config[:node_name] in your recipe. I found this in the chef-client cookbook from Opscode.

Bellhop answered 23/11, 2011 at 20:31 Comment(3)
I concur with @anaudmeuret, this solution worked for me in Shef and in a recipe, where the accepted answer did not.Xeroderma
The original question is how to access node name in a template. This answer does not work for that. Actually, <%= node.name %> is the complete syntax for the .erb file, and is how to access the node name in a template.Swum
It seems the template syntax and recipe syntax are different. I've got a template that uses the <%=node.name%> syntax And just used the above Chef::Config[:node_name] syntax in a recipePizza
L
5

To see all node specific attributes type command

knife node edit <name> -a

First level keys accessible with "node." prefix.

{
  "name": "n1",
  ...
  "hostname": "chef-n1",
  "fqdn": "chef-n1.dan.lan",
  "domain": "dan.lan",
  "ipaddress": "192.168.4.4",
  "macaddress": "52:54:00:72:E7:C5",
Lopes answered 30/11, 2013 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.