deeper merge_behaviour 对于 hiera 5 不工作,但在 hiera 3 中工作

deeper merge_behaviour for hiera 5 not working, but working in hiera 3

深度合并在 hiera 5 中不起作用。

我在 main hiera.yaml 版本 3 中使用 merge_behavior: deeper,因此它会在找到它的地方合并所有 hiera 数据,但现在我已经升级到版本 5,并发现合并行为不起作用?

:merge_behavior: deeper

任何帮助将不胜感激

根据官方 Puppet documentation for upgrading from Hiera 3 to Hiera 5:

These have no equivalent support in a version 5. If you’d like to learn about how Hiera 5 supports deep hash merging, see Merging data from multiple sources.

我最终在 puppet 文件中使用 lookup() 函数代替了 hiera_hash() 函数。像这样的东西。

来自

hiera_hash( 'firewall::firewalld::zones', {} )

lookup( { 'name' => 'firewall::firewalld::zones',
                                'merge' => { 
                                  'strategy' => 'deep',
                                  'knockout_prefix' => '--',
                                }, 
                                'default_value' => {} 
                      })

正如文档 Hiera: Merging data from multiple sources 所说:

Note: Hiera 5’s deep merge is equivalent to Hiera 3’s “deeper” merge.

您可以在一些 YAML 配置中定义查找规则:

lookup_options:
  "^firewall::(.*)":
    merge:
      strategy: deep

从 CLI 使用 puppet lookup --explain needle 可能有助于理解正在发生的事情:

$ puppet lookup --explain firewall::firewalld::zones
Searching for "lookup_options"
  Global Data Provider (hiera configuration version 5)
    Using configuration "/etc/puppetlabs/puppet/hiera.yaml"
    Merge strategy hash
      Hierarchy entry "Defaults"
        Path "/etc/puppetlabs/puppet/hieradata/default.yaml"
          Original path: "default.yaml"
          Found key: "lookup_options" value: {
            "^firewall::(.*)" => {
              "merge" => {
                "strategy" => "deep"
              }
            }
          }
      Merged result: {
        "^firewall::(.*)" => {
          "merge" => {
            "strategy" => "deep"
          }
        }
      }
  ...

您可以将 hiera_hash( 'firewall::firewalld::zones', {} ) 替换为:

lookup('firewall::firewalld::zones', Hash, {'strategy' => 'deep'}, {})

或者使用预定义的 lookup_options 你最终会得到像这样的简单代码:

lookup('firewall::firewalld::zones')