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?

sarnold
  • 102,305
  • 22
  • 181
  • 238
erikcw
  • 10,787
  • 15
  • 58
  • 75

3 Answers3

35

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

node.name
jarrad
  • 3,292
  • 1
  • 23
  • 15
31

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

Marcus
  • 12,296
  • 5
  • 48
  • 66
Shuang Wu
  • 541
  • 4
  • 6
  • 2
    I concur with @anaudmeuret, this solution worked for me in Shef and in a recipe, where the accepted answer did not. – jslatts Feb 08 '12 at 18:32
  • 1
    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. – Andrew Apr 07 '15 at 14:11
  • 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 recipe – jorfus Aug 31 '16 at 01:15
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",
Daniil Iaitskov
  • 5,525
  • 8
  • 39
  • 49