在木偶中使用模块 hiera 时遇到一些问题

Having some trouble using module hiera in puppet

我在使用模块 hiera 数据时遇到了一些问题。

模块:/etc/puppetlabs/code/environments/production/modules/usehiera

树结构:

usehiera
usehiera/hiera.yaml
usehiera/data
usehiera/data/common.yaml
usehiera/manifests
usehiera/manifests/init.pp

hiera.yaml:

---
version: 5
defaults:  
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: 'common'
  - path: 'common.yaml'

data/common.yaml:

---
usehiera::apples: 'this is some data'

manifests/init.pp:

class usehiera{
    file{'/tmp/hiera_lookup.txt':
        ensure => present,
        #content => hiera('oranges') #this works with global hiera
        content => $apples
    }
}

如您所见,当我在我的节点上 运行 这个模块时,我似乎让全局 hiera 与“hiera('oranges')”一起工作。当我尝试使用模块 hiera 数据时,人偶 运行 成功完成但 hiera_lookup.txt 只是空的。

我已采取的故障排除步骤:

  1. hiera 更改后重启 puppetserver
  2. 尝试使用 $usehira::apples
  3. 尝试使用 hiera('apples')
  4. 移动我的 hiera.yaml 内部数据/
  5. 使用 lookup with --explain 并没有给我任何有用的信息,只是说 lookup() not found

谁能帮帮我?我已经坚持了相当长的一段时间,但不确定问题出在哪里。

正如@MattSchuchard 在评论中指出的那样,您的 hiera.yaml 格式不当。 The documentation 包含示例。

但更大的问题似乎是不正确的期望。显然,您假设普通 class 变量 $usehiera::apples 将自动获取与 module-level hiera 数据中相应键关联的值,但事实并非如此。 Hiera 数据——无论是全局、environment-level 还是 module-level——自动绑定到 class参数,但不会绑定到其他 class ]变量。

您可以通过显式查找从 hiera 数据设置普通 class 变量:

# the hiera() function is deprecated; use lookup():
$apples = lookup('usehiera::apples')

或者,您可以将 $apples 设为 class 参数:

class usehiera(String $apples) {
  file{'/tmp/hiera_lookup.txt':
    ensure  => 'present',
    content => $apples,
  }
}

请注意,如果您将其作为参数,那么它的值也可以通过 resource-like class 声明进行自定义,这优先于您的 Hiera 数据。

另请注意,全局、per-environment 和 module-specific Hiera 数据之间的区别只是范围和优先级,而不是功能。