How to reference a hiera variable from elsewhere the hierarchy?

Solution 1:

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.

Solution 2:

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)