Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex hiera lookup not working

Tags:

puppet

hiera

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 ?

like image 226
Stanislav Avatar asked Jan 09 '15 12:01

Stanislav


People also ask

How many lookup functions does hiera?

When looking up a key, Hiera searches up to four hierarchy layers of data, in the following order: Global hierarchy. The current environment's hierarchy. The indicated module's hierarchy, if the key is of the form <MODULE NAME>::<SOMETHING> .

How does hiera work in puppet?

Hiera uses Puppet's facts to specify data sources, so you can structure your overrides to suit your infrastructure. While using facts for this purpose is common, data-sources can also be defined without the use of facts.

What is hiera Yaml?

The Hiera configuration file is called hiera. yaml . It configures the hierarchy for a given layer of data.


Video Answer


2 Answers

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')}"
like image 110
kkamilpl Avatar answered Oct 16 '22 02:10

kkamilpl


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

like image 21
Rick Renshaw Avatar answered Oct 16 '22 02:10

Rick Renshaw