29

I'm using puppet to configure servers. I want to print current machine (node) name in *.erb template. There is hostname variable, but this holds puppetmaster hostname. Is there any good reference/list regarding to this topic?

Michal Bryxí
  • 1,166
  • 1
  • 11
  • 18

5 Answers5

38

Seems like I have miss-looked somewhere. I can get(print) node-hostname simply by invoking following code in *.erb template:

<%= @hostname %>

Edit: As of Puppet 3, using local variables (i.e. hostname is deprecated; Puppet 4 will remove support for them entirely. The recommended way is to use instance variables (prefixed with @. So in this case, @hostname. Source

peelman
  • 1,339
  • 1
  • 11
  • 13
Michal Bryxí
  • 1,166
  • 1
  • 11
  • 18
  • ah exactly, I was really puzzled, because in my case for "fqdn" I get the full hostname (like "myhost.acnemcom"), while for "hostname" I get the short version (the one returned by the command hostname -s) , like "myhost". In no case I get the puppetmaster host. – Pierluigi Vernetto Sep 12 '14 at 16:48
32

Entering the following command into your terminal will return variables available in your erb files:

facter --puppet
pcting
  • 1,286
  • 14
  • 20
6

Yes to all the above; you may also find the fqdn variable useful, depending on what you're up to.

If you DO find yourself needing the puppet master server's info in a template, you can use the variables servername, serverip, and serverversion (From the Puppet docs).

IBBoard
  • 909
  • 4
  • 16
nfagerlund
  • 111
  • 1
  • 3
4

Apparently you can now use

<%= fqdn %> 

and get the DNS reverse name.

If you need to lowercase it, use

<%= fqdn.downcase %>
Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
mtneagle
  • 136
  • 3
3

Do note though, this is the hostname rather than the node name (which you originally asked for). In 99% of cases, these will be the same. AFAIK there isn't a way to get the actual node name in templates (and if there is, I'd love to hear it).

adamcooke
  • 386
  • 3
  • 3