5

I have the following definition in a yaml file:

keepalived:
    cluster_name: "cluster.example.lan"
    cluster_ip: "192.168.1.10"
    cluster_nic: "eth0"
haproxy:
    bind_address: %{hiera('keepalived::cluster_ip')}

And as a result in bind_address I've got an empty string.

If I use %{hiera('keepalived')} I've got the whole hash printed, but I need only cluster_ip from this hash. How can I lookup the cluster_ip ?

kkamil
  • 2,593
  • 11
  • 21
Stanislav
  • 73
  • 1
  • 7

2 Answers2

8

I think it is not possible:

Hiera can only interpolate variables whose values are strings. (Numbers from Puppet are also passed as strings and can be used safely.) You cannot interpolate variables whose values are booleans, numbers not from Puppet, arrays, hashes, resource references, or an explicit undef value.

Additionally, Hiera cannot interpolate an individual element of any array or hash, even if that element’s value is a string.

You can always define cluster_ip as a variable:

common::cluster_ip: "192.168.1.10"

and than use it:

keepalived:
    cluster_name: "cluster.example.lan"
    cluster_ip: "%{hiera('common::cluster_ip')}"
    cluster_nic: "eth0"

haproxy:
    bind_address: "%{hiera('common::cluster_ip')}"
Community
  • 1
  • 1
kkamil
  • 2,593
  • 11
  • 21
3

Hiera uses the . in a string interpolation to look up sub-elements in an array or hash. Change your hiera code to look like this:

keepalived:
  cluster_name: "cluster.example.lan"
  cluster_ip: "192.168.1.10"
  cluster_nic: "eth0"
haproxy:
  bind_address: %{hiera('keepalived.cluster_ip')}

For an array, you use the array index (0 based) instead of a hash key.

See interpolating hash or array elements

Rick Renshaw
  • 799
  • 4
  • 3
  • Since the accepted answer was accepted this has changed in more recent versions of hiera and is now possible, as shown in this answer. So it depends on your version of hiera! – David Gardner Oct 06 '16 at 14:27