简体   繁体   中英

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

I have defined a puppet function check_value in module test_one

test_one
 |- functions
     |- check_value.pp

and the function declaration:

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

I declared a class test_functions within the same module.

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

Everything seems to be perfect and I can call this function check_value from the class test_functions within the same module and could fetch the return value.

However, if I call this function from another module, I get Evaluation Error: Unknown function: ...

test_two
 |- manifests
     |- test_external_function.pp

In the class test_external_function , I tried several ways to call check_value but with no luck:

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")

All trials have failed. Is it possible to call and use these puppet (non-ruby) functions from another module? I couldn't seem to find a way. Google is of no help so far!

As per the puppet documentation, it is possible: 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.

This is due to a relatively new requirement introduced in Puppet coding that is documented here . Specifically:

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

Typically, module generation via either the PDK or puppet module generate will create a placeholder metadata.json with puppetlabs/stdlib as a dependency. This is normally fine in practice, but it will break autoloading custom functions from other modules.

In this case (and I guess you would also say in general per best practices), you would want to specify other modules with custom functions you are invoking as dependencies in your metadata.json for the module. For example, in test_two/metadata.json , you could have something like:

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

with full documentation on specifying dependencies here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM