无法在Hiera中查找类参数



我还有其他问题,例如使用hiera设置类参数?和讨论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。我在这里做错了什么?

谢谢

基于问题中的信息,我无法再现此内容。如果有帮助,这是我的设置:

# 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

在木偶版本上测试:

▶ 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

相关内容

  • 没有找到相关文章

最新更新