如何从与其定义的模块不同的模块调用木偶函数

How to call a puppet function from a different module than the module it is defined in

我在模块 test_one

中定义了一个人偶函数 check_value
test_one
 |- functions
     |- check_value.pp

和函数声明:

test_one::check_value(String $check) >> String {
    ...
}

我在同一模块中声明了 class test_functions

test_one
 |- functions
     |- check_value.pp
 |- manifests
     |- test_functions.pp

一切似乎都很完美,我可以从同一模块中的 class test_functions 调用此函数 check_value,并可以获取 return 值。

但是,如果我从另一个模块调用这个函数,我会得到 Evaluation Error: Unknown function: ...

test_two
 |- manifests
     |- test_external_function.pp

在 class test_external_function 中,我尝试了几种调用 check_value 的方法,但没有成功:

1. $x = test_one::check_value("t")


2. include test_one
   $x = check_value("t")  


3. include test_one
   $x = test_one::check_value("t")

所有试验都失败了。是否可以从另一个模块调用和使用这些人偶(非ruby)函数?我似乎找不到办法。 Google 目前没有任何帮助!

根据 puppet 文档,有可能: Puppet Functions

Functions are autoloaded and made available to other modules unless those modules specify dependencies. Once a function is written and available (in a module where the autoloader can find it), you can call that function in any Puppet manifest that lists the containing module as a dependency, and also from your main manifest.

这是由于 Puppet 编码中引入了一项相对较新的要求,该要求已记录在案 here。具体来说:

Note that if a module has a list of dependencies in its metadata.json file, it loads custom functions only from those specific dependencies.

通常,通过 PDK 或 puppet module generate 生成的模块将创建占位符 metadata.json,并将 puppetlabs/stdlib 作为依赖项。这在实践中通常很好,但它会破坏从其他模块自动加载自定义函数。

在这种情况下(我想您通常也会根据最佳实践说),您可能希望在 metadata.json 中为模块指定具有您正在调用的自定义函数的其他模块作为依赖项。例如,在 test_two/metadata.json 中,你可以有这样的东西:

"dependencies": [
  { "name": "org_name/test_one", "version_requirement": ">= 1.0.0 < 2.0.0" },
]

包含有关指定依赖项的完整文档here