复杂的 hiera 查找不起作用

Complex hiera lookup not working

我在 yaml 文件中有以下定义:

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

结果 bind_address 我得到了一个空字符串。

如果我使用 %{hiera('keepalived')} 我已经打印了整个散列,但我只需要这个散列中的 cluster_ip。我如何查找 cluster_ip ?

我觉得不是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.

您始终可以将 cluster_ip 定义为变量:

common::cluster_ip: "192.168.1.10"

然后使用它:

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

haproxy:
    bind_address: "%{hiera('common::cluster_ip')}"

Hiera 使用 .在字符串插值中查找数组或散列中的子元素。将您的 hiera 代码更改为如下所示:

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

对于数组,您使用数组索引(基于 0)而不是散列键。

interpolating hash or array elements