best practice for accessing facts from ERB template?

31 views
Skip to first unread message

Tim Mooney

unread,
Jul 29, 2025, 9:56:50 PMJul 29
to puppet...@googlegroups.com

All-

Is there a canonical way to access hierarchical facts like
$facts['os']['release']['major'] or $facts['networking']['fqdn'] from
an ERB template? I've been searching for a best practice answer and
so far I've only found conflicting advice.

I'm specifically asking about facts, not variables from other
classes/scope.

I've found recommendations suggesting each of these:

<%= $facts['os']['release']['major'] %>
<%= @facts['os']['release']['major'] %>
<%= $facts.dig("os","release","major") %>
<%= @facts.dig("os","release","major") %>

as well as suggestions to use scope[] or scope.lookupvar() or use dotted
fact notation, but I haven't figured out how to make either the scope or
dotted route work in ERB.

The "$facts.dig()" suggestion comes directly from the Puppet 8
compatibility wiki entry:

https://github.com/puppetlabs/puppet/wiki/Puppet-8-Compatibility

but it doesn't seem to work for me. Using the @facts.dig() does, though.

So far the two methods I've found that work are

<%= @facts['os']['release']['major'] %>

or

<%= @facts.dig("os","release","major") %>

Is there any reason to prefer one over the other, or some other method
that I haven't found yet?

I don't want to convert our existing ERB templates to EPP.

I know I could set local variables in a puppet class like

$osreleasemajor = $facts['os']['release']['major']
$fqdn = $facts['networking']['fqdn']

and then access the local variables in the template, but if possible I
would like to avoid that approach.

Thanks,

Tim
--
Tim Mooney Tim.M...@ndsu.edu
Enterprise Computing & Infrastructure /
Division of Information Technology / 701-231-1076 (Voice)
North Dakota State University, Fargo, ND 58105-5164

Dirk Heinrichs

unread,
Jul 30, 2025, 1:54:47 AMJul 30
to puppet...@googlegroups.com
Am Dienstag, dem 29.07.2025 um 20:56 -0500 schrieb 'Tim Mooney' via Puppet Users:

I've found recommendations suggesting each of these:

        <%= $facts['os']['release']['major'] %>
        <%= @facts['os']['release']['major'] %>
        <%= $facts.dig("os","release","major") %>
        <%= @facts.dig("os","release","major") %>

The "$" variants are non-sense. Use "@" to access variables defined OUTSIDE the template (@somevar or @facts[]) and omit it from variables defined INSIDE the template.

as well as suggestions to use scope[] or scope.lookupvar() or use

"scope[]" MUST be used to access variables defined in a different class, for example if a template is used in a sub-class, but the variables it uses are defined in the main class. The classical example for this is the separation of "myclass" into "init.pp" (which defines the parameters), "install.pp", "config.pp" (where a config file is generated from a template) and "service.pp".

The notation in this case is

<%= scope['myclass::param'] =>

or, if the parameter or variable is an array or hash map

<%= scope['myclass::param'][<index or key>] =>

HTH...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.
signature.asc
Reply all
Reply to author
Forward
0 new messages