无法在hiera中查找类参数

时间:2019-07-07 04:10:25

标签: puppet hiera

我查看了其他问题,例如Using hiera to set class parameters?以及其他讨论hiera 3的问题。我正在使用hiera 5。

这是我的hiera.yaml

[root@e64a2e5c7c79 fisherman]# cat /fisherman/fisherman/hiera/hiera.yaml
---
version: 5
defaults:  # Used for any hierarchy level that omits these keys.
  datadir: data         # This path is relative to hiera.yaml's directory.
  data_hash: yaml_data  # Use the built-in YAML backend.

hierarchy:
  - name: "Apps" # Uses custom facts.
    path: "apps/%{facts.appname}.yaml"

我也有这个hiera数据文件:

[root@e64a2e5c7c79 fisherman]# cat /fisherman/fisherman/hiera/apps/HelloWorld.yaml
---
fisherman::create_new_component::component_name: 'HelloWord'

但是当我像这样运行我的人偶代理时...

export FACTER_appname=HelloWorld
hiera_config=/fisherman/fisherman/hiera/hiera.yaml
modulepath=/fisherman/fisherman/modules
puppet apply --modulepath=$modulepath --hiera_config=$hiera_config -e 'include fisherman'

...我收到此错误...

Error: Evaluation Error: Error while evaluating a Function Call, Class[Fisherman::Create_new_component]: expects a value for parameter $component_name (file: /fisherman/fisherman/modules/fish
erman/manifests/init.pp, line: 12, column: 9) on node e64a2e5c7c79

我尝试像这样用puppet lookup调试hiera:

[root@e64a2e5c7c79 /]# export FACTER_appname=HelloWorld
[root@e64a2e5c7c79 /]# hiera_config=/fisherman/fisherman/hiera/hiera.yaml
[root@e64a2e5c7c79 /]# modulepath=/fisherman/fisherman/modules
[root@e64a2e5c7c79 /]# puppet lookup --modulepath=$modulepath --hiera_config=$hiera_config --node agent.local --explain fisherman::create_new_component::component_name
Searching for "lookup_options"
  Global Data Provider (hiera configuration version 5)
    Using configuration "/fisherman/fisherman/hiera/hiera.yaml"
    Hierarchy entry "Apps"
      Path "/fisherman/fisherman/hiera/data/apps/.yaml"
        Original path: "apps/%{facts.appname}.yaml"
        Path not found
  Environment Data Provider (hiera configuration version 5)
    Using configuration "/etc/puppetlabs/code/environments/production/hiera.yaml"
    Merge strategy hash
      Hierarchy entry "Per-node data (yaml version)"
        Path "/etc/puppetlabs/code/environments/production/data/nodes/.yaml"
          Original path: "nodes/%{::trusted.certname}.yaml"
          Path not found
      Hierarchy entry "Other YAML hierarchy levels"
        Path "/etc/puppetlabs/code/environments/production/data/common.yaml"
          Original path: "common.yaml"
          Path not found
  Module data provider for module "fisherman" not found
Searching for "fisherman::create_new_component::component_name"
  Global Data Provider (hiera configuration version 5)
    Using configuration "/fisherman/fisherman/hiera/hiera.yaml"
    Hierarchy entry "Apps"
      Path "/fisherman/fisherman/hiera/data/apps/.yaml"
        Original path: "apps/%{facts.appname}.yaml"
        Path not found
  Environment Data Provider (hiera configuration version 5)
    Using configuration "/etc/puppetlabs/code/environments/production/hiera.yaml"
    Hierarchy entry "Per-node data (yaml version)"
      Path "/etc/puppetlabs/code/environments/production/data/nodes/.yaml"
        Original path: "nodes/%{::trusted.certname}.yaml"
        Path not found
    Hierarchy entry "Other YAML hierarchy levels"
      Path "/etc/puppetlabs/code/environments/production/data/common.yaml"
        Original path: "common.yaml"
        Path not found
  Module data provider for module "fisherman" not found
Function lookup() did not find a value for the name 'fisherman::create_new_component::component_name'

我在上面的输出中注意到了这一点:

    Hierarchy entry "Apps"
      Path "/fisherman/fisherman/hiera/data/apps/.yaml"
        Original path: "apps/%{facts.appname}.yaml"
        Path not found

看起来facts.appname是空的,而不是我所期望的HelloWorld。 我在这里做错了什么?

谢谢

1 个答案:

答案 0 :(得分:1)

根据问题中的信息,我无法重现此问题。这是我的设置(如果有帮助的话):

# init.pp
class test (
  String $component_name,
  ) {
  notify { $facts['appname']:
    message => "Component name: $component_name for fact appname of ${facts['appname']}"
  }
}
# hiera.yaml
---
version: 5
defaults:
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: "Apps" # Uses custom facts.
    path: "apps/%{facts.appname}.yaml"
# data/apps/HelloWorld.yaml
---
test::component_name: 'MyComponentName'
# spec/classes/test_spec.rb
require 'spec_helper'

describe 'test' do
  let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
  let(:facts) {{ 'appname' => 'HelloWorld' }}
  it {
    is_expected.to contain_notify("HelloWorld")
      .with({
        'message' => "Component name: MyComponentName for fact appname of HelloWorld"
      })
  }
end

在Puppet版本上测试:

▶ bundle exec puppet -V 
6.6.0

输出:

▶ bundle exec rake spec                            
I, [2019-07-07T16:42:51.219559 #22140]  INFO -- : Creating symlink from spec/fixtures/modules/test to /Users/alexharvey/git/home/puppet-test
/Users/alexharvey/.rvm/rubies/ruby-2.4.1/bin/ruby -I/Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/rspec-core-3.8.2/lib:/Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/rspec-support-3.8.2/lib /Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/rspec-core-3.8.2/exe/rspec --pattern spec/\{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit\}/\*\*/\*_spec.rb

test
  should contain Notify[HelloWorld] with message => "Component name: MyComponentName for fact appname of HelloWorld"

Finished in 0.1444 seconds (files took 0.9699 seconds to load)
1 example, 0 failures

您还可以像这样直接使用puppet lookup查询Hiera层次结构:

▶ FACTER_appname=HelloWorld bundle exec puppet lookup \
    --hiera_config=spec/fixtures/hiera/hiera.yaml test::component_name
--- MyComponentName