Puppet 查找无法找到 hiera 键值

Puppet lookup fails to find hiera key value

我在 puppet 查找调用中看到了这个错误(使用 hiera 的基本测试):

puppet lookup --explain foo

Searching for "lookup_options"
  Global Data Provider (hiera configuration version 5)
    No such key: "lookup_options"
Searching for "foo"
  Global Data Provider (hiera configuration version 5)
    No such key: "foo"
Function lookup() did not find a value for the name 'foo'

尝试直接 hiera 查找有效:

hiera -d foo
DEBUG: 2019-01-16 11:53:39 +0000: Hiera YAML backend starting
DEBUG: 2019-01-16 11:53:39 +0000: Looking up foo in YAML backend
DEBUG: 2019-01-16 11:53:39 +0000: Looking for data source common
DEBUG: 2019-01-16 11:53:39 +0000: Found foo in common
bar

我的 hiera.yaml 文件(位于 /etc):

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

我的 common.yaml 文件(位于 /var/lib/hiera):

---
foo: bar

谁能解释为什么我会看到这个错误(对 Puppet 来说还很陌生)...

这是一个文件位置问题。

我有:

▶ cat spec/fixtures/hiera/hiera.yaml 
---
version: 5
hierarchy:
  - name: Common
    path: common.yaml
defaults:
  data_hash: yaml_data
  datadir: data

结构:

▶ tree spec/fixtures/hiera
spec/fixtures/hiera
├── data
│   └── common.yaml
└── hiera.yaml

命令行:

▶ puppet lookup --hiera_config=spec/fixtures/hiera/hiera.yaml foo 
--- bar

请注意 defaults.datadirhiera.yaml 中引用的 data 目录必须相对于 hiera.yaml 所在的目录。 Ref:

datadir — The directory where data files are kept; can be omitted if you set a default.

This path is relative to hiera.yaml’s directory: if the config file is at /etc/puppetlabs/code/environments/production/hiera.yaml and the datadir is set to data, the full path to the data directory is /etc/puppetlabs/code/environments/production/data.

In the global layer, you can optionally set the datadir to an absolute path; in the other layers, it must always be relative.