Невозможно найти параметр класса в hiera - PullRequest
0 голосов
/ 07 июля 2019

Я смотрю на другие вопросы, такие как Использование 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

Я попытался отладить hiera с puppet lookup примерно так:

[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 Ответ

1 голос
/ 07 июля 2019

На основании информации в вопросе я не могу воспроизвести это. Вот мои настройки, если это поможет:

# 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

Вы также можете запросить иерархию Hiera напрямую, используя puppet lookup, например:

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