10

So suppose in a very specific hiera YAML file I define a variable, such as "env_name".

env_name: "dev-unstable"

Now in a more general hiera file I'd like to interpolate that variable into a string.

server_name: "service-%{env_name}.%{::domain}"

My testing seems to imply that hiera variables from elsewhere in the hierarchy aren't made available for interpolation in general cases. Is that true, unfortunately?

Greg Dubicki
  • 1,191
  • 1
  • 14
  • 30
Mojo
  • 925
  • 1
  • 9
  • 24

3 Answers3

5

You can use Hiera lookup functions within Hiera. Documentation here: https://docs.puppetlabs.com/hiera/1/variables.html#using-lookup-functions

In your case you would use:

server_name: "service-%{hiera('env_name')}.%{::domain}"

This is a priority lookup and will get the value for the key env_name from the highest (most specific) hierarchy level in which it finds the key.

This requires Hiera 1.3 or higher.

daff
  • 4,729
  • 2
  • 26
  • 27
4

For Hiera 5, used in Puppet 5, use the following syntax:

server_name: "service-%{lookup('env_name')}.%{::domain}"

...where %{lookup('env_name')} is the part that gets the data from other hiera key.

(Source)

Greg Dubicki
  • 1,191
  • 1
  • 14
  • 30
  • 2
    Note: save yourself an hour and notice that there is a Puppet `lookup` function and a Hiera `lookup` function with different signatures. The documentation is not clear about this (just includes a subnote) and as a reader, it is very difficult to search for one or the other. – Jonathan Komar Oct 08 '20 at 08:03
1

See the answer from Greg Dubicki.

To quote "The hiera, hiera_array, hiera_hash, and hiera_include functions are all deprecated. The lookup function is a complete replacement for all of these."

For details see:

https://puppet.com/docs/puppet/5.5/hiera_migrate.html#updated_classic_hiera_function_calls